Using flow context in Call Service in Node-red

I am a beginner with Node-Red and have run into a brick wall trying to find the right approach for using a flow variable in a call service.
Usecase:
I poll the brightness of a light in its previous state and store the brightness in a flow variable. Upon opening a door, the light should go to full brightness. When closing the door, I want to restore the previous brightness. Everything works fine, except the restore of the previous brightness.
Apparently a JSON expression in the call service is not possible to include a flow variable?
Or what do I wrong< What is the right approach?

Thanks for guidance and help

It’s much easier to help if you post what you have.

Thanks for the response. Here is what I have including my test bench. I am not sure yet, if it works reliably

[{"id":"b6a69ab83c88ab3c","type":"server-state-changed","z":"ebb36f824d5bb606","name":"","server":"de5bcb2b.ec2a98","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.pantry_doors_contact","entityidfiltertype":"exact","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":true,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":210,"y":360,"wires":[["05c2956dd1a95898","4e6de1f1f4d6d427"]]},{"id":"bd056577038681a7","type":"inject","z":"ebb36f824d5bb606","name":"timestamp","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"timestamp","payloadType":"msg","x":300,"y":560,"wires":[["05c2956dd1a95898"]]},{"id":"05c2956dd1a95898","type":"api-current-state","z":"ebb36f824d5bb606","name":"","server":"de5bcb2b.ec2a98","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.z_wave_plus_700_series_s2_dimmer_switch_6","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":740,"y":440,"wires":[["8156ce16ee5ef7d8"]]},{"id":"4e6de1f1f4d6d427","type":"switch","z":"ebb36f824d5bb606","name":"Pantry door open","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":570,"y":300,"wires":[["22ebdda6cc559940"],["708b43fa40f6387d"]]},{"id":"8fa32df313a5a3a1","type":"change","z":"ebb36f824d5bb606","name":"Get hallway status when On","rules":[{"t":"set","p":"hallway_brightness","pt":"flow","to":"data.attributes.brightness","tot":"msg"},{"t":"set","p":"hallway_state","pt":"flow","to":"on","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Hallway Status set","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1420,"y":480,"wires":[["163fdb9e7a941128"]]},{"id":"8156ce16ee5ef7d8","type":"switch","z":"ebb36f824d5bb606","name":"Is hallway light on? ","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"else"}],"checkall":"false","repair":true,"outputs":2,"x":1130,"y":480,"wires":[["8fa32df313a5a3a1"],["a644c4084109e93c"]]},{"id":"22ebdda6cc559940","type":"api-call-service","z":"ebb36f824d5bb606","name":"Turn on hallway light","server":"de5bcb2b.ec2a98","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"{\"brightness\":255}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":840,"y":260,"wires":[[]]},{"id":"708b43fa40f6387d","type":"switch","z":"ebb36f824d5bb606","name":"Restore previous state if it was on","property":"hallway_state","propertyType":"flow","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":880,"y":320,"wires":[["b3789f6186d98927"],["ba1b4e10509f0fd8"]]},{"id":"ba1b4e10509f0fd8","type":"api-call-service","z":"ebb36f824d5bb606","name":"Keep off","server":"de5bcb2b.ec2a98","version":3,"debugenabled":true,"service_domain":"light","service":"turn_off","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1180,"y":400,"wires":[["fe3c8049e8d19911"]]},{"id":"996f8c343eb71320","type":"debug","z":"ebb36f824d5bb606","name":"Hallway Light was off","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1620,"y":600,"wires":[]},{"id":"fe3c8049e8d19911","type":"debug","z":"ebb36f824d5bb606","name":"Hallway light kept off","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1560,"y":400,"wires":[]},{"id":"dbac29e2e280dd9b","type":"inject","z":"ebb36f824d5bb606","name":"Read Hallway Light Status","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"hallway_state","payloadType":"flow","x":330,"y":620,"wires":[["0c500916203f6a20"]]},{"id":"0111c0df05f94a3f","type":"inject","z":"ebb36f824d5bb606","name":"Read Hallway Light Brightness","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"hallway_brightness","payloadType":"flow","x":340,"y":680,"wires":[["0c500916203f6a20"]]},{"id":"0c500916203f6a20","type":"debug","z":"ebb36f824d5bb606","name":"Show Hallway Variables","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":640,"wires":[]},{"id":"fb2935c5528f32e0","type":"inject","z":"ebb36f824d5bb606","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":60,"wires":[["c93f4e92d92e8c9a"]]},{"id":"c93f4e92d92e8c9a","type":"api-call-service","z":"ebb36f824d5bb606","name":"","server":"de5bcb2b.ec2a98","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"{\"brightness\":79}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":370,"y":80,"wires":[[]]},{"id":"f8cfadf62ccdc891","type":"debug","z":"ebb36f824d5bb606","name":"Status after turning back on","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1840,"y":280,"wires":[]},{"id":"163fdb9e7a941128","type":"debug","z":"ebb36f824d5bb606","name":"Hallway Variables Set","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1700,"y":480,"wires":[]},{"id":"a644c4084109e93c","type":"change","z":"ebb36f824d5bb606","name":"Get hallway status","rules":[{"t":"set","p":"hallway_brightness","pt":"flow","to":"0","tot":"num"},{"t":"set","p":"hallway_state","pt":"flow","to":"off","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Hallway Status set","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1390,"y":540,"wires":[["996f8c343eb71320"]]},{"id":"a66bf69c5e42510c","type":"change","z":"ebb36f824d5bb606","name":"Door closed","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":300,"wires":[["4e6de1f1f4d6d427","12aa71c73609912d"]]},{"id":"a2f0e7479913cf9c","type":"inject","z":"ebb36f824d5bb606","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":300,"wires":[["a66bf69c5e42510c"]]},{"id":"12aa71c73609912d","type":"debug","z":"ebb36f824d5bb606","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":200,"wires":[]},{"id":"77c1743adbf1a12b","type":"api-call-service","z":"ebb36f824d5bb606","name":"Restore Hallway to previous state","server":"de5bcb2b.ec2a98","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"{ \"brightness\" : {{payload}} }","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1540,"y":280,"wires":[["f8cfadf62ccdc891"]]},{"id":"12841b37ef3e4fb5","type":"change","z":"ebb36f824d5bb606","name":"Door open","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":220,"wires":[["4e6de1f1f4d6d427","12aa71c73609912d"]]},{"id":"8974374df8d955a5","type":"inject","z":"ebb36f824d5bb606","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":220,"wires":[["12841b37ef3e4fb5"]]},{"id":"b3789f6186d98927","type":"function","z":"ebb36f824d5bb606","name":"Set JSON Expression","func":"var hwb = flow.get(\"hallway_brightness\");\nreturn {payload: hwb};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1240,"y":280,"wires":[["77c1743adbf1a12b"]]},{"id":"de5bcb2b.ec2a98","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

I can’t find where you set the flow variable.
But I’m not sure you need it.

[{"id":"b6a69ab83c88ab3c","type":"server-state-changed","z":"ebaa69a9.649708","name":"","server":"de5bcb2b.ec2a98","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.pantry_doors_contact","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":2050,"y":920,"wires":[["22ebdda6cc559940"],[]]},{"id":"22ebdda6cc559940","type":"api-call-service","z":"ebaa69a9.649708","name":"Turn on hallway light","server":"de5bcb2b.ec2a98","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"{\"brightness\":255}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2380,"y":920,"wires":[["e01e80d489b492e6"]]},{"id":"e01e80d489b492e6","type":"switch","z":"ebaa69a9.649708","name":"","property":"data.old_state.state","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":2570,"y":920,"wires":[["41d5fc0641e59114"]]},{"id":"41d5fc0641e59114","type":"ha-wait-until","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","version":0,"outputs":2,"entityId":"binary_sensor.pantry_doors_contact","entityIdFilterType":"exact","property":"state","comparator":"is","value":"off","valueType":"str","timeout":"60","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"data","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":2720,"y":920,"wires":[["b639239bf39fde8d"],["b639239bf39fde8d"]]},{"id":"b639239bf39fde8d","type":"api-call-service","z":"ebaa69a9.649708","name":"Turn on hallway light","server":"de5bcb2b.ec2a98","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"{\"brightness\": msg.data.old_state.attributes.brightness}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2940,"y":920,"wires":[[]]},{"id":"de5bcb2b.ec2a98","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

We can just wait for the door to close.

I set a timeout on 60 seconds, change it to what you find appropriate.

Thanks for this suggestion. I love the simplicity. Unfortunately it does not work as expected.
Once you close the door it turns the lights off, because the old-state it reads was “off”, since the proposed flow checks for the state of the door contact, not the light.
The plan is that I read the current state and brightness of the light when the door contact is used. And when the door is closed, I want to restore this state for the light.

Oh…
Just add a current state node.

[{"id":"93b38e46fcefe053","type":"api-current-state","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.z_wave_plus_700_series_s2_dimmer_switch_6","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":680,"y":680,"wires":[["7d8074f75f2cac03"]]},{"id":"63ab6fdabb7cde21","type":"server-state-changed","z":"ebaa69a9.649708","name":"","server":"653d2e5cb185cfb8","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.pantry_doors_contact","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":210,"y":680,"wires":[["93b38e46fcefe053"],[]]},{"id":"7d8074f75f2cac03","type":"api-call-service","z":"ebaa69a9.649708","name":"Turn on hallway light","server":"653d2e5cb185cfb8","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"{\"brightness\":255}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1060,"y":680,"wires":[["d65a390b1aa82786"]]},{"id":"d65a390b1aa82786","type":"switch","z":"ebaa69a9.649708","name":"","property":"data.old_state.state","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1250,"y":680,"wires":[["9fd759e0fc8c60e1"]]},{"id":"9fd759e0fc8c60e1","type":"ha-wait-until","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","version":0,"outputs":2,"entityId":"binary_sensor.pantry_doors_contact","entityIdFilterType":"exact","property":"state","comparator":"is","value":"off","valueType":"str","timeout":"60","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"data","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":1400,"y":680,"wires":[["f6920e1e76fdab3f"],["f6920e1e76fdab3f"]]},{"id":"f6920e1e76fdab3f","type":"api-call-service","z":"ebaa69a9.649708","name":"Turn on hallway light","server":"653d2e5cb185cfb8","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"{\"brightness\": msg.data.old_state.attributes.brightness}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1620,"y":680,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"653d2e5cb185cfb8","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

But what do you want it to do if the light was off when the door opened?

If the light was off, then I want to turn it off again, after the pantry door is closed

I’m currently in bed now so I can’t update the sequence, but add another option in the switch node, off and add a call service to light off.
That should be it.

Excellent. I will give that a try and report back
Thanks again

I finally got to test and resolve this issue.
Firstly I made the mistake and forgot that I had conflicting old automations in HA, that had conflicting behavior.
After I removed them, it became easier to fix the errors.
Secondly I realized that your flow was checking the previous state of the door sensor, not the light. I fell back to the flow variable. Below is my working code. But for learning I would appreciate any feedback to the sequence below.
I would love to learn and get feedback on how to simplify this sequence.

Thank for feedback to simplify in advance

[{"id":"ebb36f824d5bb606","type":"tab","label":"Hallway Back ","disabled":false,"info":""},{"id":"5abbe1c83e321852","type":"server-state-changed","z":"ebb36f824d5bb606","name":"Pantry Door Moves","server":"de5bcb2b.ec2a98","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.pantry_doors_contact","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":190,"y":460,"wires":[["21cbd923b99a4765","f6c72559a5708de1"],["f6c72559a5708de1"]]},{"id":"21cbd923b99a4765","type":"api-current-state","z":"ebb36f824d5bb606","name":"Get Hallway Light Status","server":"de5bcb2b.ec2a98","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.hallway_back","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"milliseconds","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":550,"y":320,"wires":[["271672fc4eb5fd2e"],["c2c7ea636c048186"]]},{"id":"f6c72559a5708de1","type":"switch","z":"ebb36f824d5bb606","name":"Pantry door open","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":470,"y":480,"wires":[["85bb8ffd85f57e1b"],["11d2b72647497486"]]},{"id":"271672fc4eb5fd2e","type":"change","z":"ebb36f824d5bb606","name":"Set variables","rules":[{"t":"set","p":"hallway_brightness","pt":"flow","to":"data.attributes.brightness","tot":"msg","dc":true},{"t":"set","p":"hallway_state","pt":"flow","to":"on","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Hallway Status set","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1030,"y":300,"wires":[["31fe05f0c18b0c76"]]},{"id":"85bb8ffd85f57e1b","type":"api-call-service","z":"ebb36f824d5bb606","name":"Turn on hallway light","server":"de5bcb2b.ec2a98","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.hallway_back","data":"{\"brightness\":255}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"entity state","valueType":"str"},{"property":"Data","propertyType":"msg","value":"event data","valueType":"str"}],"queue":"none","x":740,"y":440,"wires":[[]]},{"id":"11d2b72647497486","type":"switch","z":"ebb36f824d5bb606","name":"What was the previous state state?","property":"hallway_state","propertyType":"flow","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":780,"y":500,"wires":[["bff5f0c2d2214d74"],["c899660e0b3b35c1"]]},{"id":"c899660e0b3b35c1","type":"api-call-service","z":"ebb36f824d5bb606","name":"Keep off","server":"de5bcb2b.ec2a98","version":3,"debugenabled":true,"service_domain":"light","service":"turn_off","entityId":"light.hallway_back","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1040,"y":520,"wires":[[]]},{"id":"0e860e661af8891e","type":"debug","z":"ebb36f824d5bb606","name":"Hallway Light was off","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1320,"y":340,"wires":[]},{"id":"986df7fd089c8644","type":"debug","z":"ebb36f824d5bb606","name":"Status after turning back on","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1700,"y":460,"wires":[]},{"id":"31fe05f0c18b0c76","type":"debug","z":"ebb36f824d5bb606","name":"Hallway Variables Set","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1320,"y":300,"wires":[]},{"id":"c2c7ea636c048186","type":"change","z":"ebb36f824d5bb606","name":"Reset variables","rules":[{"t":"set","p":"hallway_brightness","pt":"flow","to":"0","tot":"num"},{"t":"set","p":"hallway_state","pt":"flow","to":"off","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Hallway Status set","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1040,"y":340,"wires":[["0e860e661af8891e"]]},{"id":"c3f47019f20f921b","type":"api-call-service","z":"ebb36f824d5bb606","name":"Restore Hallway to previous state","server":"de5bcb2b.ec2a98","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.hallway_back","data":"{ \"brightness\" : {{payload}} }","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1400,"y":460,"wires":[["986df7fd089c8644"]]},{"id":"bff5f0c2d2214d74","type":"function","z":"ebb36f824d5bb606","name":"Set JSON Expression","func":"var hwb = flow.get(\"hallway_brightness\");\nreturn {payload: hwb};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1120,"y":460,"wires":[["c3f47019f20f921b"]]},{"id":"01d945857e25574a","type":"inject","z":"ebb36f824d5bb606","name":"timestamp","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"timestamp","payloadType":"msg","x":180,"y":280,"wires":[["21cbd923b99a4765"]]},{"id":"12d23295518f9ab8","type":"inject","z":"ebb36f824d5bb606","name":"Read Hallway Light Status","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"hallway_state","payloadType":"flow","x":270,"y":140,"wires":[["62c26a730b86b124"]]},{"id":"ef227b8ea2bfa27b","type":"inject","z":"ebb36f824d5bb606","name":"Read Hallway Light Brightness","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"hallway_brightness","payloadType":"flow","x":280,"y":200,"wires":[["62c26a730b86b124"]]},{"id":"62c26a730b86b124","type":"debug","z":"ebb36f824d5bb606","name":"Show Hallway Variables","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":160,"wires":[]},{"id":"de5bcb2b.ec2a98","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

I see I made a mistake there.

Try this instead:

[{"id":"9fd759e0fc8c60e1","type":"ha-wait-until","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","version":0,"outputs":2,"entityId":"binary_sensor.pantry_doors_contact","entityIdFilterType":"exact","property":"state","comparator":"is","value":"off","valueType":"str","timeout":"60","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"data","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":1400,"y":660,"wires":[["f6920e1e76fdab3f"],["f6920e1e76fdab3f"]]},{"id":"7d8074f75f2cac03","type":"api-call-service","z":"ebaa69a9.649708","name":"Turn on hallway light","server":"653d2e5cb185cfb8","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"{\"brightness\":255}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1220,"y":660,"wires":[["9fd759e0fc8c60e1"]]},{"id":"f6920e1e76fdab3f","type":"api-call-service","z":"ebaa69a9.649708","name":"Turn on hallway light","server":"653d2e5cb185cfb8","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"{\"brightness\": msg.data_light.old_state.attributes.brightness}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1620,"y":660,"wires":[[]]},{"id":"d65a390b1aa82786","type":"switch","z":"ebaa69a9.649708","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1030,"y":700,"wires":[["7d8074f75f2cac03"],["08474a02eec6ee14"]]},{"id":"93b38e46fcefe053","type":"api-current-state","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.z_wave_plus_700_series_s2_dimmer_switch_6","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data_light","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":700,"y":700,"wires":[["d65a390b1aa82786"]]},{"id":"08474a02eec6ee14","type":"api-call-service","z":"ebaa69a9.649708","name":"Turn on hallway light","server":"653d2e5cb185cfb8","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"{\"brightness\":255}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1220,"y":740,"wires":[["1b92690eabe22e7d"]]},{"id":"63ab6fdabb7cde21","type":"server-state-changed","z":"ebaa69a9.649708","name":"","server":"653d2e5cb185cfb8","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.pantry_doors_contact","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":230,"y":700,"wires":[["93b38e46fcefe053"],[]]},{"id":"1b92690eabe22e7d","type":"ha-wait-until","z":"ebaa69a9.649708","name":"","server":"4bbca37b.1700ec","version":0,"outputs":2,"entityId":"binary_sensor.pantry_doors_contact","entityIdFilterType":"exact","property":"state","comparator":"is","value":"off","valueType":"str","timeout":"60","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"data","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":1400,"y":740,"wires":[["072456d188dc88ec"],["072456d188dc88ec"]]},{"id":"072456d188dc88ec","type":"api-call-service","z":"ebaa69a9.649708","name":"Turn off hallway light","server":"653d2e5cb185cfb8","version":3,"debugenabled":true,"service_domain":"light","service":"turn_off","entityId":"light.z_wave_plus_700_series_s2_dimmer_switch_6","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1620,"y":740,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"653d2e5cb185cfb8","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Still not working, I am getting an API error:
1/31/2022, 9:21:16 AMnode: Turn on hallway lightmsg :

object

domain: “light”

service: “turn_on”

data: object

0: “{”

1: “”"

2: “b”

3: “r”

4: “i”

5: “g”

6: “h”

7: “t”

8: “n”

9: “e”

10: “s”

11: “s”

12: “”"

13: “:”

14: " "

15: “d”

16: “a”

17: “t”

18: “a”

19: “_”

20: “l”

21: “i”

22: “g”

23: “h”

24: “t”

25: “.”

26: “a”

27: “t”

28: “t”

29: “r”

30: “i”

31: “b”

32: “u”

33: “t”

34: “e”

35: “s”

36: “.”

37: “b”

38: “r”

39: “i”

40: “g”

41: “h”

42: “t”

43: “n”

44: “e”

45: “s”

46: “s”

47: “}”

entity_id: “light.hallway_back”

1/31/2022, 9:21:16 AMnode: Turn on hallway lightmsg : string[54]

“Call-service error. extra keys not allowed @ data[‘0’]”

I use scenes for this type of automation where I want to restore a state.

[{"id":"f751dc8b1e892291","type":"api-call-service","z":"c99cbbb8e48d5306","name":"","server":"","version":3,"debugenabled":false,"service_domain":"scene","service":"create","entityId":"","data":"{\"scene_id\":\"door_light\",\"snapshot_entities\":[\"light.lefty\",\"light.righty\"]}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":530,"y":480,"wires":[["6f498aedde3e5890"]]},{"id":"6f498aedde3e5890","type":"api-call-service","z":"c99cbbb8e48d5306","name":"turn light bright","server":"","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lefty, light.righty","data":"{\"brightness\":255}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":780,"y":480,"wires":[[]]},{"id":"5524088cc0152df9","type":"server-state-changed","z":"c99cbbb8e48d5306","name":"door open","server":"","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"","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":300,"y":480,"wires":[["f751dc8b1e892291"]]},{"id":"9dc790186d16ea7f","type":"server-state-changed","z":"c99cbbb8e48d5306","name":"door closed","server":"","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"","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":620,"wires":[["233b3d100b0cb971"]]},{"id":"233b3d100b0cb971","type":"api-call-service","z":"c99cbbb8e48d5306","name":"","server":"","version":3,"debugenabled":false,"service_domain":"scene","service":"turn_on","entityId":"scene.door_light","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":530,"y":620,"wires":[[]]},{"id":"867659718a0def2c","type":"inject","z":"c99cbbb8e48d5306","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":420,"wires":[["f751dc8b1e892291"]]},{"id":"4a9c4ad99e7b5d71","type":"inject","z":"c99cbbb8e48d5306","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":560,"wires":[["233b3d100b0cb971"]]}]

Wow, simple and effective. Thank you Mikefila

Quick clarifying question: I realized that the Node-Red automation are sometimes not working, almost like they do not exist. I cannot put a finger on the resolution yet, but I believe they start working again specifically when I open None-Red on any device in Homeassistant.
Any idea why that is the case?
Thanks

The call service nodes in the options has a check box for show debug. When you see some thing didn’t work look at that call service node. The message under the node will tell you if it was nodered or the service call is not being executed by HA.

I figured out that something must have been reset when upgrading the the latest Homeassistand and Node-red version.
I am now on Node-red 2.2.1 and Homeassistand 2022.2.9.
During the upgrade some data was lost. The scene creation is now coming back with an API error: “Call-service error. extra keys not allowed @ data[‘device_id’]”. I could not see anything being wrong with the code below. Has something changed in the syntax for call service node and scene.create, which I did not get correctly fixed:

[{"id":"5fd20dc5396e0d29","type":"server-state-changed","z":"ebb36f824d5bb606","name":"door open","server":"de5bcb2b.ec2a98","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.pantry_doors_contact","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"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":120,"y":140,"wires":[["0f7c8bea525fef4d"],[]]},{"id":"de5bcb2b.ec2a98","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]