Sun-Relative Time Events Using Python
For my current X10 home automation project, I want the ability to schedule events to occur at sun-relative times like 'sunrise' and 'sunset'. Now, you can do that by using an X10 light sensor and watching for the 'on' and 'off' commands it will send. But will the sensor be triggered by stray light, like from a passing car's headlights? I don't know. Perhaps with careful placement of the sensor, you could avoid accidental triggering. But there's another way to handle the problem.
Given your position on the world by latitude and longitude and today's date, sunrise and sunset can be calculated accurately using spherical geometry. It's what astromomers do. You can google around for existing software and source code that does these calculations. I wanted a Python solution, and so I settled for Sun.py by Henrik Härkönen. This is code for a Python class that calculates sunrise and sunset, as well as three flavors of 'twilight'. The code is a direct translation of someone else's C code, even to the point of preserving the original C comments, but hey, it works well. It calculates all times in UTC, so you'll need to apply the correct offset for your timezone.
With this code, I can now translate an X10 event scheduled for sunrise today into the actual sunrise time for today. That's one more item on my wish-list for X10 home automation I can check off.
11:31:38 AM
|
|