Skip to content

Instantly share code, notes, and snippets.

@h3xagn
Last active Jul 7, 2022
Embed
What would you like to do?
Give your cleverHome one voice: https://h3xagn.com
def SendTelegramMessage(message):
"""Send a Telegram message to the group"""
try:
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
payload = {"chat_id": f"{chat_id}", "text": message, "parse_mode": "MarkdownV2"}
requests.post(url, data=payload)
except Exception as e:
print(f"Error sending Telegram message: {e}")
# while loop to get power measurements and determine if the power state has changed every 60 seconds
while True:
# append the measurement to the queue
measurement = GetPowerMeasurement()
if measurement is not None:
measurements.appendleft(measurement)
# if we have 3 measurements, check if the power state has changed
if len(measurements) == 3:
if all(measurement == 0 for measurement in measurements):
if mains_power_state:
SendTelegramMessage("Mains power is OFF")
mains_power_state = False
if all(measurement > 0 for measurement in measurements):
if not mains_power_state:
SendTelegramMessage("Mains power is ON")
mains_power_state = True
# use first two readings to confirm the current power state
elif len(measurements) == 2:
if all(measurement == 0 for measurement in measurements):
mains_power_state = False
if all(measurement > 0 for measurement in measurements):
mains_power_state = True
time.sleep(60)
@h3xagn
Copy link
Author

h3xagn commented Jul 7, 2022

updated for blog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment