# Restore Switch State after Node-RED restart

**URL:** <https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608>\
**Category:** Dashboard\
**Created:** [11 October 2019 18:05 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608 "2019-10-11T18:05:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [11 October 2019 18:05 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/1 "2019-10-11T18:05:30Z")

</div>

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 🙂

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

---

<div class="post-metadata">

**Author:** ![miker256](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/miker256/32/13042_2.png) [@miker256](https://discourse.nodered.org/u/miker256)\
**Post date:** [11 October 2019 18:13 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/2 "2019-10-11T18:13:27Z")

</div>

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](https://localhost:1880/restore-states)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [11 October 2019 18:33 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/3 "2019-10-11T18:33:36Z")

</div>

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

![switch](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f4d0cd00a472f455b6ad0282f34edf2d74e97e2c.jpeg)

```auto
[{"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}]

```

---

<div class="post-metadata">

**Author:** ![miker256](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/miker256/32/13042_2.png) [@miker256](https://discourse.nodered.org/u/miker256)\
**Post date:** [11 October 2019 21:21 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/4 "2019-10-11T21:21:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 October 2019 06:08 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/5 "2019-10-12T06:08:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [12 October 2019 14:58 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/6 "2019-10-12T14:58:54Z")

</div>

@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 ☹

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.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [12 October 2019 15:08 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/7 "2019-10-12T15:08:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nicedevil](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@nicedevil](https://discourse.nodered.org/u/nicedevil)\
**Post date:** [12 October 2019 15:13 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/8 "2019-10-12T15:13:09Z")

</div>

ah maybe that's why it won't work 😃

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

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/556e00f8ebe6afd048e08b67bce51542fe068b4f.png)

the mqtt solution works well 🙂

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 🙂 The change node is still needed because I set a variable on putting the switch into any state.

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/458ca298feade530c5492c52a5497b4dc7f3e0a6.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 October 2019 15:56 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/9 "2019-10-12T15:56:46Z")

</div>

> [@nicedevil](#):
>
> Node-RED seems to start a switch always in "off" state which overwrites every global variable that was set before the restart.

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.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [12 October 2019 18:31 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/10 "2019-10-12T18:31:52Z")

</div>

> [@nicedevil](#):
>
> enabled the local store but it won't change the behaviour of the switch by itself.

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

---

<div class="post-metadata">

**Author:** ![Vlak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vlak/32/26888_2.png) [@Vlak](https://discourse.nodered.org/u/Vlak)\
**Post date:** [27 April 2020 21:05 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/11 "2020-04-27T21:05:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [27 April 2020 23:08 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/12 "2020-04-27T23:08:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [27 April 2020 23:08 UTC](https://discourse.nodered.org/t/restore-switch-state-after-node-red-restart/16608/13 "2020-04-27T23:08:15Z")

</div>


