HUE motion sensor temperature to InfluxDB

HUE motion sensor temperature to InfluxDB

HUE motion sensor temperature to InfluxDB

Last night, I was broswing my HUE app, and I saw that my motion sensor did send temperature information.

So, I instantly wanted to add this input temperature to my Grafana dashboard.

HUE API

I've read the introduction about token generation on the official philips hue documentation : https://developers.meethue.com/develop/get-started-2/

Discovery

To automate the process of token generation OR if you don't know the IP address of your bridge, you wan surf on this page : https://discovery.meethue.com/ but you need to be on the same network as your hue bridge (or with m-dns rules).

Exemple with Opera browser

As you can see, the answer is a JSON format (I've hidden the bridge ID) and the internalipaddress is 192.168.100.50 (IOT VLAN in my case). It also works with curl / wget / others broswers :

Discovery process with curl

You can test it by browsing the IP address

Browsing the bridge IP address

Token generation

Before I read the documentation, in my mind : the application needed to initiate the token generation before pressing the link button on the bridge, but ... The application doesn't need to initiate anything before, you need to send a token generation request, after pressing the link button.

The API URL is ... /api (đź‘Ť philips) and, the second path in the URL must be the token. If you send a fake / wrong token you'll have the message

Wrong token error message

You need to send a JSON encoded POST request with the device type you "need" to process the token registration. It can be anything. Philips hue documentation exemple :

{"devicetype":"my_hue_app#iphone peter"}

So, I prepared the JSON encoded POST request with CURL, then I pressed my bridge link, and immediatly AFTER I ran this CURL command

curl -d '{"devicetype":"test#monitor01"}' -H "Content-Type: application/json" -X POST http://192.168.100.50/api
The curl result with a echo for better spacing

The request response is simple : the status (success), and our username (which is actually a token). Keep it securely. If you don't run the command fast enough, you'll have this error

List all sensors

Now, we have the IP address of the hue bridge and a functionnal token, we could list all sensors by sending this request

curl http://192.168.100.50/api/YOURTOKENHERE/sensors
All the sensors linked to my bridge

It's JSON format and could easily by parsed by using jq command, a simple pipe to jq will display the informations in a JSON conventionnal reading format, this will help to understand the structure and will be used after to extract the datas.

curl http://192.168.100.50/api/YOURTOKENHERE/sensors|jq
Result with the |jq

As you can see, there is an ID and a name, you can scroll until you find the sensor you need.

Also, you can use this command to only extract the ID and the name

curl -s  http://192.168.100.50/api/YOURTOKENHERE/sensors|jq -r 'keys[] as $k| "\($k) \(.[$k].name)"'

As you can see I have a lot of  sensors, but a sensor is not a full device, it's just a sensor. For exemple, the motion sensor create a least this sensors :

  • The motion sensor (5 in my previous list)
  • The temperature sensor (4 in my previous list)
  • The ambiant light sensor (6 in in my previous list)

Use a sensor

Now that we have spotted all the sensors we need (4 for temperature and 6 for lux) we want to display all the informations contained by the sensor. The request is simple (jq is only for human reading purpose) :

curl http://192.168.100.50/api/YOURTOKENHERE/sensors/4|jq

We see

  • The temperature (in celcius and that need to be divided by 100, I guess a int to float trick)
  • The battery level
  • The name

We have all the informations we want, it's extraction time ! As mentionned earlier, I will be using JQ :

curl http://192.168.100.50/api/YOURTOKENHERE/sensors/6|jq .state

It's keeps all the state part

And finally

curl http://192.168.100.50/api/YOURTOKENHERE/sensors/6|jq .state.temperature

2454 (so 24.54 °C).

Everything in a script

The final script temp-hue.sh, I used the -s parameter to prevent curl to display the request status, and I do use bc to calculated the float value

#!/bin/bash

token="YOURTOKENHERE"
ip_bridge="192.168.100.50"
temp=$(curl -s "http://$ip_bridge/api/$token/sensors/4"|jq .state.temperature)
temp=$(echo "scale=2;$temp / 100"|bc)

lux=$(curl -s "http://$ip_bridge/api/$token/sensors/6"|jq .state.lightlevel)

echo "philips-hue,device=capteur-salon temperature=$temp,lux=$lux"

And ... The telegraf input exec

Then the owner and execution flag for the script file

And finally service telegraf restart

Now I switched to my grafana dashboard and created a dashboard to visualize the new incoming data

If you have any question, please react on https://www.reddit.com/r/homelab/comments/cxmy9r/philips_hue_temperature_monitoring_grafana/