diff --git a/mqtt2prometheus.py b/mqtt2prometheus.py --- a/mqtt2prometheus.py +++ b/mqtt2prometheus.py @@ -44,6 +44,13 @@ co2_emission = Gauge( "co2_emission_kilogramm", "The CO2 emission in kilogramm", labelnames=["location"] ) +energy_consumption = Gauge( + "energy_consumption_kilowatthour", + "The energy consumption in kilowatthour", + labelnames=["location"], +) + + def on_connect(client, userdata, flags, rc): client.publish("availability/mqtt2prometheus", "online", 2, True) @@ -56,6 +63,9 @@ def on_connect(client, userdata, flags, 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.message_callback_add( + "sensors/energy_consumption/#", handle_energy_consumption + ) client.subscribe("sensors/people_now_present") client.subscribe("space/status") @@ -67,6 +77,7 @@ def on_connect(client, userdata, flags, client.subscribe("sensors/barometer/#") client.subscribe("sensors/power_consumption/#") client.subscribe("sensors/co2_emission/#") + client.subscribe("sensors/energy_consumption/#") def handle_people_now_present(client, userdata, msg): @@ -124,6 +135,12 @@ def handle_co2_emission(client, userdata co2_emission.labels(location).set(float(msg.payload)) +def handle_energy_consumption(client, userdata, msg): + t = msg.topic.split("/") + location = "-".join(t[2:]) + energy_consumption.labels(location).set(float(msg.payload)) + + def main(): client = mqtt.Client("mqtt2prometheus")