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;



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.


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?

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.


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.