Advice on monitoring the status of a switched output please

Hi,

I'm looking for advice on a good way to monitor when the status of a switched device is different from the switch position.

I've been using Node-red for a few weeks now, learning how to control a number of ESP8266s using a Raspberry Pi, MQTT and Node-red dashboard. My ESP8266s are connected to relays controlling solenoid valves and a pump and also to flow, humidity and ultrasonic sensors, keeping the garden watered from the User Interface on my smart phone web browser.

I've noticed that sometimes the status of a switch in the Editor is different from the selected status in the UI. Thus, the switch position on the UI is "Off" but the status is "On", and the solenoid valve is actually open. To close it, I have to operate the UI switch to the On position and then to the Off position. I suspect that the cause of this is RFI or some other interference near the ESP8266 as I am sure it isn't an MQTT message coming from outside Node-Red. It was a pleasant surprise to find that the actual status of the relay was being reported correctly although I don't understand how this is being achieved.


I have looked at the Node-Red Status node and can extract the Status ("on", "off"). I had originally considered creating a variable which changed when the switch sent out "off" or "on" via MQTT but I was wondering whether there is a simpler or more elegant way of capturing the change of switch state?

Once I can get a notification of when the relay operates erroneously, I will be able to try to work out what might have caused it.

Thanks,

Tim

[{"id":"b8f8ea88.6d1d58","type":"mqtt in","z":"54dd5c1b.000824","name":"Potting Shed Humidity","topic":"garden/GPotShed/humidity","qos":"2","datatype":"auto","broker":"741a65d9.3edd9c","x":180,"y":140,"wires":[["dbbee51a.a9a248","51bc87ab.52d518","f24c3e45.57a75"]]},{"id":"dbbee51a.a9a248","type":"ui_text","z":"54dd5c1b.000824","group":"d6792cfa.7ab8c","order":7,"width":"3","height":"1","name":"Raised Bed Humidity %","label":"Humidity","format":"{{msg.payload}}%","layout":"row-left","x":650,"y":140,"wires":[]},{"id":"61fb6af2.294254","type":"mqtt in","z":"54dd5c1b.000824","name":"Potting Shed Water Butt Flow Rate","topic":"garden/GPotShed/waterflowlpm","qos":"2","datatype":"auto","broker":"741a65d9.3edd9c","x":220,"y":200,"wires":[["51bc87ab.52d518","f24c3e45.57a75","a8f5ea9.e786418"]]},{"id":"2ba885f2.42598a","type":"mqtt in","z":"54dd5c1b.000824","name":"Potting Shed Water Volume","topic":"garden/GPotShed/waterlevel","qos":"2","datatype":"auto","broker":"741a65d9.3edd9c","x":200,"y":60,"wires":[["51bc87ab.52d518","f24c3e45.57a75","1236ea1c.41c996"]]},{"id":"9e68712c.3df9f","type":"mqtt in","z":"54dd5c1b.000824","name":"Potting Shed Water Butt Flow","topic":"garden/GPotShed/waterflow","qos":"2","datatype":"auto","broker":"741a65d9.3edd9c","x":200,"y":280,"wires":[["f080f3bd.dbbc2","51bc87ab.52d518","f24c3e45.57a75"]]},{"id":"f080f3bd.dbbc2","type":"ui_text","z":"54dd5c1b.000824","group":"d6792cfa.7ab8c","order":8,"width":"3","height":"1","name":"Potting Shed Flow Integrator","label":"Total Flow (l)","format":"{{msg.payload}}","layout":"row-right","x":660,"y":280,"wires":[]},{"id":"af13d1d.e293a3","type":"mqtt out","z":"54dd5c1b.000824","name":"Raised Bed Solenoid Valve","topic":"","qos":"","retain":"","broker":"741a65d9.3edd9c","x":800,"y":600,"wires":[]},{"id":"3f718a5f.1df6d6","type":"ui_switch","z":"54dd5c1b.000824","name":"Potting Shed Raised Bed","label":"<font color= {{msg.color}} >Raised Bed</font>","tooltip":"","group":"d6792cfa.7ab8c","order":4,"width":"2","height":"1","passthru":true,"decouple":"false","topic":"garden/GPotShed/relay1","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"#F3B567","offvalue":"off","offvalueType":"str","officon":"","offcolor":"#F3B567","x":450,"y":600,"wires":[["af13d1d.e293a3","51bc87ab.52d518"]]},{"id":"16aaeeac.404e61","type":"ui_button","z":"54dd5c1b.000824","name":"Potting Shed LCD","group":"23c9e606.d5a5ba","order":1,"width":0,"height":0,"passthru":true,"label":"LCD-ON","tooltip":"","color":"","bgcolor":"","icon":"","payload":"on","payloadType":"str","topic":"garden/GPotShed/LCD","x":450,"y":480,"wires":[["c497ee1e.3e9cb","51bc87ab.52d518"]]},{"id":"c497ee1e.3e9cb","type":"mqtt out","z":"54dd5c1b.000824","name":"Turn on Potting Shed LCD","topic":"","qos":"0","retain":"","broker":"741a65d9.3edd9c","x":800,"y":480,"wires":[]},{"id":"51bc87ab.52d518","type":"debug","z":"54dd5c1b.000824","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":730,"y":440,"wires":[]},{"id":"91f73b6.e1acfc8","type":"mqtt out","z":"54dd5c1b.000824","name":"Potting Shed Solenoid Valve","topic":"","qos":"","retain":"","broker":"741a65d9.3edd9c","x":800,"y":660,"wires":[]},{"id":"bef2028e.16198","type":"ui_switch","z":"54dd5c1b.000824","name":"Potting Shed Pots Outlet","label":"<font color= {{msg.color}} >Pots Outlet</font>","tooltip":"","group":"d6792cfa.7ab8c","order":5,"width":"2","height":"1","passthru":true,"decouple":"false","topic":"garden/GPotShed/relay2","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"#F3B567","offvalue":"off","offvalueType":"str","officon":"","offcolor":"#F3B567","x":450,"y":660,"wires":[["91f73b6.e1acfc8","51bc87ab.52d518"]]},{"id":"34a94506.d95f2a","type":"mqtt out","z":"54dd5c1b.000824","name":"Hanging Baskets Solenoid Valve","topic":"","qos":"","retain":"","broker":"741a65d9.3edd9c","x":790,"y":720,"wires":[]},{"id":"56e39bb0.666fd4","type":"ui_switch","z":"54dd5c1b.000824","name":"Hanging Baskets Outlet","label":"<font color= {{msg.color}} >Basket Outlet</font>","tooltip":"","group":"d6792cfa.7ab8c","order":6,"width":"2","height":"1","passthru":true,"decouple":"false","topic":"garden/GPotShed/relay3","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"#F3B567","offvalue":"off","offvalueType":"str","officon":"","offcolor":"#F3B567","x":430,"y":720,"wires":[["34a94506.d95f2a","51bc87ab.52d518"]]},{"id":"cd23a082.6006c","type":"ui_button","z":"54dd5c1b.000824","name":"Reset Water Flow Integrator","group":"23c9e606.d5a5ba","order":9,"width":0,"height":0,"passthru":false,"label":"Reset Flow","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"garden/GPotShed/resetwaterflow","x":440,"y":540,"wires":[["48fbfc70.0f3484"]]},{"id":"48fbfc70.0f3484","type":"mqtt out","z":"54dd5c1b.000824","name":"Reset Water Flow Integrator","topic":"","qos":"","retain":"","broker":"741a65d9.3edd9c","x":800,"y":540,"wires":[]},{"id":"f24c3e45.57a75","type":"trigger","z":"54dd5c1b.000824","op1":"Active","op2":"Inactive","op1type":"str","op2type":"str","duration":"5","extend":true,"units":"s","reset":"","bytopic":"all","name":"Is System sending MQTT Messages?","x":330,"y":380,"wires":[["e8ab94d8.e9bac8"]]},{"id":"e8ab94d8.e9bac8","type":"switch","z":"54dd5c1b.000824","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Active","vt":"str"},{"t":"eq","v":"Inactive","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":150,"y":560,"wires":[["14b6d71f.3e47d9"],["b7ddc4f7.edf258"]]},{"id":"14b6d71f.3e47d9","type":"function","z":"54dd5c1b.000824","name":"SystemActive","func":"msg.color = \"white\";\nreturn msg;","outputs":1,"noerr":0,"x":160,"y":640,"wires":[["3f718a5f.1df6d6","bef2028e.16198","56e39bb0.666fd4"]]},{"id":"b7ddc4f7.edf258","type":"function","z":"54dd5c1b.000824","name":"SystemInactive","func":"msg.color = \"red\";\nreturn msg;","outputs":1,"noerr":0,"x":160,"y":680,"wires":[["3f718a5f.1df6d6","bef2028e.16198","56e39bb0.666fd4"]]},{"id":"1236ea1c.41c996","type":"ui_level","z":"54dd5c1b.000824","group":"d6792cfa.7ab8c","order":1,"width":0,"height":0,"name":"Potting Shed Water Level","label":"Available Water","colorHi":"#e60000","colorWarn":"#ff9900","colorNormal":"#00b33c","colorOff":"#595959","min":0,"max":"400","segWarn":"","segHigh":"","unit":"Litres","layout":"sh","channelA":"","channelB":"","decimals":0,"animations":"soft","shape":"3","colorschema":"valuedriven","textoptions":"default","colorText":"#eeeeee","fontLabel":"","fontValue":"","fontSmall":"","colorFromTheme":true,"textAnimations":false,"hideValue":false,"tickmode":"off","peakmode":false,"peaktime":3000,"x":650,"y":60,"wires":[]},{"id":"a8f5ea9.e786418","type":"ui_level","z":"54dd5c1b.000824","group":"d6792cfa.7ab8c","order":2,"width":"0","height":"0","name":"Flow Rate Single Horizontal","label":"Flow Rate","colorHi":"#e60000","colorWarn":"#ff9900","colorNormal":"#00b33c","colorOff":"#595959","min":0,"max":"8","segWarn":"","segHigh":"","unit":"l/min","layout":"sh","channelA":"","channelB":"","decimals":0,"animations":"rocket","shape":"3","colorschema":"rainbow","textoptions":"default","colorText":"#eeeeee","fontLabel":"","fontValue":"","fontSmall":"","colorFromTheme":true,"textAnimations":true,"hideValue":false,"tickmode":"off","peakmode":false,"peaktime":3000,"x":660,"y":200,"wires":[]},{"id":"741a65d9.3edd9c","type":"mqtt-broker","z":"","name":"GwelforpiMQTTBroker","broker":"gwelforpi","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"d6792cfa.7ab8c","type":"ui_group","z":"","name":"Potting Shed","tab":"1a81afe6.49674","order":2,"disp":true,"width":"6","collapse":false},{"id":"23c9e606.d5a5ba","type":"ui_group","z":"","name":"Potting Shed","tab":"edfbf889.280388","order":2,"disp":true,"width":"6","collapse":false},{"id":"1a81afe6.49674","type":"ui_tab","z":"","name":"Garden Watering","icon":"dashboard","order":1,"disabled":false,"hidden":false},{"id":"edfbf889.280388","type":"ui_tab","z":"","name":"Garden Watering LCD Control","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

Rearrange the logic in your flow so that the current solenoid state (from MQTT) is fed into the front of the switch so the switch will always match the current state in the real world. Disable Pass Through in the switch to prevent a loop. Feed the logic that you currently have driving the switch directly to MQTT instead of the switch node. The result will be that the logic will change the state of the solenoid, which will be fed back via MQTT and set the ui switch accordingly.

Edit, this has the additional advantage that the ui switch will automatically synchronise with the real world on node-red restart.

Colin,

Thanks for the elegant solution.

Tim

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.