Changeset - a0e920df8f3f
[Not reviewed]
default
0 1 0
Dennis Fink - 5 years ago 2020-01-27 20:07:18
dennis.fink@c3l.lu
Handle humidity
1 file changed with 8 insertions and 0 deletions:
0 comments (0 inline, 0 general)
mqtt2prometheus.py
Show inline comments
 
@@ -21,40 +21,43 @@ member_count = Gauge(
 
)
 

	
 
availability = Gauge(
 
    "availability",
 
    "The availability status of different MQTT clients",
 
    labelnames=["clientname"],
 
)
 

	
 
temperature = Gauge(
 
    "temperature_celsius", "The current temperature", labelnames=["location"]
 
)
 

	
 
humidity = Gauge("humidity_percent", "The current humidity", 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.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/#")
 

	
 

	
 
def handle_people_now_present(client, userdata, msg):
 
    people_now_present.set(json.loads(msg.payload))
 

	
 

	
 
def handle_space_status(client, userdata, msg):
 
    state = json.loads(msg.payload)
 
    space_state.set(int(state["open"]))
 
    space_lastchange.set(state["lastchange"])
 

	
 

	
 
@@ -70,24 +73,29 @@ def handle_availability(client, userdata
 
    else:
 
        c = "_".join(t[1:])
 
    v = 1 if msg.payload == b"online" else 0
 
    availability.labels(c).set(v)
 

	
 

	
 
def handle_temperature(client, userdata, msg):
 
    t = msg.topic.split("/")
 
    location = "-".join(t[2:])
 
    temperature.labels(location).set(float(msg.payload))
 

	
 

	
 
def handle_humidity(client, userdata, msg):
 
    t = msg.topic.split("/")
 
    location = "-".join(t[2:])
 
    humidity.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)
 

	
 
    client.connect(CONFIG["mqtt"]["host"])
 
    client.loop_start()
 
    start_http_server(CONFIG["prometheus"]["port"])
 
    while True:
 
        time.sleep(1)
0 comments (0 inline, 0 general)