Store state of a LEDstrip and use later on

Hi,

I am struggling with the concepts of node red and having the “state” of an entity to be used later on in the flow.
What I want to achieve. When a LEDstrip is on, no matter what it was doing, 1 colour, brightness, sprinkling, tinkling, rainbowlight :-), I want thát state to be saved in a variable and use that later on to return to.
In my use case I want to assign a button of a remote control, when pressed, check the state of the LEDstrip, save it, perform an optical signal “off-80%on-off-original state”.

Hope I explain clear enough… I have tried this… but I am having trouble to “put” the original payload back into the LEDstrip…


[{"id":"29c0fb3d.715f6c","type":"api-current-state","z":"4c426d0e.6b201c","name":"LedStrip on?","server":"c6b350c5.b8b3c","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.slaapkamer_groot_ledstrip","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1550,"y":920,"wires":[["a4df51f7.0785a"],[]]},{"id":"a871b025.8a4918","type":"api-call-service","z":"4c426d0e.6b201c","name":"Turn off","server":"c6b350c5.b8b3c","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.slaapkamer_groot_ledstrip","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1920,"y":900,"wires":[["7c5c594c.0cb66"]]},{"id":"ede562f7.4ad4a","type":"stoptimer","z":"4c426d0e.6b201c","duration":"2","units":"Second","payloadtype":"num","payloadval":"0","name":"","x":2420,"y":900,"wires":[["ab049498.cde92"],[]]},{"id":"70fc007b.4ee018","type":"api-call-service","z":"4c426d0e.6b201c","name":"Turn on 80%","server":"c6b350c5.b8b3c","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.slaapkamer_groot_ledstrip","data":"{\"entity_id\":\"light.slaapkamer_groot_ledstrip\",\"effect\":\"none\",\"brightness\":\"80\",\"rgb_color\":[255, 255, 255]}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2250,"y":900,"wires":[["ede562f7.4ad4a"]]},{"id":"a4df51f7.0785a","type":"change","z":"4c426d0e.6b201c","name":"flow variable","rules":[{"t":"set","p":"payload","pt":"msg","to":"ledstate","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1770,"y":900,"wires":[["a871b025.8a4918"]]},{"id":"7c5c594c.0cb66","type":"stoptimer","z":"4c426d0e.6b201c","duration":"1","units":"Second","payloadtype":"num","payloadval":"0","name":"","x":2080,"y":900,"wires":[["70fc007b.4ee018"],[]]},{"id":"ab049498.cde92","type":"change","z":"4c426d0e.6b201c","name":"flow variable","rules":[{"t":"set","p":"ledstate","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2590,"y":900,"wires":[["efaabd63.e6453"]]},{"id":"efaabd63.e6453","type":"api-call-service","z":"4c426d0e.6b201c","name":"Turn to flow.ledstate","server":"c6b350c5.b8b3c","version":1,"debugenabled":false,"service_domain":"light","service":"","entityId":"light.slaapkamer_groot_ledstrip","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2780,"y":900,"wires":[[]]},{"id":"c6b350c5.b8b3c","type":"server","z":"","name":"Home Assistant"}]

How can I do this? What am I overthinking?

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/cookbook/saving-and-restoring-states.html

Hello, thank you for this link, tried to import them… off course errors… And I am not sure if this helps me to 'understand" where my thinking is going wrong… can you help me out?

In the service fields of the last call service node use turn_{{payload}}

You can save variables in memory.
Read (or watch the video) here: http://www.steves-internet-guide.com/node-red-variables/

1 Like

Call-service API error. Error Message: Service not found.

Hello, I want to use states to be store in “flow variable” and not use functions please.

Your first change node you’re setting flow from message. You need to swap that around and set the flow variable from message payload

1 Like

like this?


[{"id":"a871b025.8a4918","type":"api-call-service","z":"4c426d0e.6b201c","name":"Turn off","server":"c6b350c5.b8b3c","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.slaapkamer_groot_ledstrip","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2160,"y":1440,"wires":[["7c5c594c.0cb66"]]},{"id":"ede562f7.4ad4a","type":"stoptimer","z":"4c426d0e.6b201c","duration":"2","units":"Second","payloadtype":"num","payloadval":"0","name":"","x":2700,"y":1440,"wires":[["ed4c9170.6cb96","7d11a37.30cafdc","d244e579.23b1"],[]]},{"id":"70fc007b.4ee018","type":"api-call-service","z":"4c426d0e.6b201c","name":"Turn on 80%","server":"c6b350c5.b8b3c","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.slaapkamer_groot_ledstrip","data":"{\"entity_id\":\"light.slaapkamer_groot_ledstrip\",\"effect\":\"none\",\"brightness\":\"80\",\"rgb_color\":[255, 255, 255]}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2490,"y":1440,"wires":[["ede562f7.4ad4a"]]},{"id":"7c5c594c.0cb66","type":"stoptimer","z":"4c426d0e.6b201c","duration":"1","units":"Second","payloadtype":"num","payloadval":"0","name":"","x":2320,"y":1440,"wires":[["70fc007b.4ee018"],[]]},{"id":"64c50a27.7ad19c","type":"debug","z":"4c426d0e.6b201c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":3470,"y":1440,"wires":[]},{"id":"e96e4583.e006b8","type":"ha-get-entities","z":"4c426d0e.6b201c","server":"c6b350c5.b8b3c","name":"","rules":[{"property":"entity_id","logic":"is","value":"light.slaapkamer_groot_ledstrip","valueType":"str"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":1600,"y":1340,"wires":[["332c9cbf.9ac0cc","7ee002d7.a1ea44","82f2e075.ea062","364d1743.d6cda8"]]},{"id":"332c9cbf.9ac0cc","type":"debug","z":"4c426d0e.6b201c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1840,"y":1340,"wires":[]},{"id":"3ada7792.3b387","type":"inject","z":"4c426d0e.6b201c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1380,"y":1480,"wires":[["e96e4583.e006b8"]]},{"id":"7ee002d7.a1ea44","type":"change","z":"4c426d0e.6b201c","name":"flow variable ledstate.effect","rules":[{"t":"set","p":"payload[0].attributes.effect","pt":"msg","to":"ledstate.effect","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1800,"y":1400,"wires":[["a871b025.8a4918"]]},{"id":"82f2e075.ea062","type":"change","z":"4c426d0e.6b201c","name":"flow variable ledstate.brightness","rules":[{"t":"set","p":"payload[0].attributes.brightness","pt":"msg","to":"ledstate.brightness","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1810,"y":1440,"wires":[["a871b025.8a4918"]]},{"id":"364d1743.d6cda8","type":"change","z":"4c426d0e.6b201c","name":"flow variable ledstate.rgbcolour","rules":[{"t":"set","p":"payload[0].attributes.rgb_color","pt":"msg","to":"ledstate.rgbcolor","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1810,"y":1480,"wires":[["a871b025.8a4918"]]},{"id":"782923f6.9e8d8c","type":"api-call-service","z":"4c426d0e.6b201c","name":"","server":"c6b350c5.b8b3c","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.slaapkamer_groot_ledstrip","data":"{\"effect\":{{flow.ledstate.effect}},\"brightness\":{{flow.ledstate.brightness}},\"rgb_color\":{{flow.ledstate.rgbcolor}}}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":3270,"y":1440,"wires":[["64c50a27.7ad19c"]]},{"id":"ed4c9170.6cb96","type":"change","z":"4c426d0e.6b201c","name":"flow variable ledstate.effect around","rules":[{"t":"set","p":"ledstate.effect","pt":"flow","to":"payload[0].attributes.effect","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":3020,"y":1500,"wires":[["782923f6.9e8d8c"]]},{"id":"7d11a37.30cafdc","type":"change","z":"4c426d0e.6b201c","name":"flow variable ledstate.brightness around","rules":[{"t":"set","p":"ledstate.brightness","pt":"flow","to":"payload[0].attributes.brightness","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":3040,"y":1540,"wires":[["782923f6.9e8d8c"]]},{"id":"d244e579.23b1","type":"change","z":"4c426d0e.6b201c","name":"flow variable ledstate.rgbcolour around","rules":[{"t":"set","p":"ledstate.rgbcolor","pt":"flow","to":"payload[0].attributes.rgb_color","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":3040,"y":1580,"wires":[["782923f6.9e8d8c"]]},{"id":"c6b350c5.b8b3c","type":"server","z":"","name":"Home Assistant"}]

And how do I pass the values of them to the call service node?

{"effect":WHATHERE,"brightness":WHATHERE,"rgb_color":WHATHERE}

Doesn’t flow variables get cleared when you restart?
Or that is perhaps not a problem?

no problem.

I had the same issue and for me something like this worked:
{ "brightness": {{para_brightness}}, "rgb_color": [ {{para_red}}, {{para_green}}, {{para_blue}} ]}
where para_* are some vars in the message payload.

No matter what I try or put in the Data field of the call service node, I keep getting:

“Call-service API error. Error Message: expected int for dictionary value @ data[‘brightness’]” (or other, now only trying to pass the brightness value)