From e7114310f91bac3cc673cdb46278abf128b7fef0 Mon Sep 17 00:00:00 2001 From: jwansek Date: Fri, 17 Jan 2025 00:41:03 +0000 Subject: Changed MQTT topic naming scheme, added logging zigbee information --- mqtt-client/mqtt-client.py | 35 +++++++++++++++++++++++++++-------- switch-snmp/port-names.conf | 16 ++++++++++------ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/mqtt-client/mqtt-client.py b/mqtt-client/mqtt-client.py index c51f4b8..a441e6d 100644 --- a/mqtt-client/mqtt-client.py +++ b/mqtt-client/mqtt-client.py @@ -23,20 +23,39 @@ class MQTTClient: def _on_connect_cb(self, mqtt, userdata, flags, rc): #print("Connected to broker") - self.mqttc.subscribe("tele/+/SENSOR") + self.mqttc.subscribe("tele/+/+/SENSOR") def _on_message_cb(self, mqtt, userdata, msg): #print('Topic: {0} | Message: {1}'.format(msg.topic, msg.payload)) - if "Tasmota" in msg.topic: - self.handle_tasmota(msg) - - def handle_tasmota(self, msg): - from_ = msg.topic.split("/")[1] + # my MQTT naming scheme is tele/// + # e.g. + # tele/TasmotaPlug/TasmotaNAS/SENSOR + # tele/TasmotaZigbee/TasmotaZigbee/SENSOR (there is only one Tasmota Zigbee bridge) + type_ = msg.topic.split("/")[1] + location = msg.topic.split("/")[2] msg_j = json.loads(msg.payload.decode()) - print("'%s' is using %.1fw @ %s. %.1fkWh so far today, %.1fkWh yesterday" % (from_, msg_j["ENERGY"]["Power"], msg_j["Time"], msg_j["ENERGY"]["Today"], msg_j["ENERGY"]["Yesterday"])) + + if type_ == "TasmotaPlug": + self.handle_plug(msg_j, location) + elif type_ == "TasmotaZigbee": + self.handle_zigbee(msg_j) + + def handle_plug(self, msg_j, location): + print("'%s' is using %.1fw @ %s. %.1fkWh so far today, %.1fkWh yesterday" % (location, msg_j["ENERGY"]["Power"], msg_j["Time"], msg_j["ENERGY"]["Today"], msg_j["ENERGY"]["Yesterday"])) fields = {k: v for k, v in msg_j["ENERGY"].items() if k not in {"TotalStartTime"}} - points = [{"measurement": "tasmota_power", "tags": {"plug": from_}, "fields": fields}] + self.append_influxdb(fields, "tasmota_power", {"plug": location}) + + def handle_zigbee(self, msg_j): + zigbee_id = list(msg_j["ZbReceived"].keys())[0] + fields = msg_j["ZbReceived"][zigbee_id] + friendlyname = fields.pop("Name") + del fields["Device"] + print("Zigbee device '%s' reported: %s" % (friendlyname, str(fields))) + self.append_influxdb(fields, "zigbee", {"friendlyname": friendlyname, "id": zigbee_id}) + + def append_influxdb(self, fields, measurement_name, tags): + points = [{"measurement": measurement_name, "tags": tags, "fields": fields}] write_api = self.influxc.write_api(write_options = SYNCHRONOUS) write_api.write( os.environ["DOCKER_INFLUXDB_INIT_BUCKET"], diff --git a/switch-snmp/port-names.conf b/switch-snmp/port-names.conf index 67d1ad8..761804a 100644 --- a/switch-snmp/port-names.conf +++ b/switch-snmp/port-names.conf @@ -1,6 +1,10 @@ -1 = Routerbox -5 = PiKVM -6 = EAP225 WiFi -8 = Git Raspberry Pi -7 = Intel Compute Stick -13 = Monitor Raspberry Pi +1 = EAP225 Wifi +2 = PiKVM +4 = 2.5Gb Switch +6 = Routerbox +13 = Intel Compute Stick +24 = Frigate Pi +23 = Modem & ES205G +8 = PiKVM +10 = TL-RP108GE & EAP110 + -- cgit v1.2.3