# How can I update a switch status on the dashboard

**URL:** <https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989>\
**Category:** Dashboard\
**Created:** [1 September 2019 04:23 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989 "2019-09-01T04:23:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rcbuck](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@rcbuck](https://discourse.nodered.org/u/rcbuck)\
**Post date:** [1 September 2019 04:23 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/1 "2019-09-01T04:23:21Z")

</div>

I am new to Node-RED and have been playing with it for the last few days. I would like a few pointers as to how to proceed with my project.

I have node-red installed on a raspberry pi. I have a client, broker, and dashboard all running on the Pi. Performance is good enough for what I am doing.

I have an ESP32 client running and subscribed to two topics. The ESP32 also publishes to one topic.

The ESP32 has a switch that I can check the status of with the Raspberry Pi. When I check the status, I get the correct response. I also have a relay connected to the ESP32 that I can operate by issuing a command from the Raspberry Pi.

On my Dashboard I have two buttons that pubblish topics to the broker. One topic is the relay and the other is the switch status. The relay works properly but I have an issue with the switch status.

When the status button is clicked, I need it to return the status of the switch to both the dashboard and the Raspberry Pi. For the Pi it should send the same publish message that gets sent when the Raspberry Pi asks for a status upadte. For the dashboard a simple line of text indicating either "Door is closed" or "Door is open" would be good enough. It would also be nice to have a circle that changes from red for closed to green for open. However, that is not really necessary.

I can export my flow and attach it here if someone could tell me how to do that.[MyFlow.txt](https://discourse.nodered.org/uploads/short-url/qjtx618A4EnxBCXyl4MoyVGElbe.txt) (2.2 KB)

I uploaded it as a text file but I don't think that is the proper way to do it.

Thank you.

---

<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:** [1 September 2019 08:28 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/2 "2019-09-01T08:28:17Z")

</div>

> [@rcbuck](#):
>
> When the status button is clicked, I need it to return the status of the switch to both the dashboard and the Raspberry Pi. For the Pi it should send the same publish message that gets sent when the Raspberry Pi asks for a status upadte. For the dashboard a simple line of text indicating either "Door is closed" or "Door is open" would be good enough.

I am not sure I fully understand that. Why do you need a button to request a status update, why does it not automatically update whenever the status changes?

---

<div class="post-metadata">

**Author:** ![rcbuck](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@rcbuck](https://discourse.nodered.org/u/rcbuck)\
**Post date:** [1 September 2019 16:08 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/3 "2019-09-01T16:08:32Z")

</div>

I click the button on the dashboard that publishes a request asking for the status of the switch. If I am subscribed to the topic with the Raspberry Pi client I see the message indicating the status of the switch. I need to see some kind of indication on the dashboard that will tell me the status. I don't know how to do that.

The Raspberry Pi sees the message "Switch open" or "Switch closed". I need to see the same thing on the dashboard.

---

<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:** [1 September 2019 16:17 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/4 "2019-09-01T16:17:29Z")

</div>

Why do you need a button to request it, why not poll it regularly so the flow always knows the number and state?

---

<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:** [1 September 2019 16:29 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/5 "2019-09-01T16:29:54Z")

</div>

In fact I suggest you get the pi to poll it every second (or whatever is appropriate) and publish the state to MQTT. Then the dashboard can be fed that value (via a Change node for the text display) and you won't need the request button at all.

---

<div class="post-metadata">

**Author:** ![rcbuck](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@rcbuck](https://discourse.nodered.org/u/rcbuck)\
**Post date:** [1 September 2019 17:11 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/6 "2019-09-01T17:11:34Z")

</div>

I want to use my cell phone to check the status of the switch from outside my network at random times during the day. I guess I don't really "need" the push button. I could poll every minute or so and update the status on the dashboard. I just thought the push button request would be convenient and eliminate the need to poll.

I think everyone is not understanding my problem. The problem is I never see anything on the dashboard to indicate the status of the switch. I don't know how to get the message to display on the dashboard.

"via a Change node for the text display" How do I get the text to display on the dashboard?

---

<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:** [1 September 2019 17:14 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/7 "2019-09-01T17:14:13Z")

</div>

Have you tried using the dashboard's node called `text`?

---

<div class="post-metadata">

**Author:** ![rcbuck](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@rcbuck](https://discourse.nodered.org/u/rcbuck)\
**Post date:** [1 September 2019 17:50 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/8 "2019-09-01T17:50:10Z")

</div>

Yes. If you import the flow (not sure you can the way I uploaded it) you can see I have a text node on the dashboard. I don't know what to enter in the Value format. Currently I have {{msg.payload}}.

The Raspberry Pi command line client works fine. I issue the command:  
mosquitto\_pub -t 'esp32/door/staus' -m 'null'.  
The ESP32 is subscribed to that topic and checks the status of the switch. The ESP32 then issues command:  
client.publish("esp32/door/stat-return", "Door is open"); -- or "Door is closed" if that is the current state.  
The Raspberry Pi receives the message "Door is open". -- or "Door is closed" if that is the current state.

I need to duplicate that functionality on the dashboard. I think the problem is the dashboard doesn't see the reply from the ESP32. My implementation of the process is somehow wrong.

---

<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:** [1 September 2019 18:07 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/9 "2019-09-01T18:07:16Z")

</div>

Subscribe to that topic and feed it straight into the text node

---

<div class="post-metadata">

**Author:** ![rcbuck](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@rcbuck](https://discourse.nodered.org/u/rcbuck)\
**Post date:** [1 September 2019 18:29 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/10 "2019-09-01T18:29:30Z")

</div>

Do you mean have the button subscribe to that topic? Or an mqtt IN node?

---

<div class="post-metadata">

**Author:** ![rcbuck](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@rcbuck](https://discourse.nodered.org/u/rcbuck)\
**Post date:** [1 September 2019 18:38 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/11 "2019-09-01T18:38:26Z")

</div>

I just noticed that if I request a switch status from the command line, the returned status DOES show up in the text node on the dashboard.

Apparently the status button on the dashboard isn't configured properly to send the request message to the ESP32. If I press the status button on the dashboard the Pi also does not receive an update. I do have the status button subscribe to esp32/door/status and the payload is set to null to duplicate the Pi message.

---

<div class="post-metadata">

**Author:** ![rcbuck](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@rcbuck](https://discourse.nodered.org/u/rcbuck)\
**Post date:** [1 September 2019 18:56 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/12 "2019-09-01T18:56:00Z")

</div>

I confirmed the status button is the problem. If I bring up the serial monitor on the ESP32 and click the status button the esp32/door/status message doesn't arrive at the ESP32. So how to get the status button to publish to the topic esp32/door/status??

---

<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:** [1 September 2019 19:06 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/13 "2019-09-01T19:06:24Z")

</div>

Show us the command line you use to do it and export the flow consisting of the button to the mqtt out node and post that too. See this for how to post a flow

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

**Author:** ![rcbuck](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@rcbuck](https://discourse.nodered.org/u/rcbuck)\
**Post date:** [1 September 2019 19:25 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/14 "2019-09-01T19:25:32Z")

</div>

The command line request is this:  
mosquitto\_pub -t 'esp32/door/stat-return' -m 'null'  
This command will return the switch status to the command line and it also displays in the dashboard text node.

Flow is attached.

```auto
[{"id":"71994e9.b9a4fb","type":"tab","label":"ESP32 Flow","disabled":false,"info":""},{"id":"a2725ee8.01405","type":"debug","z":"71994e9.b9a4fb","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":670,"y":200,"wires":[]},{"id":"6c5b5475.83bc84","type":"ui_button","z":"71994e9.b9a4fb","name":"Status","group":"eb6c6b8d.b6f26","order":2,"width":0,"height":0,"passthru":false,"label":"Status ?","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"esp32/door/status","x":410,"y":120,"wires":[["a2725ee8.01405"]]},{"id":"292b8758.7f9a8","type":"mqtt out","z":"71994e9.b9a4fb","name":"Publish","topic":"esp32/door/button","qos":"2","retain":"false","broker":"c3002906.2a182","x":660,"y":280,"wires":[]},{"id":"77d8c755.715a","type":"ui_button","z":"71994e9.b9a4fb","name":"Door Switch","group":"eb6c6b8d.b6f26","order":3,"width":0,"height":0,"passthru":true,"label":"Operate","tooltip":"","color":"","bgcolor":"","icon":"","payload":"pressed","payloadType":"str","topic":"esp32/door/button","x":390,"y":200,"wires":[["a2725ee8.01405","292b8758.7f9a8"]]},{"id":"f30ecb57.69abd","type":"ui_text","z":"71994e9.b9a4fb","group":"eb6c6b8d.b6f26","order":1,"width":0,"height":0,"name":"Door Status","label":"Door Status","format":"{{msg.payload}}","layout":"row-spread","x":670,"y":120,"wires":[]},{"id":"95f345b6.c0c44","type":"mqtt in","z":"71994e9.b9a4fb","name":"Return Status","topic":"esp32/door/stat-return","qos":"2","datatype":"utf8","broker":"c3002906.2a182","x":390,"y":280,"wires":[["292b8758.7f9a8","f30ecb57.69abd"]]},{"id":"eb6c6b8d.b6f26","type":"ui_group","z":"","name":"Garage Door","tab":"5d370db7.ba1564","disp":true,"width":"6","collapse":false},{"id":"c3002906.2a182","type":"mqtt-broker","z":"","name":"MyPi","broker":"172.16.1.112","port":"1883","clientid":"pi","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"home/Relays","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"5d370db7.ba1564","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![rcbuck](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@rcbuck](https://discourse.nodered.org/u/rcbuck)\
**Post date:** [1 September 2019 19:48 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/15 "2019-09-01T19:48:17Z")

</div>

Success!! I found the problem. I needed to have two mqtt out nodes. One connects to the operate push button and the other one connects to the status push button. Once I did that, everything works as I want it to. The status now shows up on the dashboard when I click the status button. The Pi also receives the status message when the button is clicked.

Thanks to everyone for their suggestions and help, especially to Colin.

---

<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:** [1 September 2019 20:48 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/16 "2019-09-01T20:48:37Z")

</div>

> [@rcbuck](#):
>
> Success!! I found the problem. I needed to have two mqtt out nodes.

Almost certainly you don't. Post the flow you are using and I am sure it can be simplified. Just post the nodes from the buttons through to the mqtt nodes.

**[Edit]** Out of interest, what software are you running in the esp32?

---

<div class="post-metadata">

**Author:** ![rcbuck](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@rcbuck](https://discourse.nodered.org/u/rcbuck)\
**Post date:** [1 September 2019 21:43 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/17 "2019-09-01T21:43:44Z")

</div>

I am running Arduino code on the esp32. I am using \<PubSubClient.h\> for the mqtt functions. I am monitoring two switches and triggering one relay. I am using it on my garage door opener. Sometimes I leave the house and wonder if I closed the door. With my cell phone I can connect through my firewall and check the status of the door.

I will also monitor to see if the beam is broken. I leave the door up during the summer since our temperatures are 100-120 degrees F. This at least keeps the garage at outside temperature. Detecting the beam being broken will set off an alarm connected to the Raspberry Pi inside the house. This will alert me to intruders if there are any, human or animal.

If you are interested I can zip up my ESP32 code and schematic of the hardware that I bread boarded. The beam detect was the most interesting. The beam is a constant 3 msec 12 volt pulse stream. When broken the stream goes to a steady 12 volts. I use a NE555 as a missing pulse detector with a 10 msec timeout. You can't get through the beam in 10 msec. Of course a smart burglar may just step over the beam.

Node code is attached. You can see if there is a more simple way to detect the status switch.

```auto
[{"id":"71994e9.b9a4fb","type":"tab","label":"ESP32 Flow","disabled":false,"info":""},{"id":"6c5b5475.83bc84","type":"ui_button","z":"71994e9.b9a4fb","name":"Status Switch","group":"eb6c6b8d.b6f26","order":2,"width":0,"height":0,"passthru":false,"label":"Status ?","tooltip":"","color":"","bgcolor":"","icon":"","payload":"null","payloadType":"str","topic":"esp32/door/status","x":400,"y":120,"wires":[["58a47caa.389504"]]},{"id":"292b8758.7f9a8","type":"mqtt out","z":"71994e9.b9a4fb","name":"Publish","topic":"esp32/door/button","qos":"2","retain":"false","broker":"c3002906.2a182","x":660,"y":280,"wires":[]},{"id":"77d8c755.715a","type":"ui_button","z":"71994e9.b9a4fb","name":"Door Switch","group":"eb6c6b8d.b6f26","order":3,"width":0,"height":0,"passthru":false,"label":"Operate","tooltip":"","color":"","bgcolor":"","icon":"","payload":"pressed","payloadType":"str","topic":"esp32/door/button","x":390,"y":200,"wires":[["292b8758.7f9a8"]]},{"id":"f30ecb57.69abd","type":"ui_text","z":"71994e9.b9a4fb","group":"eb6c6b8d.b6f26","order":1,"width":0,"height":0,"name":"Door Status","label":"Door Status","format":"{{msg.payload}}","layout":"row-spread","x":670,"y":360,"wires":[]},{"id":"95f345b6.c0c44","type":"mqtt in","z":"71994e9.b9a4fb","name":"Return Status","topic":"esp32/door/stat-return","qos":"2","datatype":"utf8","broker":"c3002906.2a182","x":390,"y":280,"wires":[["f30ecb57.69abd"]]},{"id":"58a47caa.389504","type":"mqtt out","z":"71994e9.b9a4fb","name":"Publish","topic":"esp32/door/status","qos":"2","retain":"false","broker":"c3002906.2a182","x":660,"y":200,"wires":[]},{"id":"eb6c6b8d.b6f26","type":"ui_group","z":"","name":"Garage Door","tab":"5d370db7.ba1564","disp":true,"width":"6","collapse":false},{"id":"c3002906.2a182","type":"mqtt-broker","z":"","name":"MyPi","broker":"172.16.1.112","port":"1883","clientid":"pi","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"home/Relays","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"5d370db7.ba1564","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Edit: Corrected flow posting.

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [1 September 2019 22:07 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/18 "2019-09-01T22:07:18Z")

</div>

Can you export your flow again, and paste it between the backticks as you did before

---

<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:** [2 September 2019 10:21 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/19 "2019-09-02T10:21:43Z")

</div>

If you clear the topic in the Publish nodes you should be able to use just one. It will pick up the topic from the button.

I still don't understand why you need to request the status. If the esp32 were to write the status to MQTT with the Retain flag set every time the door opened or closed then there should be no need to do any more. It would always be up to date and the correct value would appear on the dashboard.

---

<div class="post-metadata">

**Author:** ![rcbuck](https://avatars.discourse-cdn.com/v4/letter/r/7ab992/32.png) [@rcbuck](https://discourse.nodered.org/u/rcbuck)\
**Post date:** [2 September 2019 18:27 UTC](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989/20 "2019-09-02T18:27:33Z")

</div>

A single Publish node doesn't work. The status button Topic is set to esp32/door/status with a Payload string set to null. The single Publish node has no way of knowing it should listen for the publish topic esp32/door/stat-return that comes from the ESP32 when the status button is pressed. That publish topic is sent by the ESP32 when it receives the esp32/door/status topic from the Status pushbutton. If there is a way to do that, let me know how and I will test it.

I know the Retain flag works as I have tested that. The question I have is how is that flag saved? Is it saved to the SD card each time the value changes? If I leave the house and a power failure occurs 30 minutes later, is that value correct when power comes back on the the Pi reboots?

Assume the garage door is open when the power failure occurs. When I connect 2 hours later and read the Retained flag value, will it return a message indicating the door is closed or will the message indicate the door is open?

[Next page](https://discourse.nodered.org/t/how-can-i-update-a-switch-status-on-the-dashboard/14989.md?page=2)
