A Little Home Automation
I’ve mentioned elsewhere that I have a lamp timer and was finding it frustrating to have to manually change the on/off times throughout the year as the length of the day changes. One option I suggested using a computer to to give more flexibility and control.
Well I’ve been running with a setup that does use a computer for a few months now and it seems to be working well, so well that it’s controlling my Christmas lights too. In the end I didn’t wait for a Raspberry Pi (I already have a small Linux server that’s on 24/7, so just used that instead).
In my other article I posited using something like X10 to control it, but X10 plugs and controllers are fairly expensive. I had a set of three remote controlled plugs already and a bit of searching found that they could be controlled remotely. I got the set for free, but you can pick up similar ones in supermarkets and DIY places from as little as £5. Just be sure they adhere to one of the standards (such as Bye Bye Standby or Home Easy, which seem to be the most common in the UK). To send on and off signals I settled on a simple USB stick from a Swedish company to do just that.
I opted for the Telldus TellStick, which isn’t cheap (though it’s the cheapest of their range), but it had good support on multiple platforms (including Linux) and saved me soldering or whatnot. It cost me £54.60 (inc VAT and postage) from Audon Electronics.
The unit is underwhelmingly small and looked a little disappointing when I opened the pack, but I plugged it in and followed the instructions to download and install the application and within minutes I was able to switch a socket on and off through the control centre software.
I was thinking of using the automation as an exercise to play about with Python, but I know PHP better and there seemed a fairly simple way to get that to work (plus I already had everything installed so could get a web front-end working no problem).
A few more minutes and I had a rough web page with some links I could click to trigger the socket. A static web page was never going to be enough for me though, so after a few quick scribblings to get my head around the design I knocked up a database to store the device details and some on/off timer settings.
Then I wrote a couple of simple functions to poll the timers’ table for any that were due, checked the current status of the device and switched it on or off accordingly. I set up a script to call these every minute using a cron job and voila I had a working timer.
A few more lines of code and I’d added functionality to support weekday, weekend and everyday timers so I could limit events appropriately.
The timer is controlled by some simple command-line calls to the script but I also built a web page to allow me to control the devices manually (and see the status) should I wish (for turning a lamp on or off whenever I wanted). I could use the supplied remote, but the benefit of the web page is that it updates the status of the device (these are purely one-way, you don’t get feedback as to whether it’s on or off). It also means I can control the devices from anything with a web browser (my phone, tablet, desktop, etc). I could even expose the page through my router so I could access it from anywhere I have an internet connection to check the status and switch my devices on or off.
There are plenty of home automation packages out there, many of them free, which do a lot more than what I’ve built so far. I could have used one them but they seemed like overkill for such a simple setup, plus some of them just looked clunky. It also gave me a chance to tinker and I’d have needed to modify them to get the functionality I wanted anyway.
One of my goals was to stop having to make adjustments as sunrise and sunset moved. A quick hunt around led me to the sunrise and sunset times over at HM Nautical Almanac Office. They weren’t in a great format, but I managed to download them and reformatted them in Excel so I could load them into the database via CSV. That left me with a table filled with times through to the end of 2013.
A simple script is run by a cron job each night to update the on time of the relevant timers with sunset from the table of saved values. I also modified the on function to take account of sunrise and only trigger the lamp to come on if sunrise happens after the set time (meaning it’s dark).
Subsequently I’ve found that I didn’t need to store all the times as PHP has a function to get sunrise and sunset times. I’ll modify my script at some point to switch over to this method as it’s more sustainable.
I’ve solved my immediate problem and the timers are now more aware of the hours of daylight, but I’ve already found a few areas that could offer improvement:
- Make the timers aware of bank holidays and support option to disable them.At the moment the timers treat each day the same, but if I’m not getting up for work then some don’t really need to come on. I need to figure out whether these can be calculated on the fly or whether I’ll need to tell the script manually when they fall.
- Make timers aware of when I’m on holiday/off work and support option to disable them.Again, if I’m not getting up then (in my case) the morning timer is redundant. I could link this to a web calendar (similar to what someone’s done here) to keep up to date while avoiding duplication.
- Take weather conditions into account.Obviously the light conditions are significantly affected by the weather, a sunny day will offer usable light much longer than a cloudy one.
- Support single day and specific day timers.By that I mean timers for a specific date (with no repeat) or day of the week (i.e. every Tuesday).
- Improve web interface to support adding and modification of timers and devices.It’s a bit rough-and-ready at the moment and if I want to create new timers or add a device I have to enter it directly on the database.
- Find a cheaper solution.As easy as the TellStick is to setup it’s not a cheap option (and that’s assuming you have a computer to run it). You can pick up a 433Mhz transmitter (most of these systems all use that frequency) for around £3. As the lucky recipient of a Raspberry Pi I’ll be trying to figure out how you could build the whole system for less than the cost of a TellStick.
I had to make a few tweaks to get my system to work correctly, but it’s now been working reliably for a few months and I’ve already stopped worrying about it, it’s become an automatic system I’ll just leave to run unaided (except for the tweaks mentioned above).
You’re limited more by the number of devices the protocol supports than the TellStick (16 on the ones I have) and as it supports such a wide range of protocols I could use it for a whole lot more than I am. I’d like to get things like my hot water and central heating on a similar system, potentially some curtain closers too (if I can find something suitable).
That’s barely scratching the surface of what you could do with it, but considering how easy (and relatively cheap) it is to get home automation up and running it’s a wonder why more people aren’t doing it.