# Set Initial State of Dashboard Switch

**URL:** <https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913>\
**Category:** Dashboard\
**Created:** [4 July 2019 15:38 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913 "2019-07-04T15:38:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [4 July 2019 15:38 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/1 "2019-07-04T15:38:09Z")

</div>

Guys, i have just started using the Dashboard Switch component and would like to be able to set its iniitial state after a Node-Red restart/reload/reboot to the same as the real world device that it is controlling. Likewise if the Arduino that is physically attached to the relay is restarted i would like it to transmit the new state back to the NR instance and for that to be reflected in the switch

Is it possible to do this ?

Craig

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [4 July 2019 15:44 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/2 "2019-07-04T15:44:03Z")

</div>

If you look at the `info` panel on the right hand side when you have clicked on a dashboard switch node. You will read the following

```auto
The widget can be updated by an incoming msg.payload with the specified values, that must also match the type of the input (number, string, etc).

```

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [4 July 2019 15:46 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/3 "2019-07-04T15:46:38Z")

</div>

Yep just saw that as i had the Debug window selected as was not seeing the node info - also found this thread - time to start playing around and seeing how this works

> [@Status dashboard switch](https://discourse.nodered.org/t/status-dashboard-switch/573):
>
> Good afternoon! There is a dashboard on the NodeRed, which displays the sensors. Now there was a need for a switch, I decided to try espEasy on sonoff on MQTT, everything works, but the problem is that the status of the switch on the dashboard is not updated if I switch through another application from the smartphone. From the phone I publish: / nameSonoff / gpio / 12 Two messages are published: / nameSonoff / gpio / 12, / nameSonoff / relay / Switch How do I understand / nameSonoff / relay …

Thanks for the quick response

Craig

---

<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:** [4 July 2019 18:41 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/4 "2019-07-04T18:41:16Z")

</div>

The way I handle such issues is to have a Retained topic in MQTT which represents the current status. Then on startup that value can be sent to the device and also initiates the display state of the dashboard switch. The output of the dashboard switch does not need to directly control the device, it just sets the value in MQTT, then the same nodes that pick up the state on startup will pick it up and send it to the device.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [4 July 2019 19:03 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/5 "2019-07-04T19:03:20Z")

</div>

If you are using presistable context and you are storing/holding states in Node-RED, you can use `ui_control` node.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b25dc8e4a85a458226ff0d114b2798072222df4c.png)

Listening the event of dashboard connection allow you to feed stored state to switch or button nodes and your widgets are always "up to date" at the moment you are opened the dashboard page. Included reloads and deploy events.

---

<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:** [4 July 2019 19:12 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/6 "2019-07-04T19:12:09Z")

</div>

Just to point out that it is not necessary to do that if you get the value from MQTT as it will always be set correctly.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [4 July 2019 19:25 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/7 "2019-07-04T19:25:24Z")

</div>

Absolutely. Just the MQTT is one of many possible communication protocols you can use with Node-RED and it really depends on project if it is reasonable choice to use or in some cases just include for state management. So I pointed out the other way and I think it is not bad thing to do so.

---

<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:** [4 July 2019 21:03 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/8 "2019-07-04T21:03:26Z")

</div>

> [@hotNipi](#):
>
> So I pointed out the other way and I think it is not bad thing to do so

Agreed, there are often many ways to do things.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [16 December 2020 22:00 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/9 "2020-12-16T22:00:12Z")

</div>

@hotNipi would you be able to expound on how to use the UI control node for my challange?

I have a UI switch node which can get it's state altered via an external input (433mhz) message, and also be controlled by the UI. Upon re-deploy or reboot, the state of that UI switch is lost, thus I wish to deploy a method to "remember" the state of the UI switch, then reset it (if necessary) to the state it was in prior to the reboot etc. Currently my UI switch is using a change node to set a flow.xxx and is storing hat in Persistable file storage

![savestate](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/ef72f82183447c53e54d0ae930c35e262725d73f.png)

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [16 December 2020 22:08 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/10 "2020-12-16T22:08:47Z")

</div>

The ui\_control node has ability to fire an event when dashboard connects to server. Connect it to the change node to retrieve last known state and send that state to the switch node.

---

<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:** [17 December 2020 11:16 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/11 "2020-12-17T11:16:17Z")

</div>

This appears to be continuation of your [previous thread](https://discourse.nodered.org/t/have-objects-in-msg-need-to-take-actions-based-on-one-of-the-payload-codes/37451). I thought we had a solution that did what you want. There is no point switching to an existing thread that is a year old. Please continue on that old thread or, since the thread title is not relevant to this issue then start a new thread with a relevant title. However, if you do that then start by posting the flow you currently have and explain what about it does not work.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [17 December 2020 13:18 UTC](https://discourse.nodered.org/t/set-initial-state-of-dashboard-switch/12913/12 "2020-12-17T13:18:00Z")

</div>

Understood, but as I worked more on the solution that you have guided me with...I am finding unreliability issues. Thus I continued my investigation. I posted what I believe to be my biggest challenge with the reset switch state back in the other thread earlier. Thank you!
