4 minutes
Monitoring Temperature with a Hue Motion Sensor
It’s getting a bit chilly.. isn’t it?
We’re all needing to cut down on our energy usage this winter, and one of the most obvious ways of doing so is to knock down the thermostat by a couple of degrees or so. This does tend to mean one or other of the family disagrees with the new setting, and changes it back.
I found out recently that the Philips Hue Motion sensor that I’ve been using to automate some simple lights in the house has a handy built-in thermometer, so decided to build something to track the changes in how warm (or cold) the house gets, which hopefully will help settle some arguments about whether we’re being overly frugal!
It’s a bit of fun to try for yourself and if nothing else, it lets you play around with some common tools that get used to keep track of metrics across real applications and infrastructure in the world of IT.
This does assume some knowledge of computers and networking and the like, but it shouldn’t be too hard to get the idea if you like that sort of thing.
What Do You Need?
- A Philips Hue installation with a motion sensor
- A machine to run the software on 24x7
The Hue API
One of the nice things about the Hue API is that it’s all available locally, and doesn’t depend on a cloud service to control your devices in the house.
Getting the IP address
You need to get the address of the Hue hub to get started. This can normally be retrieved from whatever device does DHCP on your home network (normally a router) or you can visit the Hue discovery endpoint from a computer connected to the same network as the hub.
Using the CLIP API Debugger
The easiest way of interacting with the API is using the built-in CLIP debugger page. This is available via an HTTP
request to /debug/clip.html
on the IP address of the hub.
Creating a User
To interact with the API, a user needs to be created. This is done from the CLIP API debugger by making a POST request
with the following JSON to the /api
endpoint immediately after pressing the button on the Hue hub:
{"devicetype":"my_app_name"}
If everything works, you’ll get back a randomly created username that you can use to interact with the API.
Getting Data from the Sensor
You can get the state of all the sensors connected to the Hue hub with a GET request to /api/<username>/sensors
. It’s
just then a case of working out which data you want to collect and chart.
Prometheus
Prometheus is designed to scrape simple web accessible endpoints to collect data, so we need a small bit of code running that will periodically get the latest temperature from the Hue motion sensor and provide that endpoint for Prometheus to hit.
Luckily, the Python Prometheus client library makes that easy enough. Here’s a deliberately very simple implementation for one sensor.
If you have more, then it will need extending a bit, but this is the basic idea.
import time
import requests
from prometheus_client import start_http_server, Gauge
IP = "<your hue address>"
USERNAME = "<your username>"
DELAY = 30
SENSOR_ID = 12
def do_collection():
url = f"http://{IP}/api/{USERNAME}/sensors/{str(SENSOR_ID)}"
g = Gauge('temperature', 'Temperature')
while True:
try:
r = requests.get(url)
d = r.json()
temp = d['state']['temperature']
g.set(temp)
except Exception as e:
print(e)
time.sleep(DELAY)
if __name__ == "__main__":
start_http_server(8000)
do_collection()
Once that’s setup and running somewhere on your home network, you need to tell Prometheus it should scrape it. In this
case, setting it up as below as a static target in prometheus.yml
probably makes the most sense.
- job_name: 'hometemp'
static_configs:
- targets: ['<ip address of python host>:8000']
Grafana
After running for a few weeks, here’s the output showing in a Grafana chart.
What’s next?
This has been a fun experiment using an extra bit of functionality that was built into a sensor that I’d already purchased for use elsewhere. I’ll be looking around for other, hopefully slightly less expensive, sensors to add, and then look into some automation based on the data.
I’m almost certainly going to connect to the Octopus Energy API to retrieve data on the amount of energy we’re actually using, and comparing that to the change in temperature we actually see.
It’s been worthwhile to get a better idea of what changes actually happen in the home, and I’m looking forward to expanding it.