I felt it’s about time I share a bit of background about my lighting setup at home, since a few people have been asking about it.
A few months ago I took the plunge and decided to support my former employer with a sizable contribution towards their Hue WiFi lightbulbs. The intention here was mostly to add an element of home security and with it, a degree of convenience.
I think Philips made the right decision by creating an API that supports a multitude of different platforms and languages. With things like HomeKit support seemingly taking longer than expected, the creativity of the design and developer community can certainly add some magic. In the long run we’ll hopefully have something that’s a bit more accessible to everyone else.
My objective was to create a simple setup where the lights in the living room come on at sunset time and switch off around midnight. I wanted to avoid the use of additional sensors and solely rely on data that’s available through the Internet. Despite the fun factor of an Arduino project, limiting the amount of networked components makes the entire setup more robust.
The brain behind the setup is a Synology Diskstation, something we invested in after our Lacie network drive gave up on us. The Diskstation is more or less a fully fledged Linux computer, which offers the advantages of being able to run and schedule all sorts of user-defined scripts. The Diskstation software makes setting this up easy with the web-based UI and task scheduler.
For the script I went with Python, supported by the excellent Phue library from studio imaginaire. I used the Astral module –which offers a ‘sunset’ function for any given city– to return the sunset time. Every morning at 4am the script is scheduled to run, which then creates a set of timers on the Philips Hue bridge.
A couple of things to be mindful of:
- The Philips Hue bridge works with an authorisation mechanism that requires you to press the button on the bridge the first time you run your script. I am having some issues at the moment where the bridge appears to ‘forget’ about the authorisation and stops the script from properly running the next day. The user-defined script section in the screenshot above of the scheduler shows the >>/tmp/daylights.out 2>&1 suffix, which creates a log file for me to track when this occurs.
- The Philips Hue bridge appears to have an issue with daylights savings changes, which is why in my Python script I have to offset the schedule time by an hour. Something to be mindful of when setting up your script. Getting the timing right requires a bit of tweaking and refinement over the course of a few days.
Finally you may want to stop people from actually switching off your lights. While my script contains a (disabled) section that sends an email (using another Bash script), there’s an easier solution that involves a few plastic wall plugs and your light switch. Simply place the plugs at the bottom behind the switch, and you’re all set.
And that’s it, feel free to have a go with my script, where the usual disclaimers apply: use at your own risk and use Google to answer any questions you may have.