Trivial Thoughts
Thoughts and discussion on programming projects using the Python language.


Python Sites of Note
Software Development



Recent Posts
 6/15/04
 5/16/04
 5/13/04
 5/10/04
 5/8/04
 5/5/04
 5/5/04
 4/23/04
 9/23/03
 9/22/03
 9/12/03
 9/11/03
 8/21/03
 7/21/03
 7/17/03
 7/10/03
 7/7/03
 7/1/03
 6/26/03
 6/25/03
 6/18/03
 6/15/03
 6/2/03
 5/28/03


Subscribe to "Trivial Thoughts" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.
 

 

Thursday, May 13, 2004
 

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  comment []    


Click here to visit the Radio UserLand website. © Copyright 2004 Michael Kent.
Last update: 6/15/2004; 4:14:00 PM.
This theme is based on the SoundWaves (blue) Manila theme.
May 2004
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Apr   Jun

Previous/Next