Created
June 6, 2024 06:01
-
-
Save jnachtigall/8a5f521bb5e5ebf794bd7f8906085564 to your computer and use it in GitHub Desktop.
Adding a rain delay for OpenSprinkler
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
# PLEASE SEE https://kevinsaye.wordpress.com/2021/07/05/adding-a-rain-delay-for-opensprinkler/ | |
import requests | |
import sys | |
import time | |
import datetime | |
URL = "https://api.openweathermap.org/data/2.5/forecast?lat=YYY&lon=XXX&appid=SECRET" | |
delayURL = "https://MY_URL/cv?pw=SECRETTOO&rd=" | |
# check every 6 hours | |
interval = 21600 | |
# 7mm (7 Liter per qm in the next 24h) | |
rainTargetMMTotal = 7 | |
# We only want to disable *our* rain delay; not a manually added delay from the OSPI App | |
rainDelayFromScript = False | |
while True: | |
# TODO: Actually it would be worth checking by crawling the OSPI API whether there already is a (manual) delay that's longer than our 12h and only set a delay if there is none | |
# And then "continue" here if so | |
# But for the sake of simplicity let's ignore this for now | |
try: | |
weatherJSON = requests.get(URL).json() | |
rainMMTotal = 0 | |
rainData = "" | |
# next 0-7 => 8 x 3h forcast = next 24h rain forcast | |
for x in range(0,8): | |
if "rain" in weatherJSON["list"][x]: | |
# rain in mm * pop (Probability of precipitation in percent 0-1) | |
rainMMTotal += (weatherJSON["list"][x]["rain"]["3h"] * weatherJSON["list"][x]["pop"]) | |
rainData += str(weatherJSON["list"][x]["dt_txt"]) + "=" + str(weatherJSON["list"][x]["weather"][0]["main"]) + "," + str(weatherJSON["list"][x]["rain"]["3h"] * weatherJSON["list"][x]["pop"]) + ";" | |
else: | |
rainData += str(weatherJSON["list"][x]["dt_txt"]) + "=" + str(weatherJSON["list"][x]["weather"][0]["main"]) + ";" | |
if rainMMTotal >= rainTargetMMTotal: | |
print(str(datetime.datetime.now()) + " Rain Delay, data:" + rainData) | |
rainDelayFromScript = True | |
requests.get(delayURL + "12") | |
elif rainDelayFromScript: | |
print(str(datetime.datetime.now()) + " No Rain Delay, data:" + rainData) | |
rainDelayFromScript = False | |
requests.get(delayURL + "0") | |
except: | |
print(str(datetime.datetime.now()) + " Error: " + str(sys.exc_info())) | |
time.sleep(interval) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment