tidbyt / community

A publishing platform for apps developed by the Tidbyt community 🚀
Apache License 2.0
379 stars 643 forks source link

Expose "Day/Night" magic variables #40

Closed joeyhoer closed 6 months ago

joeyhoer commented 2 years ago

Some app developers may want to provide a different theme/content at night. Currently app developers must utilize an API to determine daytime/nighttime based on sunset/sunrise times; however it seems that Tidbyt must already be determining this globally for the "dim at sunset" configuration.

Current approach:

resp = http.get("https://api.sunrise-sunset.org/json?lat=%f&lng=%f" % (loc.get("lat"), loc.get("lng")))
data = resp.body()
json_data = json.decode(data)
current_time=time.parse_time(now.format('3:04:05 PM'), format="3:04:05 PM", location=timezone)
sunrise=time.parse_time(json_data['results']['sunrise'], format="3:04:05 PM")
sunset=time.parse_time(json_data['results']['sunset'], format="3:04:05 PM")
color = color_nighttime
if current_time > sunrise and current_time < sunset:
    color = color_daytime

Exposing a "magic variable" (similar to $tz for timezone) could simplify this significantly and reduce API requests.

Suggested approach:

color = color_nighttime
if $is_daytime:
    color = color_daytime
rohansingh commented 2 years ago

We use go-sunrise in the Tidbyt backend to power the "dim before sunset" feature. It has the huge advantage of not requiring any network calls.

Here's an example of the Go usage:

rise, set := sunrise.SunriseSunset(
    43.65, -79.38,          // Toronto, CA
    2000, time.January, 1,  // 2000-01-01
)

Perhaps we could add Starlark bindings for it and then we wouldn't need any special magic variable.

henryso commented 2 years ago

Perhaps we could add Starlark bindings for it and then we wouldn't need any special magic variable.

That sounds like a really good idea. Calling (and caching) api.sunrise-sunset.org for every potential latitude/longitude of Tidbyt users will probably lead to nightmares.

dinosaursrarr commented 2 years ago

This was fixed by https://github.com/tidbyt/pixlet/pull/124, right?

joeyhoer commented 2 years ago

As a related request, I'm noticing that an abrupt change when "day" turns to "night" (and vice versa) in somewhat jarring. This could be resolved in the app by calculating some type of gradual transition; however, it could also be helpful to have variables for the different types of twilight (Civil twilight, Nautical twilight, and Astronomical twilight). This would provide app authors with the ability to slightly change colors to more accurately match ambient lighting without an physical light sensor connected to the Tidbyt (which would be a really cool hardware enhancement).

dinosaursrarr commented 2 years ago

@joeyhoer -- I have https://github.com/tidbyt/pixlet/pull/381 out at the moment which would let you do the maths. You could use sunrise.elevation_time to compute when the sun is at a certain angle below the horizon.

Or use the sunrise.elevation function to determine whether it's currently day, civil, nautical, astronomical, or night for a given location.