How can I update a switch status on the dashboard

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

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?

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.

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

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.

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?

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

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.

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

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

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.

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??

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

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.

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

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.

1 Like

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?

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.

[{"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.

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

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.

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?