Philips Hue and a Raspberry PI.. the perfect fit!

Augustus 11th 2017

In 2015 I started to experiment with my first Philips Hue starters kit because of my discontent with the quality of the older 433mhz Kaku/Coco (Klik aan Klik uit) products I was using.  The signal didn’t reach everywhere and often lights didn’t switch on or off when I wanted them to.  I had read about Hue before but the relative high price always kept me away from the system. 

As most of you probably know Philips Hue comes with a bridge that controls everything and you can extend your network with remote control switches, lamps and motion sensors. The system has a well documented API and using simple linux commands you can control and read out the whole system.  In this article I will give you some simple tips & tricks of using Hue with a raspberry and point out some not-so-well documented features. 

Basic control from Linux

There are several ways to control the hue system, write your own script or use some of the existing ones. The one I hue is a little perl script named It allows you to do basic controls on Hue lamps like switching them on and off. The use of this little tool is well documented and it works very well. Just make sure if you have multiple Hue bridges (like in my situation) that you put the scripts in separate directories (one for each bridge).

Reading the Hue json files

In my house I have every room equiped with Hue Motion sensors. Using the Hue app you can program them to turn on or off lights when motion is detected. I personally decided to let the decisionmaking be done by my raspberry. I simply poll the Hue bridge once a second and read out all it’s variables. Some of those variables are quite interesting.. and a lot more is possible then just detecting motion. 

To read the json file I issue this command;

wget --output-document=/tmp/huesensors.txt http://<IP>/api/<APIKEY>/sensors

(make sure you replace the IP with your bridge IP and the APIKEY with your apikey). There are several ways to get a api key but one of the easiest ways might be by just using the one found in the hueplkey file that is generated by the huepl command mentioned above. 

Now let’s see the data we just downloaded. For this I use the jq command to pipe the json data to give me a nice output;

cat /tmp/huesensors.txt | jq “.”

This will give me about that looks like this;

The value I poll for motion is presence. As soon the motion detector picks up motion this value will change to true. As you can see in the above sample there are other interesting values to poll. I also poll the battery level (100% in the above example) and if we look a little bit futher;

there are also values for current temperature (21.57c right now) and a ‘reachable’  value telling you if the sensor is connected to your Hue bridge. One more value you could read is ‘lightlevel’. I use the lightlevel for charts like this;

Some of the same values (like reachable and state on/off) can not only be pulled from the hue sensors but also from the hue lights. For this we only change the last part;

wget --output-document=/tmp/huesensors.txt http://<IP>/api/<APIKEY>/sensors

so all together the Hue system has a lot more to offer than just light and motion. The official limit on motionsensors for one Hue bridge is 8 devices. However, if you don’t use the motionsensors to control the lights directly you can delete some of the rules and connect more than 8. In my case I use 2 bridges simply because of range issues. 
I also noticed the ‘reachable’ state doesn’t work very well with 3rd party bulbs and switches. I have also connected some Osram and INNR lamps/switches and they sometimes report their reachable state as false while they work just fine. Even the Hue Remote controls have info about their batteries and reachability. All together making Hue a very good home automation system. 

One final tip

When you are polling your bridge you can also poll for a lamp’s brightness, hue and saturation;

What I did was make the raspberry trigger a script when the brightness, hue and saturation of a very specific lamp has very specific values. So when on my hue remote control I activate scene #5 it will trigger my ‘going to bed’  script. It will not only turn off my lamps but also lower my thermostat, turn off the tv and radio etc. So in a way I use a very specific color setting to activate a script. In theory you could also use Siri for this.