# HG changeset patch # User Dennis Fink # Date 2020-02-10 21:11:05 # Node ID 008a986903ea50c9d3c44b0e9a1813f38471f895 # Parent d35c626db59dd274916f2d6094cb32034826a02e Added co2 emission handling diff --git a/mqtt2prometheus.py b/mqtt2prometheus.py --- a/mqtt2prometheus.py +++ b/mqtt2prometheus.py @@ -40,6 +40,10 @@ power_consumption = Gauge( "power_consumption_watt", "The current power used", labelnames=["location"] ) +co2_emission = Gauge( + "co2_emission_kilogramm", "The CO2 emission in kilogramm", labelnames=["location"] +) + def on_connect(client, userdata, flags, rc): client.publish("availability/mqtt2prometheus", "online", 2, True) @@ -51,6 +55,7 @@ def on_connect(client, userdata, flags, 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.message_callback_add("sensors/co2_emission/#", handle_co2_emission) client.subscribe("sensors/people_now_present") client.subscribe("space/status") @@ -61,6 +66,7 @@ def on_connect(client, userdata, flags, client.subscribe("sensors/humidity/#") client.subscribe("sensors/barometer/#") client.subscribe("sensors/power_consumption/#") + client.subscribe("sensors/co2_emission/#") def handle_people_now_present(client, userdata, msg): @@ -112,6 +118,12 @@ def handle_power_consumption(client, use power_consumption.labels(location).set(float(msg.payload)) +def handle_co2_emission(client, userdata, msg): + t = msg.topic.split("/") + location = "-".join(t[2:]) + co2_emission.labels(location).set(float(msg.payload)) + + def main(): client = mqtt.Client("mqtt2prometheus")