Fun and games with the Hue API

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.

The /resource/motion endpoint is what deals with sensors. To query it I do:

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:

The output is a list of sensors:

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:

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.

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).

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:

One of the entries looks like this:

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:

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.