Last active
July 7, 2022 19:27
-
-
Save h3xagn/49f5d2665a6706452d428fdadfb23bfa to your computer and use it in GitHub Desktop.
Give your cleverHome one voice: https://h3xagn.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated for blog