I have a Hue sensor on my desk that turns on and off the lights on the desk when I am at it.
Since my desk is in our bedroom, and I don’t want the sensor to be active during weekends, I wanted an easy way to disable it on weekends and enable it after weekends (or maybe not enable it, I think I’ll enable it manually depending on whether I am actually working or not etc).
Yes I can do this via the app but it’s a bother and I wanted to just set and forget it. So I took a look at the Hue API.
First things first, you need the IP address of your Hue Bridge. I got that from the app. Then, you need to create an API key. The getting started section of the Hue API docs has instructions on creating this. I did that, put the key into pass and now I am ready to go.
|
1 2 3 4 5 |
# Once the API key is in pass I can refer to it thus (apiKeys/hue is the name of the secret) APIKEY=$(pass apiKeys/hue) # Also set the bridge IP in a variable BRIDGE_IP="192.168.200.200" |
The /resource/motion endpoint is what deals with sensors. To query it I do:
|
1 |
curl --insecure --request 'GET' --header "hue-application-key: $APIKEY" --url "https://$BRIDGE_IP/clip/v2/resource/motion" |
I must use the --insecure switch due to certs (it is a self-signed cert). Also, to make the output readable, one can pipe it through jq:
|
1 |
curl --insecure --request 'GET' --header "hue-application-key: $APIKEY" --url "https://$BRIDGE_IP/clip/v2/resource/motion" | jq |
The output is a list of sensors:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
{ "errors": [], "data": [ { "id": "72164b38-907d-42ad-a65b-4f22178c3e6d", "id_v1": "/sensors/47", "owner": { "rid": "20d706c2-1a05-4ef6-8a18-cdee0cf42b23", "rtype": "device" }, "enabled": false, "motion": { "motion": false, "motion_valid": false }, "sensitivity": { "status": "set", "sensitivity": 4, "sensitivity_max": 4 }, "type": "motion" } ] } |
The id is what I need. Currently it is disabled, as can be seen from "enabled": false. I stored it in a variable SENSOR_ID.
Now, enabling it is simple:
|
1 2 3 4 5 6 |
curl --insecure \ --request 'PUT' \ --header "hue-application-key: $APIKEY" \ --header "content-type: application/json" \ --url "https://$BRIDGE_IP/clip/v2/resource/motion/$SENSOR_ID" \ --data '{ "enabled" : true }' |
I must 1) change the request method to PUT, 2) set content-type headers for JSON, 3) modify the URL to include the sensor Id I previously got, and 4) send a JSON data that sets it to enabled.
Disabling is similarly easy, just change to false.
|
1 2 3 4 5 6 |
curl --insecure \ --request 'PUT' \ --header "hue-application-key: $APIKEY" \ --header "content-type: application/json" \ --url "https://$BRIDGE_IP/clip/v2/resource/motion/$SENSOR_ID" \ --data '{ "enabled" : false }' |
So now I can put each of these into separate files and setup a cron job to run the disable file just before the weekend. (The cron snippet below disables the sensor at 7pm every day; and enables it at 8am every weekday).
|
1 2 3 4 |
# m h dom mon dow command 00 08 * * 1-5 /path/to/sensor_enable.sh 00 19 * * * /path/to/sensor_disable.sh |
Next, I also wanted to turn off or on the lights affected by the sensor. Coz I realized that say I was working late and the sensor gets disable at 7pm via the above job – the lights now continue to stay on coz there’s nothing to disable them. So I must also add some code to disable the lights when disabling the sensor.
I have two lights together in a group, controlled by the sensor. The lights are in fake room of their own. So I ran the following command to first find the room details:
|
1 |
curl --insecure --header "hue-application-key: $APIKEY" --request 'GET' --url https://$BRIDGE_IP/clip/v2/resource/room | jq |
One of the entries looks like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
{ "id": "baddd0bb-23cd-4e13-8ddd-627d131f8b18", "id_v1": "/groups/82", "children": [ { "rid": "ef2f1315-7c24-4470-8d54-126026083250", "rtype": "device" }, { "rid": "c7109087-2324-4ddb-9496-0b285ca87d5a", "rtype": "device" } ], "services": [ { "rid": "1de1ee19-edd7-407c-8358-1dd1cc972143", "rtype": "grouped_light" } ], "metadata": { "name": "Desk", "archetype": "office" }, "type": "room" }, |
This is the room. You can see the group lights in there, with the id for the group.
Controlling a grouped light is the same way as controlling a light. Here’s what I did:
|
1 2 3 4 5 6 |
curl --request PUT \ --url https://$BRIDGE_IP/clip/v2/resource/grouped_light/$LIGHT_ID \ --header "content-type: application/json" \ --header "hue-application-key: $APIKEY" \ --data '{ "on": { "on" : false } }' \ --insecure |
The above snippet turns it off; change the false to true to turn it on.
I added this code to the same script as disabling the sensor, and now it disables the sensor and turns off the light! Yay.
Update (05 Nov 2025): A follow up post here as the API call to the sensor simply stopped working.
