Using switch node to show actual state

Using elements of the switch node example - Examples of Colour change button and switch, I've updated my relayboard dashboard, so that the actual, not the expected status is shown by the switch.

  • Flicking a switch sends a MQTT msg to the (ESP8266) Relayboard which energises a relay.
  • 100ms later, the flow sends a 'status' MQTT request of the relayboard.
  • The 'status' msg response is received by the flow, which is saved to flow context, and also changes the icon colour to indicate the 'real' status.

Without this, if communication failed between client & broker, the relay would not be energised, whilst the dashboard switch would indicate otherwise.

Further thought to be given to the benefits of using buttons instead of switches.


switch

6 Likes

Would you mind sharing the flow?

I'm no longer using this flow, but this is from my archive -

[{"id":"393b4f1.83476b","type":"mqtt out","z":"6eb37a1d.ee8f34","name":"request status","topic":"relayboard","qos":"2","retain":"false","broker":"3ecd7f11.4d3dd","x":850,"y":210,"wires":[]},{"id":"be1306ae.f74358","type":"mqtt in","z":"6eb37a1d.ee8f34","name":"","topic":"relayboard/status","qos":"2","broker":"3ecd7f11.4d3dd","x":100,"y":270,"wires":[["ac77d09d.c76a4"]]},{"id":"e99b35c.756f9c8","type":"change","z":"6eb37a1d.ee8f34","name":"Write to flow context","rules":[{"t":"set","p":"relayStat","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":270,"wires":[[]]},{"id":"ac77d09d.c76a4","type":"json","z":"6eb37a1d.ee8f34","name":"","property":"payload","action":"","pretty":false,"x":330,"y":270,"wires":[["e99b35c.756f9c8","542cbed8.7c85c"]]},{"id":"4cc56b37.5a7994","type":"inject","z":"6eb37a1d.ee8f34","name":"Restore","topic":"","payload":"relayStat","payloadType":"flow","repeat":"","crontab":"","once":true,"onceDelay":"1","x":160,"y":90,"wires":[["542cbed8.7c85c"]]},{"id":"83a4cac0.49afd8","type":"ui_switch","z":"6eb37a1d.ee8f34","name":"","label":"Relay 1","tooltip":"","group":"78c14ac3.283354","order":1,"width":"4","height":"2","passthru":false,"decouple":"true","topic":"relayboard/relay1","style":"","onvalue":"on","onvalueType":"str","onicon":"fa-bolt fa-5x","oncolor":"red","offvalue":"off","offvalueType":"str","officon":"fa-power-off fa-5x","offcolor":"green","x":343,"y":100,"wires":[["5e0ca9b6.073418"]]},{"id":"5fdfe712.58fed8","type":"ui_switch","z":"6eb37a1d.ee8f34","name":"","label":"Relay 2","tooltip":"","group":"78c14ac3.283354","order":2,"width":"4","height":"2","passthru":false,"decouple":"true","topic":"relayboard/relay2","style":"","onvalue":"on","onvalueType":"str","onicon":"fa-bolt fa-5x","oncolor":"red","offvalue":"off","offvalueType":"str","officon":"fa-power-off fa-5x","offcolor":"green","x":343,"y":154,"wires":[["da3680c4.fad65"]]},{"id":"542cbed8.7c85c","type":"function","z":"6eb37a1d.ee8f34","name":"Parse state json","func":"// Restore status after a re-deploy/reboot and\n// update switch status\nvar relay1 = {payload: (msg.payload.status.relay1 ? \"on\" : \"off\")};\nvar relay2 = {payload: (msg.payload.status.relay2 ? \"on\" : \"off\")};\nvar relay3 = {payload: (msg.payload.status.relay3 ? \"on\" : \"off\")};\nreturn [relay1, relay2, relay3];","outputs":3,"noerr":0,"x":170,"y":154,"wires":[["83a4cac0.49afd8"],["5fdfe712.58fed8"],["453a7cec.97d4d4"]]},{"id":"453a7cec.97d4d4","type":"ui_switch","z":"6eb37a1d.ee8f34","name":"","label":"Relay 3","tooltip":"","group":"78c14ac3.283354","order":3,"width":"4","height":"2","passthru":false,"decouple":"true","topic":"relayboard/relay3","style":"","onvalue":"on","onvalueType":"str","onicon":"fa-bolt fa-5x","oncolor":"red","offvalue":"off","offvalueType":"str","officon":"fa-power-off fa-5x","offcolor":"green","x":343,"y":210,"wires":[["632257e3.b18908","d020f8d6.e4d478"]]},{"id":"632257e3.b18908","type":"change","z":"6eb37a1d.ee8f34","name":"Change Status","rules":[{"t":"set","p":"payload","pt":"msg","to":"status","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":210,"wires":[["faf4d333.d7a3f"]]},{"id":"faf4d333.d7a3f","type":"delay","z":"6eb37a1d.ee8f34","name":"Delay","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":700,"y":210,"wires":[["393b4f1.83476b"]]},{"id":"d020f8d6.e4d478","type":"mqtt out","z":"6eb37a1d.ee8f34","name":"control relay","topic":"","qos":"2","retain":"false","broker":"3ecd7f11.4d3dd","x":570,"y":100,"wires":[]},{"id":"dc135f34.a3d11","type":"comment","z":"6eb37a1d.ee8f34","name":"RELAYBOARD","info":"","x":160,"y":40,"wires":[]},{"id":"47663be9.340164","type":"ui_button","z":"6eb37a1d.ee8f34","name":"All Off","group":"78c14ac3.283354","order":5,"width":"2","height":"1","passthru":false,"label":"All Off","tooltip":"","color":"green","bgcolor":"#2A2A2A","icon":"","payload":"off","payloadType":"str","topic":"relayboard/all","x":750,"y":100,"wires":[["8a8dcfde.be33f"]]},{"id":"cc64eb9c.5a7088","type":"ui_button","z":"6eb37a1d.ee8f34","name":"All On","group":"78c14ac3.283354","order":4,"width":"2","height":"1","passthru":false,"label":"All On","color":"red","bgcolor":"#2A2A2A","icon":"","payload":"on","payloadType":"str","topic":"relayboard/all","x":750,"y":150,"wires":[["e338452.1c565b8"]]},{"id":"da3680c4.fad65","type":"link out","z":"6eb37a1d.ee8f34","name":"","links":["4f6ad260.ba944c"],"x":448,"y":155,"wires":[]},{"id":"4f6ad260.ba944c","type":"link in","z":"6eb37a1d.ee8f34","name":"","links":["da3680c4.fad65","8a8dcfde.be33f","e338452.1c565b8","5e0ca9b6.073418","915ca260.894b9","95c25ab1.ef0458"],"x":515,"y":160,"wires":[["d020f8d6.e4d478","632257e3.b18908"]]},{"id":"8a8dcfde.be33f","type":"link out","z":"6eb37a1d.ee8f34","name":"","links":["4f6ad260.ba944c"],"x":845,"y":100,"wires":[]},{"id":"e338452.1c565b8","type":"link out","z":"6eb37a1d.ee8f34","name":"","links":["4f6ad260.ba944c"],"x":845,"y":150,"wires":[]},{"id":"5e0ca9b6.073418","type":"link out","z":"6eb37a1d.ee8f34","name":"","links":["4f6ad260.ba944c"],"x":448,"y":100,"wires":[]},{"id":"3ecd7f11.4d3dd","type":"mqtt-broker","z":"","name":"paul_broker","broker":"192.168.1.8","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"78c14ac3.283354","type":"ui_group","z":"","name":"Remote relay","tab":"225049e7.1f19a6","disp":true,"width":"4","collapse":false},{"id":"225049e7.1f19a6","type":"ui_tab","z":"","name":"ESP devices","icon":"dashboard","order":10}]

Hi Paul.

One things I may suggest to add:
As it is the button is pressed. It sends a MQTT request.
A reply is sent when the relay is activated and the button changes colour.

I know this is more a real world problem I am about to mention, but I think it can still happen with screen buttons.
Did I actually press the button?
So you press it again.

Doing that will negate the actual turn on request.

Idea:
When the button is pressed, (say it is showing RED) make it acknowledge the press by turning ORANGE (or some other colour) then when it actually happens, it turns GREEN.

That way it is an immediate feedback to the user that the button has been pressed.

Thanks @Trying_to_learn a good idea, but...

Maybe something for @tangtadang to further develop

Hi Paul, what are you using instead of this flow to show state? newbie here struggling with this.

It's not really possible to share my current flow, because it's heavily integrated into MQTT, and will not demo without a response from the remote devices, but this mini flow may give you some ideas.
I'm not using a direct link between output & input (as in this flow) but the feedback is provided by the remote device as a way of confirming that the command has been received and actioned.

[{"id":"98af66a2.07ef48","type":"function","z":"1154e329.a14a8d","name":"Update icon","func":"if (msg.payload == true) {msg.colour = '#fc0303';}\nelse {msg.colour = '#545454';}\nreturn msg;","outputs":1,"noerr":0,"x":270,"y":1230,"wires":[["694039b0.5b7368"]]},{"id":"260d5981.16ba86","type":"function","z":"1154e329.a14a8d","name":"flip-flop","func":"if (msg.payload === true) {\n   var status = flow.get(\"enabled_status\") || \"off\";\n   if (status === \"on\") {\n       msg.payload = false;\n       flow.set(\"enabled_status\", \"off\");\n   } else if (status === \"off\") {\n       msg.payload = true;\n       flow.set(\"enabled_status\", \"on\");\n   }\n}\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":1230,"wires":[["98af66a2.07ef48"]]},{"id":"694039b0.5b7368","type":"ui_button","z":"1154e329.a14a8d","name":"Icon - enabled","group":"860dfdb6.a5ac4","order":5,"width":1,"height":1,"passthru":false,"label":"","tooltip":"","color":"{{msg.colour}}","bgcolor":"#2a2a2a","icon":"fa-plug fa-3x","payload":"true","payloadType":"bool","topic":"","x":450,"y":1230,"wires":[["260d5981.16ba86"]]},{"id":"860dfdb6.a5ac4","type":"ui_group","z":0,"name":"Shelly 1pm","tab":"594f4bb7.074f34","order":1,"disp":true,"width":"6","collapse":true},{"id":"594f4bb7.074f34","type":"ui_tab","z":0,"name":"Shelly","icon":"fa-power-off","order":5,"disabled":false,"hidden":false}]

If you need any help, please start a forum thread.