Here is a cheeky script to drive the API on the Philips Hue lighting system to change the colour of a lamp based on the temperature. That’s right you can colour the light based on the temperature outside, quite neat so you can quickly see if its cold, hot or indifferent just by the colour of the bulb.
The script was written as a way for me to learn a bit of Python scripting, manipulating JSON and Python dictionaries and using a Restful API in general. The scripts are fairly basic but hopefully someone will find them useful to play around with or add to in future.
System Requirements
You will need a Philips Hue Bridge with at least one colour change bulb, I’ve only got on Philips Hue Play Lamp (which changes colour), so I’ve only tested the script with this.
You will need to register and generate an API Key for the Philips Hue Bridge, there’s great documentation on the Philips Hue Developer website about this, so i’m not going to repeat it here; but in a nutshell, you need to find the IP address of your bridge, then go to a URL on it, run a command, it will create a key (you’ll need to press the button on the bridge to confirm), then you’ll have a API key presented, which you need to use when calling this script.
The second thing you’ll need is an API key from Open Weather Map (https://home.openweathermap.org), the script uses their API to get weather information, again its free for a basic account and then you can generate an API key which you’ll also need to use when calling this script. You’ll also need to find the location format for where in the world you are, for London in the UK for example it is: London,GB.
You may need some Python Modules, you can install these with pip install <modulename>, if required:
- requests
- pyowm
Script Description
There are two scripts provided, yes I know both could be written into one, but i’ve not yet got to the point of learning enough about Python yet! The first script called weather-hue-id.py is used to interrogate your Bridge and show you the list of all the lights on your Bridge, along with their ID’s, it is the ID of the colour light you will need when you come to run the next script.
The second script is called weather-hue.py, its the script that actually does the work to check the current temperature and then change the light colour.
Using the Scripts
You will need to run the weather-hue-id.py first to show you all the IDs of your lamps. Here’s an example, obviously substitute in your Bridge IP address and API Key.
python weather-hue-id.py -g <Bridge IP Address> -k <API Key>
python weather-hue-id.py -g 192.168.1.10 -k 4237DGSDGFG123DGSGS
Once the script runs you’ll get the output of all the lights on your Philips Hue Bridge. If a lamp is a “Play Lamp” it will be shown with an XY colour value, you can also check here to see the Connection State of each lamp as well as the On/Off State of each lamp. So when I ran this my Play Lamp was on ID 11, yours will likely be a different ID.
Now we can run the script to actually identify the temperature and then change the light colour, you need to put in the location (which is the location as specified by the Open Weather Map site), the Light ID that you just identified and then you can specify if script should run in debug mode or not, 0 for off, 1 for on, the debug mode can be helpful to identify when problems occur.
python weather-hue.py -g <Bridge IP Address> -l <Location> -k <Philips Hue API Key> -j <Open Weather API Key> -i <Light ID> -d <Debug Mode>
python weather-hue.py -g 192.168.1.10 -l London,GB -k Pu5MQs5URdh1MjjGrmypKPu5MQs5URdh1MjjGrmypK -j 5367ed1fb0waq234c4702d12345be5e -i 11 -d 0
When you run you’ll get a result such as this:
In this example, the light is switched off, you can only change the light colour if the light is on.
Automating the Script
You can automate the script in various ways, in my example i’m going to install the script to run periodically on my Raspberry Pi, so i’ll just put it in as a crontab. First run the script manually just to test it works as expected. Then add it to the crontab.
crontab -e
Then paste in the line above preceded with the crontab information and stick the /dev/null on the end so it doesn’t give output, you can of course replace this with a file if you wish so you can see any output for troubleshooting.
*/15 * * * * python weather-hue.py -g 192.168.1.10 -l London,GB -k Pu5MQs5URdh1MjjGrmypKPu5MQs5URdh1MjjGrmypK -j 5367ed1fb0waq234c4702d12345be5e -i 11 -d 0 >/dev/null 2>&1
Script Download
You can download the scripts from the files section below! The scripts are provided without any warranty and should be used at your own risk, no responsibility is taken by the site author for loss or damage the occurs from using the scripts.