How to dynamically activate a scene based on the state of a helper

I’ve just got an Aqara H1 switch and have create a flow in Node Red to switch on or off lights in two zones in our lounge based on their state using a single click.

Next I was to use the double click action to cycle through scenes. I’ve stored the scenes in a drop-down helper and can obtain the state and progress through the scenes with no issues in Node Red. But I don’t seem to be able to use the state of the helper to activate a scene.

Can anyone advise on the best way to make this work? Thanks in advance :slight_smile:

1 Like

Rather than checking the current state of the helper you should have a state change event node (on the helper) which triggers the desired actions.

Thanks for your reply. It’s not the trigger part that’s a problem, it’s activating the correct scene using the contents of the helper. Roughly this:

WHEN the wall switch receives a double click action

  1. Select the next value from the helper list
  2. Get the newly selected scene frome the helper e.g. scene.lounge_bright
  3. Trigger the scene.lounge_bright scene.

The last part is difficult because the scene is dynamic, I can’t find a way using the scene call service to trigger the specific scene.

Just to provide some further detail, I’ve created a workaround which allows the scenes to be changed based on the helper value, but it isn’t dynamic and is difficult to maintain. The benefit of dynamically using a value from a helper to select the scene is that you only need to maintain the selectable scene list within the helper.

My workaround uses a switch which depended on having knowledge of the values in the switch. It also means that the selectable scenes need to be updated in the switch and a call service node for each possible scene needs to be created.

[{"id":"f1c36b8e18a8b9cb","type":"inject","z":"120358abd7c22d30","name":"fake getting scene from select","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"(\t    $scenes := [\"first\", \"second\", \"third\"];\t    $selectedScene := $shuffle($scenes);\t    {\t        \"target\": {\t            \"entity_id\": [\"scene.\" & $selectedScene[0]]\t        }   \t    }\t)","payloadType":"jsonata","x":368,"y":3600,"wires":[["02a8dfbfe3db28fe"]]},{"id":"02a8dfbfe3db28fe","type":"api-call-service","z":"120358abd7c22d30","name":"","server":"","version":5,"debugenabled":true,"domain":"scene","service":"turn_on","areaId":[],"deviceId":[],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":624,"y":3600,"wires":[[]]},{"id":"644d24549882dc98","type":"inject","z":"120358abd7c22d30","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":308,"y":3664,"wires":[["e82e491ae704cc44"]]},{"id":"e82e491ae704cc44","type":"api-current-state","z":"120358abd7c22d30","name":"","server":"","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_select.scenes","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"{\t    \"target\": {\t        \"entity_id\": [\"scene.\" & $entity().state]\t    }   \t}","valueType":"jsonata"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":540,"y":3664,"wires":[["c982ac754d7140ea"]]},{"id":"c982ac754d7140ea","type":"api-call-service","z":"120358abd7c22d30","name":"","server":"","version":5,"debugenabled":true,"domain":"scene","service":"turn_on","areaId":[],"deviceId":[],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":816,"y":3664,"wires":[[]]}]
2 Likes

Great solution @Kermit, thanks for your help :slight_smile:

That code allows me to just add a new scene to the helper and not touch the Node Red flow which is just what I wanted.

Hey @jamieyates79 any chance you could share you final flow? :grinning: