Setting the scene on a Hue light via API

Continuing my trilogy (here and here) of Hue API posts… in my previous post I detailed how I set the lights to turn or or off when I login to my Mac.

Slight issue with that. When I used to do this via the Hue sensor, it would set the correct scene coz the sensor was set to power on the light with a particular scene. Now when I turn it on directly, it just turns on to whatever scene was last set and not a specific one I want.

Figuring out how to do this was a bit more work. Lights don’t seem to have scenes associated with them, but there is a scene API so I started from there.

To get a list of all my scenes:

The output of that looks like this:

The scene name can be found in the metadata > name property. What I learnt from some fooling around is that each of these scenes has a rid property which is the room id the scene is associated with. (And the lights themselves are in rooms have so have a rid associated with them, and that’s how you link a scene to a light).

I had some fun with jq. It’s been a while, and while I usually use PowerShell and never had to fuss too much with extracting data from JSON, this was a good excuse to stick with Bash and jq.

I piped the output of the above command into jq like this:

The output is a list of scenes and room ids and scene ids.

Next I went through the lights I wanted to modify, and found their room Ids.

At this point I could have just dumped the output of the first command that gives scenes and room ids and ids, and searched for the room ids of my lights. But I wanted to keep things interesting, so I took one of the room Ids and decided to search all scenes for that room id.

This will return a single id, that of the scene.

Later I realized I have two set of lights in two set of rooms, so I modifed the above to search for both rooms. (Both rooms had the same scene name, so that’s why I don’t search for multiple names).

Now I have the two scene Ids I am interested in. Time to see if I can turn on the lights with these ids. Each scene Id is for a particular light. (Disclaimer: my situation might be a bit different from anyone else reading this. I have a set of lights that I put into separate “fake” rooms in Hue, so that’s why I am fussing about with rooms. I do this just so I can turn on the bedroom “room” lights without turning on the lights on my desk etc. too even though they are all in the bedroom).

The instructions on turning on a scene aren’t very clear in the API docs. All they have is the following which you send via a PUT request:

So first I tried this:

Didn’t work! I got an error:

Then I took a look at the scene JSON again and saw the following:

Hmm, last status is inactive. So I changed my code to do active, instead of on.

Boom! That worked. 😎

Interestingly, you’d think that sending the action as inactive instead of active will turn it off, but that didn’t do anything. I got an error again:

Which is fine, I guess. What you want to do is turn off the lights anyway.

Here’s an updated version of my script that turns on or off the lights:

And that’s all folks! 🤓

Update (7th Nov 2025): I noticed that whenever I’d lock the screen Hammerspoon wouldn’t always run the script to turn off lights. It could be a time issue I suppose, coz reloading Hammerspoon and then locking the screen does. turn off lights. I am not entirely convinced it’s a time issue coz if I leave the system as is overnight and then login the next day, the script to turn on lights runs.

Maybe I am spending more working than I don’t? 🤓

Anyways, slight hack to fix the issue. I added the following to my config:

This causes Hammerspoon to bind to the keys I use for locking the screen, execute the script and then proceed to lock the screen. It’s not ideal coz it doesn’t capture the scenario where I step away for a while without locking the screen and the system goes to sleep and the script doesn’t run.