I'm currently working on a project that notifies the users when certain activities related to light are triggered. I've done the part related to light. Hence, I need to find an effective way to retrieve sunrise and sunset time in python, since the whole script is written in python. I know there are several libraries out there for other languages, I wonder what the most convenient way is to do this in python.
It will seem pretty much like this, I suppose:
if(sunrise<T<sunset) and (light<threshold):
notifyUser()
I'd appreciate any help here, have a good one.
This Sunrise and Sunset API will return interesting data about the day like: sunset, sunrise, day length, nautical twilight, civil twilight and astronomical twilight. One thing to be careful about is times will be GMT so depending on the location you’re looking up you might want to adjust the times returned.
Sunset/Sunrise time is almost the same every year on a particular day. So you can first try to build your clock with static data then later when it's working you can switch to real time google data.
This means sunrise would be 0.00% and sunset would be 100%. I wanted to create a easier version of this, having a program Google the sunset and sunrise times for the day and work from there.
I'm using Sun.py. Today I got the value of minutes = 60, UT = 12.9979740551 #10. Return min = round ( (UT - int (UT))*60,0) #add this after min calculate if min == 60: hr += 1 min = 0
Check out astral. Here's a slightly modified example from their docs:
>>> from astral import Astral
>>> city_name = 'London'
>>> a = Astral()
>>> a.solar_depression = 'civil'
>>> city = a[city_name]
>>> sun = city.sun(date=datetime.date(2009, 4, 22), local=True)
>>> if (sun['sunrise'] < T < sun['sunset']) and (light < threshold):
>>> notifyUser()
If you use something like this example, please remember to change the city_name
and date provided to city.sun
.
You can also try with suntime. An example from their doc below:
import datetime
from suntime import Sun, SunTimeException
latitude = 51.21
longitude = 21.01
sun = Sun(latitude, longitude)
# Get today's sunrise and sunset in UTC
today_sr = sun.get_sunrise_time()
today_ss = sun.get_sunset_time()
print('Today at Warsaw the sun raised at {} and get down at {} UTC'.
format(today_sr.strftime('%H:%M'), today_ss.strftime('%H:%M')))
# On a special date in your machine's local time zone
abd = datetime.date(2014, 10, 3)
abd_sr = sun.get_local_sunrise_time(abd)
abd_ss = sun.get_local_sunset_time(abd)
print('On {} the sun at Warsaw raised at {} and get down at {}.'.
format(abd, abd_sr.strftime('%H:%M'), abd_ss.strftime('%H:%M')))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With