# HG changeset patch # User Dennis Fink # Date 2020-02-10 21:09:27 # Node ID d35c626db59dd274916f2d6094cb32034826a02e # Parent bfe1467514b9917ee543455f627f34a1f0fe92dd Added power consumption handling diff --git a/mqtt2prometheus.py b/mqtt2prometheus.py --- a/mqtt2prometheus.py +++ b/mqtt2prometheus.py @@ -36,6 +36,9 @@ barometer = Gauge( "barometer_hectopascal", "The current air pressure", labelnames=["location"] ) +power_consumption = Gauge( + "power_consumption_watt", "The current power used", labelnames=["location"] +) def on_connect(client, userdata, flags, rc): client.publish("availability/mqtt2prometheus", "online", 2, True) @@ -47,6 +50,7 @@ def on_connect(client, userdata, flags, client.message_callback_add("sensors/temperature/#", handle_temperature) client.message_callback_add("sensors/humidity/#", handle_humidity) client.message_callback_add("sensors/barometer/#", handle_barometer) + client.message_callback_add("sensors/power_consumption/#", handle_power_consumption) client.subscribe("sensors/people_now_present") client.subscribe("space/status") @@ -56,6 +60,7 @@ def on_connect(client, userdata, flags, client.subscribe("sensors/temperature/#") client.subscribe("sensors/humidity/#") client.subscribe("sensors/barometer/#") + client.subscribe("sensors/power_consumption/#") def handle_people_now_present(client, userdata, msg): @@ -101,6 +106,12 @@ def handle_barometer(client, userdata, m barometer.labels(location).set(float(msg.payload)) +def handle_power_consumption(client, userdata, msg): + t = msg.topic.split("/") + location = "-".join(t[2:]) + power_consumption.labels(location).set(float(msg.payload)) + + def main(): client = mqtt.Client("mqtt2prometheus")