Set Initial State of Dashboard Switch

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 ?


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

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).

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

Thanks for the quick response


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.

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

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.

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.

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.

Agreed, there are often many ways to do things.