General Musings

Posts about my general musings about life, technology and other things I have an opinion on!

Driving Philips Hue with Python Script via API (Colour of Temperature)

posted 9 Mar 2019, 12:08 by Tristan Self   [ updated 10 Mar 2019, 12:58 ]

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 (, 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 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, 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 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 -g <Bridge IP Address> -k <API Key>

python -g -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 -g <Bridge IP Address> -l <Location> -k <Philips Hue API Key> -j <Open Weather API Key> -i <Light ID> -d <Debug Mode>

python -g -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 -g -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.

Nissan Leaf (2018) Range Tracking

posted 9 Mar 2019, 11:52 by Tristan Self

So I took the plunge on a lease of an electric car, I've always been interested in electrically powered transportation, but only now has the battery technology developed to the point where it is 1. practical and 2. cheap enough to do. There's lots of benefits to the planet, environment and health, and also the cost savings (until the government adds some tax!). As of the middle of 2018, when I took out the lease, one of the leading EVs (Electric Vehicles) on the market was the new Nissan Leaf (2nd Generation). 

I'll give my thoughts on the car, bear in mind i'm not a car journalist, so you should take the information I give below as merely my opinion; also bear in mind the main reason for this article is for me to give some statistics on the range of the vehicle in real world use, and my thoughts of if this is a problem or not! Of course the technology for electric vehicles is developing rapidly, I don't believe there is any future for the internal combustion engine in the long term other than very niche usage; the future of transportation is electrified, be it with batteries (Lithium Ion, Solid state) or some other method like like Hydrogen fuel cells. 

Okay now the blurb is out of the way i'll go through my use of the car and the results of the range usage, but first i'll explain a few basics about the battery vs. an internal combustion engine. The battery of an EV is pretty much the same as what you would find in a mobile phone or laptop, its a Lithium Ion battery (bear in mind there are lots of adjustments on the battery chemistry that are being made) but essentially just on a larger scale than in your phone or laptop. The size of the battery is normally given kWh (that's Kilowatt Hours), so if you had a 30 kWh battery and you drained it at the rate of 1 kW (Kilowatt), it would supply that power for 30 hours (hence the 30 kWh Kilowatt Hours), okay that is a very simplistic overview but its essentially the relationship between the figures you'll see given.

So from an EV point of view, the Nissan Leaf (2nd Generation) has a 110 kilowatt electric motor and a 40 kWh battery, so that means if you were to put your foot flat down and use the full 110 kilowatt of the electric motor at all times you'd basically run the battery flat within about 15 minutes! Of course in reality you wouldn't do that, but it gives you an idea of how driving carefully could extend the range of an EV.

Petrol Vs. Battery

So to an ICEV (Internal Combustion Engine Vehicle), lets say it has a 40 litre petrol tank, 1 litre of petrol is roughly equivalent to 10 kWh citation. An EV with a 40 kWh battery is the equivalent of an ICEV with a 4 litre fuel tank, which would get you about 30 miles (if you get around 40 miles per gallon economy)! The difference, of course, is the efficiency of the electric power train compared to a petrol powered drive train, the EV is much more efficient and that petrol is an extremely energy dense fuel. So for example I used to have a Mini Cooper, this had a 35 litre fuel tank, with that I could get about 310 miles of range (at about 40 miles per gallon very roughly), so that would be equivalent to a 350 kWh battery in an EV.

So a Nissan Leaf that can go around 140-150 miles on a 40 kWh battery, would be able to go around 1,312 miles on 350 kWh battery, to compare an ICEV would go around 310 miles to an EV on 1,312 miles on the "same" amount of energy used.

Anyway enough of my pigeon science, you get the idea of the basics of how the size of battery affects the range and how this tallies up to a petrol vehicle, so now i'll move onto how I use my EV and what this means for the range data that I have collected.

Driving Profile

I use the car primarily for travelling to and from work and then some sort trips to the shop, gym, running errands etc. So my commute is about 27 miles each way, so around a round trip of about 54 miles a day, surprisingly this distance is more than the daily UK average citation. Has having an EV caused me any issue with range, simple answer no. Which makes me think for the vast majority of people who use their car for commuting only, an EV with a 30-40 kWh would be sufficient for them to do at least 2-3 days worth of commuting without needing to charge.

So travelling about 54 miles a day, in my Mini I never drove more than 100 miles in a single day, possibly this is odd, I never took the car on holiday so never really used it that way, but think of the population overall, what percentage are the same as me and only use the car for commuting or possibly just to make short trips around town, probably quite a few.

I'm lucky that I have a driveway to my house and a home charger (6 kW) so I can charge the car from flat overnight without issue, I also have access to chargers at my place of work, so can charge during the day as well if required. With this as I don't do long journeys, i.e. set out on a journey that is longer than the range of the battery (ergo i'd need to charge on route), charging when the car is parked at work or at home is fine. So the issues with the "Rapidgate" saga with the Nissan Leaf (2018 model) and still limited charging facilities on the UK road network hasn't been an issue for me.

Okay so this gives you an idea of my driving profile, commuting around 54 miles a day and some odds and ends 2 to 5 miles perhaps extra for errands etc. My journey to work that makes up the vast majority of use is also possibly non-standard to a certain extent, its a mix of urban driving (stop start and constant speed 30 MPH), dual carriage way (70 MPH), single carriage way (40-60 MPH) and country tracks (20-40 MPH), although the bulk of the journey is at over 40 MPH on dual carriage way or single carriage way roads. I don't drive to the 55 MPH that EVs are most efficient at (wind resistance) I just essentially drive the same way I drove my ICEV.

Range Tracking Results

The results of the range shown below are those that I have collected over a period of time, they are not particularly scientific collected in identical lab conditions, they are the real world figures I achieve driving the car. I like the car very much and range anxiety has never been an issue for me 

The car is around 6 months old at the time of writing, it has the full 12 bars of battery health, the vehicle has re-generative breaking capabilities, these are taken into account by the figures, essentially the breaking recharges the battery and thus adds to the range (or a better way of putting it is that it reduces the rate that the battery is drained. The results have been obtained during 2 weeks (or so) within January 2019, as its winter time I've been able to collect data to assist in the determining the impact of the heater (air-conditioning) on the range. I'll take some figures later on in the year (i.e. during late Spring) to gather some statistics where the heater (or air-conditioning) is not being used and later on in the Summer when air-conditioning (for cooling) will be in play. I'd also be adding the use of ECO mode, to see what affect that has the range of the vehicle within a future data collection.

You'll notice a column called "Linear Discharge Range", this is a calculation/estimate based on the discharge profile of the battery on that journey, that assumes if the discharge was to continue in a linear fashion until the battery was flat what an estimate range is of the vehicle. The calculation for this is as follows:

( ( End Mileage - Start Mileage ) / ( Start Battery Percentage - End Battery Percentage ) * 100 )

I pre-warmed the car some days before starting my journey, mostly this was just off the battery to clear frost and warm up the car, but on one day I pre-warmed on mains (while the charger was still connected) so the car used the mains to warm up, rather than draining the battery.

I also drove using the e-pedal mode engaged for all urban driving (i.e. 30 MPH) upon leaving urban areas this was turned off.

You may also notice the day to day mileage and battery levels may not flow, this is because I made short journeys between where I did not note down the mileage or usage, these are normally mid-late evening to go to the Gym and with the heating on high. The column entitled GOM means Guess 'o' Meter, this is the range estimate the car gives based on the current usage and driving pattern.

DateTemp (c)Battery %MileageGOMBattery %MileageGOMLightsCli. Ctrl.ECO Modee-Pedal (Urban Only)Linear Discharge RangeNotes
17/01/201901007482142777514113YesYesNoYes139Pre-Warmed on Mains


The range without using climate control (heating in the case of these figures) puts the range based on my estimate at about 140 miles (not sure how I got 180 on the first day, but that wasn't a trip to work) so it would seem reasonable with some more conservative driving and no climate control on you can achieve around 150 miles on a charge - these are shown highlighted in orange. Obviously ECO mode would be good to test next to see if that increasing it at all.

With air-conditioning (heating) turned on there is a noticeable impact on the range with it being reduced to around 110-120 miles.

The outside air temperature (i.e. cold) doesn't appear to make much odds on the performance on the battery, but more data is needed to confirm this; of course this is difficult to gauge because more power will be used by the heater.

What is a bit of a surprise (highlighted in Green) is the pre-warming taking place using the mains rather than off the battery, now obviously the battery is used to heat the car before I start the journey so its assumed there will be some capacity used there, but what a bit of a surprise was that this seemingly increased the range. Where I pre-warmed off mains and where i pre-warmed off battery the pre-heating off mains gave a longer range, certainly more investigation will follow about this to confirm if this is the case. If it is confirmed to be the case, just warming the car ahead of your trip gives a range not far off having no heating on at all.

To conclude the range of a 40 KwH Nissan Leaf (2018) looks to be:
  • 140-150 miles with climate control (heating) turned OFF.
  • 110-120 miles with climate control (heating) turned ON.

1-2 of 2