Added the possibility to write directly to Influx

Since I am not using Influx as a DB of my HomeAssistant setup, I wanted a way to bring the data there. Another issue is that I used another script to gather the data and hence have other naming conventions on my dashboards. Therefore the name of the measurements are now part of the config JSON file. This could be an idea adopted to MQTT as well.
This commit is contained in:
Martin Valov
2020-11-08 22:38:50 +01:00
parent a7f161b9d6
commit 313c98c119
2 changed files with 65 additions and 2 deletions
+43 -1
View File
@@ -4,5 +4,47 @@
"topic": "homeassistant", "topic": "homeassistant",
"devicename": "voltronic", "devicename": "voltronic",
"username": "", "username": "",
"password": "" "password": "",
"influx": {
"host": "http://[INFLUX_IP_OR_URL]:8086",
"username": "",
"password": "",
"device": "voltronic",
"prefix": "solar",
"database": "solar",
"namingMap": {
"Inverter_mode": "output_mode",
"AC_grid_voltage": "grid_voltage",
"AC_grid_frequency": "grid_frecuency",
"AC_out_voltage": "ac_output_voltage",
"AC_out_frequency": "ac_output_frecuency",
"PV_in_voltage": "pv_input_voltage_1",
"PV_in_current": "pv_input_current_for_battery",
"PV_in_watts": "pv_input_watts",
"PV_in_watthour": "pv_input_watthour",
"SCC_voltage": "battery_voltage_from_scc",
"Load_pct": "output_load_percent",
"Load_watt": "output_load_watt",
"Load_watthour": "outpu_load_watthour",
"Load_va": "ac_output_power_va",
"Bus_voltage": "bus_voltage",
"Heatsink_temperature": "inverter_heat_sink_temperature",
"Battery_capacity": "battery_capacity",
"Battery_voltage": "battery_voltage",
"Battery_charge_current": "battery_charging_current",
"Battery_discharge_current": "battery_discharge_current",
"Load_status_on": "load_status_on",
"SCC_charge_on": "scc_charge_on",
"AC_charge_on": "ac_charge_on",
"Battery_recharge_voltage": "battery_recharge_voltage",
"Battery_under_voltage": "battery_under_voltage",
"Battery_bulk_voltage": "battery_bulk_voltage",
"Battery_float_voltage": "battery_float_voltage",
"Max_grid_charge_current": "max_grid_charge_current",
"Max_charge_current": "max_charge_current",
"Out_source_priority": "output_source_priority",
"Charger_source_priority": "charger_source_priority",
"Battery_redischarge_voltage": "battery_re_discharge_voltage"
}
}
} }
+22 -1
View File
@@ -1,7 +1,12 @@
#!/bin/bash #!/bin/bash
INFLUX_HOST=`cat /etc/inverter/mqtt.json | jq '.influx.host' -r`
if [[ -n $INFLUX_HOST ]]; then
INFLUX_ENABLED=true
else
INFLUX_ENABLED=false
fi
pushMQTTData () { pushMQTTData () {
MQTT_SERVER=`cat /etc/inverter/mqtt.json | jq '.server' -r` MQTT_SERVER=`cat /etc/inverter/mqtt.json | jq '.server' -r`
MQTT_PORT=`cat /etc/inverter/mqtt.json | jq '.port' -r` MQTT_PORT=`cat /etc/inverter/mqtt.json | jq '.port' -r`
MQTT_TOPIC=`cat /etc/inverter/mqtt.json | jq '.topic' -r` MQTT_TOPIC=`cat /etc/inverter/mqtt.json | jq '.topic' -r`
@@ -16,6 +21,22 @@ pushMQTTData () {
-P "$MQTT_PASSWORD" \ -P "$MQTT_PASSWORD" \
-t "$MQTT_TOPIC/sensor/"$MQTT_DEVICENAME"_$1" \ -t "$MQTT_TOPIC/sensor/"$MQTT_DEVICENAME"_$1" \
-m "$2" -m "$2"
if "$INFLUX_ENABLED"; then
pushInfluxData $1 $2
fi
}
pushInfluxData () {
INFLUX_HOST=`cat /etc/inverter/mqtt.json | jq '.influx.host' -r`
INFLUX_USERNAME=`cat /etc/inverter/mqtt.json | jq '.influx.username' -r`
INFLUX_PASSWORD=`cat /etc/inverter/mqtt.json | jq '.influx.password' -r`
INFLUX_DEVICE=`cat /etc/inverter/mqtt.json | jq '.influx.device' -r`
INFLUX_PREFIX=`cat /etc/inverter/mqtt.json | jq '.influx.prefix' -r`
INFLUX_DATABASE=`cat /etc/inverter/mqtt.json | jq '.influx.database' -r`
INFLUX_MEASUREMENT_NAME=`cat /etc/inverter/mqtt.json | jq '.influx.namingMap.'$1'' -r`
curl -i -XPOST "$INFLUX_HOST/write?db=$INFLUX_DATABASE&precision=s" -u "$INFLUX_USERNAME:$INFLUX_PASSWORD" --data-binary "$INFLUX_PREFIX,device=$INFLUX_DEVICE $INFLUX_MEASUREMENT_NAME=$2"
} }
INVERTER_DATA=`timeout 10 /opt/inverter-cli/bin/inverter_poller -1` INVERTER_DATA=`timeout 10 /opt/inverter-cli/bin/inverter_poller -1`