Node-red + Hue + motion

Hope the node-red people here can help me with this : I want to use a PIR in the kitchen so if motion in the kitchen is detected some special “motion” scene is started. But I also want to read our the current hue color etc state from the kitchen Hue lamps , so after the motion is detected , the current color etc is read out, and then the motion scene is started for some minutes. And after this minutes of motion scene the read out hue status before the detected motion is restored ?
Short
Motion - read hue states - start motion scene for X minutes - restore hue state before detected motion

But how :slight_smile:

Are you wanting to do this with the Scenes in the Hue hub, or make your own within nodered?

Want to do this with node-red , the PIR is a zwave one

So you’re not wanting to start a hue scene?

I don’t think I’m quite understanding the question, but here’s a motion flow I have using Hue lights, maybe it will help give you an idea.

[{"id":"2759a671.a17caa","type":"subflow","name":"Nightlight Lights off?","info":"","category":"","in":[{"x":40,"y":80,"wires":[{"id":"97ae16db.976fe8"}]}],"out":[{"x":640,"y":80,"wires":[{"id":"b39b1a4b.c6a6c8","port":0}]}],"env":[],"color":"#DDAA99"},{"id":"97ae16db.976fe8","type":"api-current-state","z":"2759a671.a17caa","name":"TV lights off?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.tv_strip","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":190,"y":80,"wires":[["697d0d91.347424"],[]]},{"id":"697d0d91.347424","type":"api-current-state","z":"2759a671.a17caa","name":"Pendant lights off?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.pendants","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":290,"y":140,"wires":[["b39b1a4b.c6a6c8"],[]]},{"id":"b39b1a4b.c6a6c8","type":"api-current-state","z":"2759a671.a17caa","name":"Kitchen lights off?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"group.kitchen_night_lights_automation","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":470,"y":80,"wires":[[],[]]},{"id":"988c6b12.5820a8","type":"tab","label":"test 4","disabled":false,"info":""},{"id":"54d030c5.0dcd8","type":"comment","z":"988c6b12.5820a8","name":"1. (Below) If it's dark out, and the Pendant and TV lights are off, turn on kitchen can 1 and 3 to 10% for 25 seconds on motion_____________","info":"","x":710,"y":260,"wires":[]},{"id":"fd2008c8.8ebcb8","type":"api-call-service","z":"988c6b12.5820a8","name":"Kitchen night lights off","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_off","entityId":"group.kitchen_night_lights","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1340,"y":320,"wires":[["786cf1aa.b7a7b"]]},{"id":"6c64ff8b.bcc18","type":"api-current-state","z":"988c6b12.5820a8","name":"Kitchen can 1 off?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.kitchen_can_1","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1690,"y":380,"wires":[[],["fd2008c8.8ebcb8"]]},{"id":"5b0ba395.432d2c","type":"api-current-state","z":"988c6b12.5820a8","name":"Kitchen can 3 off?","server":"26956a45.92d866","version":1,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.kitchen_can_3","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1670,"y":260,"wires":[[],["fd2008c8.8ebcb8"]]},{"id":"9cea9357.6ebf4","type":"stoptimer","z":"988c6b12.5820a8","duration":"25","units":"Second","payloadtype":"num","payloadval":"0","name":"25sec","x":1130,"y":300,"wires":[["fd2008c8.8ebcb8"],[]]},{"id":"bb968cf9.34294","type":"time-range-switch","z":"988c6b12.5820a8","name":"","lat":"27.918805","lon":"-81.866830","startTime":"sunset","endTime":"sunrise","startOffset":0,"endOffset":0,"x":500,"y":300,"wires":[["8fa5a19f.b7e26"],[]]},{"id":"951e1965.2f0c98","type":"api-call-service","z":"988c6b12.5820a8","name":"Kitchen night lights on (10%)","server":"26956a45.92d866","version":1,"service_domain":"light","service":"turn_on","entityId":"group.kitchen_night_lights","data":"{\"brightness_pct\":10,\"transition\":6}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":940,"y":300,"wires":[["9cea9357.6ebf4"]]},{"id":"386ab42e.8c02ec","type":"server-state-changed","z":"988c6b12.5820a8","name":"Kitchen Motion","server":"26956a45.92d866","version":1,"entityidfilter":"sensor.kitchen_motion_burglar","entityidfiltertype":"exact","outputinitially":false,"state_type":"num","haltifstate":"8","halt_if_type":"num","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"x":320,"y":300,"wires":[["bb968cf9.34294"],[]]},{"id":"786cf1aa.b7a7b","type":"delay","z":"988c6b12.5820a8","name":"1sec","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"10","nbRateUnits":"11","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1510,"y":320,"wires":[["5b0ba395.432d2c","6c64ff8b.bcc18"]]},{"id":"8fa5a19f.b7e26","type":"subflow:2759a671.a17caa","z":"988c6b12.5820a8","name":"Nightlight Lights off?","env":[],"x":700,"y":300,"wires":[["951e1965.2f0c98"]]},{"id":"26956a45.92d866","type":"server","z":"","name":"Hass.io","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

It’s a nice one to start . But what I can’t find within this flow : the read out from the current hue state (color / dim% etc)
And what is mean with the hue motion scene , is a scene a created with the hue app , so the spots are in a chosen color etc.

Normally I powered on the hue lights and change the color and % to the colors etc I want at the moment .
This is why I want to read out this colors / % (or probably chosen hue scene) so after the PIR triggered the motion hue some the colors / % are returning to the settings before motion was detected

Hope this will explain a bit more what I am looking for

OH! You want to see the attributes of the lights, I’m with you now.

Let’s start with the Hue scene. I just create all my scenes within Node-RED, but if you want to use a hue scene, you’ll need a call service node configured to call the scene:

As far as getting the attributes of the lights, I’m going to think about it.

1 Like

So I’m not sure how to do this, I’m still figuring Node-RED out myself. Let’s start with using a debug node (set it to full msg) on a hue light and change it’s color. We need the path to the attribute of the color.

I used a current state node on my sons lamp, and turned it green

image

And the output of the debug is this:

I think rgb_color will probably be the best way to go.

I was trying to split the colors with a function node, but I’m doing something wrong:

var color=data.attributes.rgb_color;
if (color=="[0,255,65]"){ //green
    return [msg,null];
    
}

if (color=="[64,0,255]"){ //blue
    return [null,msg];
    
}

My thoughts are that you could split the colors off into different automations to change the scenes, I know it can be done with a function node, but I’ve got it wrong somewhere.

I’m gonna see what I can do with a switch node.

maybe there’s something pissoble while using the “HueMagic” node-red component Node-red HueMagic … will try this upcomming weekend

did somebody made some progress :slight_smile: ?
Was busy using the HueMagic flows:

1 Like

tried with the above node-red to find som info , but at the moment still no idea to get / pull the current status from all the Hue lights.
And then how to store this , so after the motion “scene” is started i can restore the Hue lamp’s to this state ?
I’am not the first one i think (hopefully) :slight_smile:

did you make some progress ?

I haven’t looked at it recently, but I’m happy to try and figure this out. LEt me create a Hue scene and see if I can make it do something.

What hue scene are you using?

To clarify, you want the lights to already be on a Hue scene, then on motion, activate a different hue scene, and when motion is no longer detected, go back to the previous scene, correct?

Yes indeed, the reason behind this : i start a scene and changed something like the brightness , and after the PIR detection i want it to return to this state ( scen and the changed brightness).
The PIR detection scene it needs to start is a default scen i created within the Hue app

Let’s start by finding out how how Hue presents itself to Home Assistant. I never use Hue for anything other than the gateway for my lights, but I created a room and a zone and appended them with “_hue” so I know that they came from the Hue hub.

Give me a few more minutes, I’m working on the next step.

Here’s the testing flow I’m working off of.

image

Here’s what the debug node shows when I activate the Hue group from the Hue app. The scene name does not appear to be shown to Home Assistant / Node-RED. It looks like it passes on the light attributes. All of these attributes are the total output of the scene.

image

Question: Are your different Hue scenes static colors? Meaning, they do not change and it’s only one color? If so, why do you want to use the Hue scenes, instead of creating it in Node-RED?

I was able to recreate my test Hue scene using the standard light.turn_on service. Here’s the attribute we will need (you’ll also need the brightness to accurately reproduce the scene) -

image

We take that and plug it into a call service node, and connect a inject node -

image

Now if you use an inject node, a current state node and a debug node set the full message, this is the output -

image

You see they are almost identical. With the hue.hue_activate_scene service, you have:

{
 "brightness":59,
 "hs_color":[268.138,56.863],
 "rgb_color":[177,109,255],
 "xy_color":[0.273,0.157]
}

And with the light.turn_on service:

{
 "brightness":59,
 "hs_color": [267.692,56.078],
 "rgb_color":[177,112,255],
 "xy_color":[0.273,0.16]
}

They are so close, I doubt that you could tell the difference by looking at them.

We can probably do this using the default Node-RED nodes, if you want to use the Hue scenes names directly from the Hue hub, I’ll install Hue magic when I get home tonight and see what I can figure out.

Here’s the code for my test nodes if you want to play around with it

[{"id":"b10614eb.d46f58","type":"api-call-service","z":"1663a5e8.75665a","name":"Activate hue scene","server":"26956a45.92d866","version":1,"debugenabled":false,"service_domain":"hue","service":"hue_activate_scene","entityId":"","data":"{\"scene_name\":\"Starlight\",\"group_name\":\"Living room (hue)\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":710,"y":560,"wires":[[]]},{"id":"5cccafd4.91176","type":"inject","z":"1663a5e8.75665a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":540,"y":560,"wires":[["b10614eb.d46f58"]]},{"id":"6d537674.84bf48","type":"api-call-service","z":"1663a5e8.75665a","name":"node-red hue scene","server":"26956a45.92d866","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.living_room_hue","data":"{\"brightness\":59,\"hs_color\":[268.138,56.863]}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":720,"y":620,"wires":[["5fcf225a.01515c"]]},{"id":"50b5fa79.99ec84","type":"api-current-state","z":"1663a5e8.75665a","name":"Living Room Hue Current State","server":"26956a45.92d866","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.living_room_hue","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":750,"y":680,"wires":[["a23eaf73.f0175"]]},{"id":"a23eaf73.f0175","type":"debug","z":"1663a5e8.75665a","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":680,"wires":[]},{"id":"819302d9.56623","type":"inject","z":"1663a5e8.75665a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":540,"y":680,"wires":[["50b5fa79.99ec84"]]},{"id":"49ade2ee.158a3c","type":"inject","z":"1663a5e8.75665a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":540,"y":620,"wires":[["6d537674.84bf48"]]},{"id":"5fcf225a.01515c","type":"debug","z":"1663a5e8.75665a","name":"3","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":890,"y":620,"wires":[]},{"id":"26956a45.92d866","type":"server","z":"","name":"Hass.io","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

So, with the Hue magic nodes, it reports the name of the scene in the debug output.

image

Here's what my test nodes look like.

image

The orange scene nodes turn on the selected scene, no matter the payload. It only turns on the scene, there is no way to toggle it off.

The purple group node can turn the hue group on and off. It will turn the group on to the last scene that was used.

So, I was going to try a bunch of switches and such, but would this do what you’re needing?

image

In this example “nightlight” would be the default scene, and Starlight would be the scene that activates on motion. When motion is no longer detected, it turns the Nightlight scene back on.

1 Like