Feb-08-2022, 07:19 PM
Thanks a bunch menator01.
With your suggestion, I was able to get my code to work.
I'm betting there are more slick and eloquent ways to do this, but, this is such a small program and the only one I plan on running on the RPi.
I'll run another test tonight after sunset to double check but I'm pretty confident it will work.
With your suggestion, I was able to get my code to work.
I'm betting there are more slick and eloquent ways to do this, but, this is such a small program and the only one I plan on running on the RPi.
I'll run another test tonight after sunset to double check but I'm pretty confident it will work.
""" Turn on LED every quarter hour where LED brightness is brighter between sunrise and sunset """ import requests from datetime import datetime,timedelta from zoneinfo import ZoneInfo from array import * latitude = 28.262222 longitude = -96.747165 target_timezone = "America/Chicago" timezone_offset = ZoneInfo(target_timezone).utcoffset(datetime.now()) quarter_hour = array ('d',[00,15,30,45]) day_brightnes = 255 night_brightnes = 125 url = "https://api.sunrise-sunset.org/json" params = { "lat": latitude, "lng": longitude, "formatted": 0, "date": "today", } input_format = "%Y-%m-%dT%H:%M:%S+00:00" output_format = "%H:%M:%S" resp_json = requests.get(url, params=params).json() sunrise_date_time = resp_json["results"]["sunrise"] sunset_date_time = resp_json["results"]["sunset"] sunrise = datetime.strptime(sunrise_date_time, input_format) local_sunrise = sunrise + timezone_offset sunrise = local_sunrise.strftime(output_format) sunset = datetime.strptime(sunset_date_time, input_format) local_sunset = sunset + timezone_offset sunset = local_sunset.strftime(output_format) local_datetime = datetime.now() local_time = local_datetime.strftime(output_format) print(sunrise, " ", local_time, " ", sunset) LED = night_brightnes if (sunrise < local_time < sunset): LED = day_brightnes print(f"LED brightness is {LED}") print(f"Current time is {datetime.now()} local") date_time = datetime.now() time = date_time.time() min = int(time.minute) print(f"Current minute is {min} local") if min in quarter_hour: print(f"It is a quarter hour {min} local")
Output:>>> %Run check_quarter_hour.py
07:08:37 13:15:05 18:13:40
LED brightness is 255
Current time is 2022-02-08 13:15:05.893387 local
Current minute is 15 local
It is a quarter hour 15 local
>>>