The old X10 devices were getting really unreliable: seldom firing at all, getting far too hot, bringing a whole lot of not working to my life. So while it was kind of cool to have my lights controlled by an original 256 MB Raspberry Pi Model B from 2012, it was maybe working one schedule out of ten.
So it had to go: replaced by a Raspberry Pi Zero W and a whole lot of IKEA TRÃ…DFRI kit. I was deeply unimpressed with the IKEA Home smart app, though: you couldn’t use even basic schedules with more than one light cycle per day. So while I know there are lots of clever home automation systems, I wanted to replace my old cron scripts and set about writing some simple command tools. The result is ihsctrl: very limited, but good enough for me. It’s been working exactly as expected for the last week, so I’ll finally get to wade through 8 years of cobwebs and dismantle the old X10 setup. I already miss the 06:30 clonk of the X10 controller turning the front light on â€” that was my alarm clock (or alarm clonk) every morning.
Yep, springtime’s coming, and today’s the first day I know it, despite the -5.8Â°C outside. I know spring is coming because my sunrise-adjusted lights came on before my alarm today. I’m controlling them with a Raspberry Pi, cron, and X10.
I’d described how to build and use heyu previously, so I won’t go into it further. I use sunwait to control the timing relative to local sunrise and sunset. Sunwait is a simple C program which builds quickly, and you can put the executable somewhere in your path.
(NB: newer versions of sunwait use a completely incompatible command line format. Everything here refers to the 2004 version I linked to above, which does exactly what I need in the way it’s described here.)
You need to know your latitude and longitude to use sunwait. To check its setting for the day, you can call it with the -p option:
$ sunwait -p 43.729N 79.292W
Using location:Â Â Â Â Â Â Â Â Â Â Â Â 43.729000N, 79.292000W
Date:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 6 Feb 2013
Local time:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 7:44
Day length:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 10:13 hours
With civil twilightÂ Â Â Â Â Â Â Â 11:10 hours
With nautical twilightÂ Â Â Â Â 12:18 hours
With astronomical twilightÂ 13:25 hours
Length of twilight:Â civilÂ Â 0:28 hours
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â nauticalÂ Â 1:02 hours
Â Â Â Â Â Â Â Â Â Â Â Â Â astronomicalÂ Â 1:35 hours
Current specified time zone: EST (-5 from UTC)
Sun transits meridian 1231 EST
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Sun rises 0726 EST, sets 1736 EST
Â Â Â Â Â Â Civil twilight starts 0656 EST, ends 1806 EST
Â Â Â Nautical twilight starts 0622 EST, ends 1840 EST
Astronomical twilight starts 0548 EST, ends 1913 EST
So for me, today’s sunrise is at 0726, and sunset is at 1736. All sunwait does is wait until a specific solar time is reached, and then exit. Whatever command you call after sunwait, therefore, is what gets run at the right time. So if I wanted X10 device H1 to come on an hour before sunrise, I’d run:
sunwait sun up -1:00:00 43.729N 79.292W; heyu on h1
Remembering to run this every day before sunrise would be a pain, so this is where cron helps. cron uses a slightly odd config file that is edited using the crontab -e command. Here’s the relevant bit of my crontab, showing the light control times:
# m hÂ dom mon dowÂ Â command
Â 01 00Â Â *Â Â *Â Â *Â Â /usr/local/bin/sunwait sun up -1:00:00 43.729N 79.292W; /usr/local/bin/heyu on h1
Â 02 00Â Â *Â Â *Â Â *Â Â /usr/local/bin/sunwait sun up +1:00:00 43.729N 79.292W; /usr/local/bin/heyu off h1
Â 03 00Â Â *Â Â *Â Â *Â Â /usr/local/bin/sunwait sun down -1:00:00 43.729N 79.292W; /usr/local/bin/heyu on h1
Â 45 22Â Â *Â Â *Â Â *Â Â /usr/local/bin/heyu off h1
(you can view your crontab with crontab -l)
The columns in crontab are:
day of month
day of week
So the four crontab lines mean:
Every day at 00:01, wait until an hour beforesunrise and turn light H1 on
Every day at 00:02, wait until an hour aftersunrise and turn light H1 off
Every day at 00:03, wait until an hour beforesunset and turn light H1 on
At 22:45, turn light H1 off.
So for quite a bit of the day, there are a couple of sunwait tasks just quietly waiting until sunrise or sunset to do their thing. cron, incidentally, is picky about executable paths; that’s why I specified full paths to both sunwait and heyu.
What I’d really like to do is have time on this machine update without a network connection, because it’s running from a particularly messy router set up in a spare bedroom. I should investigate a real-time clock, with GPS time updates from an IÂ²C GPS, talking through a bluetooth console. In my copious free time, of course.