Hallo zusammnen,
ich habe jetzt mal ein PythonScript erstellt, leider fehlen mir die Kenntnisse um es so zum laufen zu bringen wie ich es gern möchte.
Intension war es, die einzelnen channels auch in separate genau so benannte influxDB zu schreiben, momentan wir alles in die InfluxDB "ch4"
geschrieben.
Wie schon erwähnt, ich bin kein "Coder", wenn jemand eine Idee hat wäre es toll, das Script kann gern genutzt und verbessert werden.
*EDIT 10.04.23 /10:10 Uhr
Ich habe eine lange Nacht hinter und viel Kaffee in mir und noch mehr gelernt, vor allem, dass man nicht so kompliziert denken sollte.
Das folgende Script funktioniert so wie es soll.
Es werden alles Topics von der "AHoyDTU" in eine InfluxDB übertragen.
Folgende Topics: ch0, ch1, ch2, ch3, ch4, total
Und dennoch sind das leider nicht alle Werte( Leistungsanpassung) die AhoyDTU zur Verfügung stellt.
Ich bin jedoch zufrieden.
Code: Alles auswählen
#!/usr/bin/env python3
"""
A MQTT to InfluxDB Bridge for HM-1500 Inverter
This script receives MQTT data from HM-1500 Inverter and saves those to InfluxDB.
Scriptet by Fooose
"""
import re
import json
import time
from typing import NamedTuple
import paho.mqtt.client as mqtt
from influxdb import InfluxDBClient
INFLUXDB_ADDRESS = ''
INFLUXDB_USER = ''
INFLUXDB_PASSWORD = ''
MQTT_ADDRESS = ''
MQTT_USER = ''
MQTT_PASSWORD = ''
MQTT_TOPIC_REGEX = 'HM-1500/(total|HM-1500)/([^/]+)/([^/]+)'
class SensorData(NamedTuple):
location: str
measurement: str
value: float
def on_connect(client, userdata, flags, rc):
""" The callback for when the client receives a CONNACK response from the server."""
print('Connected with result code ' + str(rc))
# Subscribe to all the HM-1500 topics
client.subscribe('HM-1500/#')
def on_message(client, userdata, msg):
"""The callback for when a PUBLISH message is received from the server."""
print(msg.topic + ' ' + str(msg.payload))
sensor_data = _parse_mqtt_message(msg.topic, msg.payload.decode('utf-8'))
if sensor_data is not None:
_send_sensor_data_to_influxdb(sensor_data)
def _parse_mqtt_message(topic, payload):
match = re.match(MQTT_TOPIC_REGEX, topic)
if match:
location = match.group(2)
measurement = match.group(3)
if measurement == 'status':
return None
return SensorData(location, measurement, float(payload))
else:
return None
def _send_sensor_data_to_influxdb(sensor_data):
# Create a new InfluxDB client for each location
influxdb_client = InfluxDBClient(
host=INFLUXDB_ADDRESS,
port=8086,
username=INFLUXDB_USER,
password=INFLUXDB_PASSWORD,
database=sensor_data.location
)
json_body = [
{
'measurement': sensor_data.measurement,
'tags': {
'location': sensor_data.location
},
'fields': {
'value': sensor_data.value
}
}
]
influxdb_client.write_points(json_body)
def _init_influxdb_database():
pass # We don't need to do anything here
if __name__ == '__main__':
_init_influxdb_database()
mqtt_client = mqtt.Client(client_id='HM-1500-MQTT-InfluxDB-Bridge')
mqtt_client.username_pw_set(MQTT_USER, MQTT_PASSWORD)
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.connect(MQTT_ADDRESS, 1883, 60)
mqtt_client.loop_forever()