Rapporteert nu voortaan ook glucose en glucose alarm aan de MQTT broker

This commit is contained in:
GJ Koolen 2026-02-28 00:08:23 +01:00
parent b9bdc0783f
commit 33d3473895

View file

@ -10,7 +10,7 @@ from collections import deque
broker_address = "192.168.1.160" broker_address = "192.168.1.160"
broker_port = 1883 broker_port = 1883
recieve_topic = "telegrambot/in/glucose/gj" recieve_topic = "telegrambot/in/glucose/gj"
send_topic = "telegrambot/out/gj" telegram_topic = "telegrambot/out/gj"
brokercreds = ["hass","Bruk#5"] brokercreds = ["hass","Bruk#5"]
##### Variables ##### ##### Variables #####
@ -58,6 +58,8 @@ GLUC_SLOPE_PERIOD = 30 # in minutes
GLUC_BUFFER_LENGTH = round(GLUC_SLOPE_PERIOD/GLUC_READOUT_PERIOD) GLUC_BUFFER_LENGTH = round(GLUC_SLOPE_PERIOD/GLUC_READOUT_PERIOD)
gluc_buffer = deque([6.0] * GLUC_BUFFER_LENGTH, maxlen=GLUC_BUFFER_LENGTH) # The gluc_buffer always contains only the last xx elements. Initialized on 6.0 gluc_buffer = deque([6.0] * GLUC_BUFFER_LENGTH, maxlen=GLUC_BUFFER_LENGTH) # The gluc_buffer always contains only the last xx elements. Initialized on 6.0
glucose_alert_is_active = False
##### Main ##### ##### Main #####
def glucose_value(data_glucose): #Inputs json string and unbinds glucose value def glucose_value(data_glucose): #Inputs json string and unbinds glucose value
@ -69,6 +71,7 @@ def glucose_value(data_glucose): #Inputs json string and unbinds glucose valu
def main(url_main, headers_main): def main(url_main, headers_main):
global last_bolus_time global last_bolus_time
global last_bolus_type global last_bolus_type
global glucose_alert_is_active
while True: while True:
try: try:
# Send the Get request # Send the Get request
@ -80,6 +83,8 @@ def main(url_main, headers_main):
gluc_value = glucose_value(data) gluc_value = glucose_value(data)
print(f"current level: {gluc_value}") print(f"current level: {gluc_value}")
Mqtt.publish("glucose/glucose_level", f"{gluc_value}") # inform Home Assistant of new glucose level
# let's determine the slope of the last 30 minutes # let's determine the slope of the last 30 minutes
gluc_buffer.append(gluc_value) gluc_buffer.append(gluc_value)
old_value = list(gluc_buffer)[0] old_value = list(gluc_buffer)[0]
@ -100,7 +105,14 @@ def main(url_main, headers_main):
if ((gluc_value > gluc_upper_limit) or if ((gluc_value > gluc_upper_limit) or
(gluc_slope > limits_table[last_bolus_type][int(hours_since_last_bolus)][1])): (gluc_slope > limits_table[last_bolus_type][int(hours_since_last_bolus)][1])):
print("Hey joh check je glucosepeil !") print("Hey joh check je glucosepeil !")
Mqtt.publish(send_topic, f"Hey joh check peil ! (lastbolus -{round(hours_since_last_bolus, 1)})") Mqtt.publish(telegram_topic, f"Hey joh check peil ! (lastbolus -{round(hours_since_last_bolus, 1)})")
if not glucose_alert_is_active:
Mqtt.publish("glucose/glucose_alarm_state", "1") # we just entered alarm state
glucose_alert_is_active = True
else:
if glucose_alert_is_active:
Mqtt.publish("glucose/glucose_alarm_state", "0") # we exit alarm state
glucose_alert_is_active = False
else: # Request has failed else: # Request has failed
print(f"❌ Error: Could not connect to server: {response.status_code}") print(f"❌ Error: Could not connect to server: {response.status_code}")
@ -135,7 +147,7 @@ def on_message(client, userdata, msg): #This runs everytime a mqtt message
last_bolus_time = time.time() - 3600 * float(args_list[1]) last_bolus_time = time.time() - 3600 * float(args_list[1])
last_bolus_type = 0 last_bolus_type = 0
except Exception as e: except Exception as e:
Mqtt.publish(send_topic, f"Error: {e}") Mqtt.publish(telegram_topic, f"Error: {e}")
else: else:
last_bolus_type = 0 last_bolus_type = 0
last_bolus_time = time.time() last_bolus_time = time.time()
@ -145,7 +157,7 @@ def on_message(client, userdata, msg): #This runs everytime a mqtt message
last_bolus_time = time.time() - 3600 * float(args_list[1]) last_bolus_time = time.time() - 3600 * float(args_list[1])
last_bolus_type = 1 last_bolus_type = 1
except Exception as e: except Exception as e:
Mqtt.publish(send_topic, f"Error: {e}") Mqtt.publish(telegram_topic, f"Error: {e}")
else: else:
last_bolus_type = 1 last_bolus_type = 1
last_bolus_time = time.time() last_bolus_time = time.time()
@ -161,6 +173,7 @@ Mqtt.connect(broker_address, broker_port, 60) #Set Connection
Mqtt.loop_start() #Start connecting Mqtt.loop_start() #Start connecting
Mqtt.publish(send_topic, "Started Glucose Checker :-)") Mqtt.publish(telegram_topic, "Started Glucose Checker :-)")
Mqtt.publish("glucose/glucose_alarm_state", "0")
if __name__ == "__main__": if __name__ == "__main__":
main(url, headers) main(url, headers)