Restore Switch State after Node-RED restart

Hey guys,

I have a switch that is "on" or "off" and sends this payload to a global variable. If I restart my Node-RED (docker container) the button is often in the wrong state (off) because the persistent nodes for this part wont work? Maybe they are not intended to work this way as I try to use them :slight_smile:

How can I achieve to save my button state over node-red restarts?

You could make a simple script to do it.

#Tell Node-RED to restore state of lights and switches after restart
/usr/bin/curl -k https://localhost:1880/restore-states

Try this flow as an example of restoring the switch state after a reboot;

switch

[{"id":"fcacdc7d.c798a","type":"ui_switch","z":"31e18edc.98bdc2","name":"","label":"switch","tooltip":"","group":"61d2dced.3db8f4","order":4,"width":"5","height":"3","passthru":true,"decouple":"false","topic":"","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"","offvalue":"off","offvalueType":"str","officon":"","offcolor":"","x":590,"y":500,"wires":[["d0d80b69.b06ad8"]]},{"id":"d0d80b69.b06ad8","type":"change","z":"31e18edc.98bdc2","name":"","rules":[{"t":"set","p":"test","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":500,"wires":[[]]},{"id":"dc02a5a1.560988","type":"inject","z":"31e18edc.98bdc2","name":"","topic":"","payload":"test","payloadType":"global","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":410,"y":500,"wires":[["fcacdc7d.c798a"]]},{"id":"61d2dced.3db8f4","type":"ui_group","z":"","name":"flowtest","tab":"3c94630c.13381c","disp":true,"width":"12","collapse":false},{"id":"3c94630c.13381c","type":"ui_tab","z":0,"name":"Test","icon":"dashboard","order":6}]

I would say @Paul-Reed solution looks good. I second trying his solution.

If you use mqtt then keep the switch state in a retained topic in mqtt. Feed an mqtt In node into the switch and it will automatically be restored on startup.

@Paul-Reed

Tested your solution and it looks realy similiar to the one I tryed out with the difference of my persistent nodes around... it just won't work :frowning:

If the switch is turned on and I restart the Node-RED the global variable is lost. The switch will turn off after the restarted Node-RED because it seems to be that all switches should be offline after node-RED restart. The new "off" switch will then fill the new global variable to "off"... inject node won't help.
Will try the retain topic now.

Have you set up a context store to persist context after a reboot?
https://nodered.org/docs/user-guide/context

ah maybe that's why it won't work :smiley:

thats how I tryed it out before asking here... the two links are linked to each other

the mqtt solution works well :slight_smile:

EDIT: enabled the local store but it won't change the behaviour of the switch by itself. Node-RED seems to start a switch always in "off" state which overwrites every global variable that was set before the restart.

But for now I can live with this MQTT workaround :slight_smile: The change node is still needed because I set a variable on putting the switch into any state.

grafik

Are you saying that the switch sends an Off message on startup? If that were so then the MQTT solution would not work either. Conceptually there is no difference between the two. However if you are already using MQTT then I believe that is a better solution. In your position though I would not be happy not understanding why the context solution did not work, and would investigate it further so that I understood it.

Does my example flow work OK for you??
If so, then try to use that as a basis for developing your flow.

Ik ben een nieuwe gebruiker die ook last heeft van dit probleem. Waar kan ik dit script plaatsen. ik gebruik node red op Home Assistant.

Gr Vlak

@vlak please don't post the same question in multiple threads. Seeing how this thread is 7 months old, I will be closing it.

2 Likes