Changeset - d35c626db59d
[Not reviewed]
default
0 1 0
Dennis Fink - 5 years ago 2020-02-10 21:09:27
dennis.fink@c3l.lu
Added power consumption handling
1 file changed with 11 insertions and 0 deletions:
0 comments (0 inline, 0 general)
mqtt2prometheus.py
Show inline comments
 
@@ -33,32 +33,37 @@ temperature = Gauge(
 
humidity = Gauge("humidity_percent", "The current humidity", labelnames=["location"])
 

	
 
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)
 

	
 
    client.message_callback_add("sensors/people_now_present", handle_people_now_present)
 
    client.message_callback_add("space/status", handle_space_status)
 
    client.message_callback_add("space/member_count", handle_member_count)
 
    client.message_callback_add("availability/#", handle_availability)
 
    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")
 
    client.subscribe("space/member_count")
 
    client.subscribe("availability/+")
 
    client.subscribe("availability/shutdown/+")
 
    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):
 
    people_now_present.set(json.loads(msg.payload))
 

	
 

	
 
@@ -98,12 +103,18 @@ def handle_humidity(client, userdata, ms
 
def handle_barometer(client, userdata, msg):
 
    t = msg.topic.split("/")
 
    location = "-".join(t[2:])
 
    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")
 
    client.on_connect = on_connect
 
    client.username_pw_set(CONFIG["mqtt"]["username"], CONFIG["mqtt"]["password"])
 
    client.will_set("availability/mqtt2prometheus", "offline", 2, True)
0 comments (0 inline, 0 general)