From 313c98c119749cb1a6db5598b897e51e263ffd21 Mon Sep 17 00:00:00 2001 From: Martin Valov Date: Sun, 8 Nov 2020 22:38:50 +0100 Subject: [PATCH] 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. --- config/mqtt.json | 44 +++++++++++++++++++++++++++++- sources/inverter-mqtt/mqtt-push.sh | 23 +++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/config/mqtt.json b/config/mqtt.json index 6d7fa04..787e533 100644 --- a/config/mqtt.json +++ b/config/mqtt.json @@ -4,5 +4,47 @@ "topic": "homeassistant", "devicename": "voltronic", "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" + } + } } \ No newline at end of file diff --git a/sources/inverter-mqtt/mqtt-push.sh b/sources/inverter-mqtt/mqtt-push.sh index 5b28cae..cf8936c 100755 --- a/sources/inverter-mqtt/mqtt-push.sh +++ b/sources/inverter-mqtt/mqtt-push.sh @@ -1,7 +1,12 @@ #!/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 () { - MQTT_SERVER=`cat /etc/inverter/mqtt.json | jq '.server' -r` MQTT_PORT=`cat /etc/inverter/mqtt.json | jq '.port' -r` MQTT_TOPIC=`cat /etc/inverter/mqtt.json | jq '.topic' -r` @@ -16,6 +21,22 @@ pushMQTTData () { -P "$MQTT_PASSWORD" \ -t "$MQTT_TOPIC/sensor/"$MQTT_DEVICENAME"_$1" \ -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`