Python Forum
Needing to Check Every Quarter Hour
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Needing to Check Every Quarter Hour
#6
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.

""" 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 >>>
Reply


Messages In This Thread
Needing to Check Every Quarter Hour - by bill_z - Feb-08-2022, 04:30 PM
RE: Needing to Check Every Quarter Hour - by bill_z - Feb-08-2022, 05:01 PM
RE: Needing to Check Every Quarter Hour - by bill_z - Feb-08-2022, 07:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  needing some help to write some code for a game calculator rymdaksel 1 530 Jan-02-2024, 09:56 AM
Last Post: deanhystad
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,615 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Struggling for the past hour to define function and call it back godlyredwall 2 2,315 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  Noob needing guidance.... bako 0 1,904 Mar-29-2020, 06:55 PM
Last Post: bako
  Global Variables - Some Points Needing Clarification. adt 4 3,049 Nov-30-2019, 01:23 PM
Last Post: adt
  Beginner needing advice with data files JFI2019 2 2,279 Nov-06-2019, 04:56 PM
Last Post: JFI2019
  Typical beginner needing some help foxter00 1 2,736 May-08-2019, 11:46 PM
Last Post: michalmonday
  Different prices per hour kemper 1 2,092 Jan-29-2019, 11:06 AM
Last Post: Larz60+
  New user/beginner needing help oldDog 3 3,308 Apr-17-2018, 02:31 PM
Last Post: oldDog
  Beginner needing help! Franco 2 3,315 Jul-29-2017, 12:56 PM
Last Post: Franco

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020