Identify group state change in Node-RED

I have a light group, let’s say group.tv_lights, containing a few Hue lights.

I’ve created a Node-red flow that will set the brightness and color of the lights in this group when an input_boolean is triggered. I’m calling this a scene. I have one input_boolean for each scene. Let’s say “Scene Blue” and “Scene Orange”.

Once this scene is activated, I wait 5 seconds and reset the input_boolan so that this scene can be enabled again later via this same input. So far, so good.

I also have a node listening to Plex and setting the input_boolan for “Scene Orange” to “on” automatically so that my TV lights are turned on with the settings I defined.

Now, that’s where it gets interesting. I don’t want Plex to change the lights if “Scene Blue” is already on, since this is also a TV scene I defined.

My question is: how can I make Home Assistant/Node-red identify that a certain group configuration constitutes a scene? I’d like to stick to Node-red (instead of HA Automations) so that I can manage all my automations in one place (Node-red).

For example, if I manually (via the Hue app or my wall switch) change the state of one of the lights in the group, Home Assistant should know that my “Scene Orange” is no longer orange. Same goes for when I change the brightness.

Writing code is not a problem for me, so I’m open to using the “function” node. I just couldn’t get my head around how to do this within Node-red.

Thanks!

Have an input select with all possible scenes plus an unknown. On your scene triggers set the select to the scene that is active. Then use an event state node to watch all the entities that could change and set the select to unknown if anything changes. Reference the select anytime you want to know the current “scene”.

Also, when a light changes there is actually a userID embedded in the message.


In my example its in the new_state.context.id

This is different depending on how its triggered. Triggered by NodeRed / HA / HUE etc. Maybe you can play around with this too.
What I usually do is to work with a traffic light in this case or a saved variable that I set if a change is triggered by an automation. Only if this variable is set, then the automation can proceed. Otherwise it assumes a manual change that should not be overridden.

As always… many roads lead to rome :wink:

Sounds like a good plan. I hadn’t thought of the dropdown helper for this. I’ll give it try asap.
Thanks for your suggestion :slight_smile:

Nice catch with the user_id. Looks like I can indeed use that to track automation vs manual changes. I’ll give it a try.
Thanks!

Took a while before I had the time to try out the suggestions, but I’ve finally made it. Thanks for the suggestions, I have now my scenes working as I wanted. This is how my flow looks now:

I use the change node to inject the payload with the light color and brighness according to the value of the input.

you should post the json

Not sure how useful this will be, since it’s quite specific to my setup. That’s why I only posted the screenshot so that people can use the main idea.

But in any case, here’s the json for my current automation (it has evolved since I posted the screenshot).

[{"id":"fa7cf36b71729e87","type":"server-state-changed","z":"4d4a64331b17df94","name":"Is Plex running","server":"97bbe632.da4568","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"media_player.plex_plex_for_lg_lg_oled55c8pla","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"playing","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"5","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":220,"y":500,"wires":[["1b09138e2ce6289e"],[]]},{"id":"d487201a4d31aa70","type":"server-state-changed","z":"4d4a64331b17df94","name":"Scene TV Nightlight","server":"97bbe632.da4568","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_select.tv_lights","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":310,"y":740,"wires":[["0587c7ea9815c879"]]},{"id":"0587c7ea9815c879","type":"switch","z":"4d4a64331b17df94","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Fireplace","vt":"str"},{"t":"eq","v":"Lime","vt":"str"},{"t":"eq","v":"Neon","vt":"str"},{"t":"eq","v":"Pinky","vt":"str"},{"t":"eq","v":"Purple","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":530,"y":740,"wires":[["a42f71ab33233c05"],["2ef89281c823c19a"],["f8d0d8eaf8f65be5"],["6ec5b6d85b40c027"],["58e5424e4ab65bcd"]]},{"id":"a42f71ab33233c05","type":"change","z":"4d4a64331b17df94","name":"Fireplace","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"light\":{\"brightness_pct\":20,\"rgb_color\":[255,162,42]},\"name\":\"Fireplace\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":680,"wires":[["89ee6efe31b0974e"]]},{"id":"6d3a2ef3d1a082e7","type":"api-call-service","z":"4d4a64331b17df94","name":"Turn On Nightlight","server":"97bbe632.da4568","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"group.lights_lr_all","data":"msg.payload.payload.light","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1130,"y":780,"wires":[["ae7d719c71c26ad2","677dbdcd1e95d0b8","182a19f485ee5dda","efcdacf9a6beb41a"]]},{"id":"677dbdcd1e95d0b8","type":"api-call-service","z":"4d4a64331b17df94","name":"Turn Off Kitchen","server":"97bbe632.da4568","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.kitchen_ceiling_1, light.hallway_downstairs, light.hallway_upstairs","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1460,"y":760,"wires":[[]]},{"id":"182a19f485ee5dda","type":"api-call-service","z":"4d4a64331b17df94","name":"Turn Off Christmas Tree","server":"97bbe632.da4568","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.home_extra_1","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1430,"y":820,"wires":[[]]},{"id":"2ef89281c823c19a","type":"change","z":"4d4a64331b17df94","name":"Lime","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"light\":{\"brightness_pct\":20,\"rgb_color\":[217,255,108]},\"name\":\"Lime\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":720,"wires":[["89ee6efe31b0974e"]]},{"id":"f8d0d8eaf8f65be5","type":"change","z":"4d4a64331b17df94","name":"Neon","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"light\":{\"brightness_pct\":20,\"rgb_color\":[146,226,255]},\"name\":\"Neon\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":760,"wires":[["89ee6efe31b0974e"]]},{"id":"6ec5b6d85b40c027","type":"change","z":"4d4a64331b17df94","name":"Pinky","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"light\":{\"brightness_pct\":20,\"rgb_color\":[255,182,217]},\"name\":\"Pinky\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":800,"wires":[["89ee6efe31b0974e"]]},{"id":"58e5424e4ab65bcd","type":"change","z":"4d4a64331b17df94","name":"Purple","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"light\":{\"brightness_pct\":20,\"rgb_color\":[200,146,255]},\"name\":\"Purple\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":840,"wires":[["89ee6efe31b0974e"]]},{"id":"ae7d719c71c26ad2","type":"api-call-service","z":"4d4a64331b17df94","name":"Turn Off Hallways","server":"97bbe632.da4568","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.hallway_downstairs, light.hallway_upstairs","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1450,"y":700,"wires":[[]]},{"id":"fdbf8ef383ca632c","type":"server-state-changed","z":"4d4a64331b17df94","name":"Living Room Lights","server":"97bbe632.da4568","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.lr_","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":250,"y":280,"wires":[["ca1814c13a2104e2"]]},{"id":"ca1814c13a2104e2","type":"switch","z":"4d4a64331b17df94","name":"Not changed by NodeRED","property":"data.new_state.context.user_id","propertyType":"msg","rules":[{"t":"neq","v":"1274912dddfa4fd1bdc9c44efb0287d3","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":520,"y":280,"wires":[["485f87d0e25743ab","efcdacf9a6beb41a"]]},{"id":"485f87d0e25743ab","type":"api-call-service","z":"4d4a64331b17df94","name":"Select Unknown","server":"97bbe632.da4568","version":3,"debugenabled":false,"service_domain":"input_select","service":"select_option","entityId":"input_select.tv_lights","data":"{\t    \"option\": \"Unknown\"\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":820,"y":200,"wires":[[]]},{"id":"1b09138e2ce6289e","type":"api-current-state","z":"4d4a64331b17df94","name":"TV Lights Unknown","server":"97bbe632.da4568","version":2,"outputs":2,"halt_if":"Unknown","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_select.tv_lights","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":470,"y":500,"wires":[["556281cbf7e34c81"],[]]},{"id":"556281cbf7e34c81","type":"function","z":"4d4a64331b17df94","name":"Get Random Scene","func":"const globalHomeAssistant = global.get('homeassistant');\nconst ha = globalHomeAssistant.homeAssistant;\n\nconst options = new Set(ha.states['input_select.tv_lights']['attributes']['options']);\noptions.delete(\"Unknown\")\n\nconst optionsArray = Array.from(options);\n\nvar rand = optionsArray[(Math.random() * optionsArray.length) | 0]\n\n//node.warn(rand)\n\nreturn { payload: rand };","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":500,"wires":[["0587c7ea9815c879"]]},{"id":"b104e471edede5d0","type":"server-state-changed","z":"4d4a64331b17df94","name":"Random Scene Trigger","server":"97bbe632.da4568","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.trigger_random_tv_scene","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":500,"y":380,"wires":[["556281cbf7e34c81"],[]]},{"id":"1ee113954a761034","type":"api-call-service","z":"4d4a64331b17df94","name":"Random Scene Trigger Off","server":"97bbe632.da4568","version":3,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.trigger_random_tv_scene","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1040,"y":300,"wires":[[]]},{"id":"efcdacf9a6beb41a","type":"delay","z":"4d4a64331b17df94","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":800,"y":300,"wires":[["1ee113954a761034"]]},{"id":"89ee6efe31b0974e","type":"function","z":"4d4a64331b17df94","name":"Proxy","func":"const i = msg;\n//node.warn(i);\n\nconst response = {\n    \"payload\": i.payload\n};\n\nreturn { payload:  response};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":910,"y":780,"wires":[["6d3a2ef3d1a082e7","5acec8a7ad520457"]]},{"id":"5acec8a7ad520457","type":"api-call-service","z":"4d4a64331b17df94","name":"Set Dropdown","server":"97bbe632.da4568","version":3,"debugenabled":false,"service_domain":"input_select","service":"select_option","entityId":"input_select.tv_lights","data":"{\t   \"option\": msg.payload.payload.name\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":980,"y":660,"wires":[[]]},{"id":"a7ddcde07ad20de4","type":"server-events","z":"4d4a64331b17df94","name":"Living Room/Kitchen Listener","server":"97bbe632.da4568","version":1,"event_type":"deconz_event","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"x":180,"y":60,"wires":[["0d6f276ba5a1e81a"]]},{"id":"0d6f276ba5a1e81a","type":"switch","z":"4d4a64331b17df94","name":"Which remote","property":"payload.event.id","propertyType":"msg","rules":[{"t":"eq","v":"switch_kitchen_wall_left","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":240,"y":160,"wires":[["a0bf2012ceed8ae9"]]},{"id":"a0bf2012ceed8ae9","type":"switch","z":"4d4a64331b17df94","name":"Wall Remote Left","property":"payload.event.event","propertyType":"msg","rules":[{"t":"eq","v":"1002","vt":"str"},{"t":"eq","v":"5002","vt":"str"},{"t":"eq","v":"7002","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":490,"y":160,"wires":[["485f87d0e25743ab"],["485f87d0e25743ab"],["485f87d0e25743ab"]]},{"id":"97bbe632.da4568","type":"server","name":"Home Assistant","version":1,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]