On, Off, Faulty State for a Sensor

Hi. I am using a water flow sensor. My purpose here is to display "OFF", when the switch is "OFF" (even if there is flow or not, if the switch is off it should display "OFF" at all times). When it is on, it can start with "ON", and then it should say "Faulty" when there is no flow. I detect the flow with pulses coming from the sensor. For now, I am able to detect it, and it correctly changes according to the situation. However, it is able to change when I turn the switch off... It should stay "OFF" when the switch is off, without caring if there is water flow or not. I have been trying to solve this problem with everything I can think of. Can someone help me with this? Here is my flow:

[{"id":"4b8f463e.a29208","type":"inject","z":"4cdd06f6.faf528","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"3","topic":"","payload":"false","payloadType":"bool","x":290,"y":540,"wires":[["3f4d5d1a.4c6aa2"]]},{"id":"14dab897.d647d7","type":"ui_switch","z":"4cdd06f6.faf528","name":"","label":"START/STOP","tooltip":"","group":"843bc78a.c5b078","order":7,"width":5,"height":1,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"mi-power_settings_new ","oncolor":"yellow","offvalue":"false","offvalueType":"bool","officon":"mi-power_settings_new ","offcolor":"black","x":500,"y":500,"wires":[["e116c5af.573e28","7791562c.bacb98","29119598.39809a","3f4d5d1a.4c6aa2","c7b4886f.dd9f28"]]},{"id":"3f4d5d1a.4c6aa2","type":"function","z":"4cdd06f6.faf528","name":"","func":"var b0 = msg.payload;\n\nif(b0 === false)\n{\n    msg.payload = \"OFF\";\n}\nelse if(b0 === true)\n{\n    msg.payload = \"ON\";\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":860,"y":540,"wires":[["25c1da1.d10c026"]]},{"id":"c7b4886f.dd9f28","type":"switch","z":"4cdd06f6.faf528","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":850,"y":580,"wires":[["74474b49.f48c94"]]},{"id":"74474b49.f48c94","type":"trigger","z":"4cdd06f6.faf528","name":"","op1":"ON","op2":"FAULTY","op1type":"str","op2type":"str","duration":"1","extend":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":900,"y":700,"wires":[["7e33356e.69f75c"]]},{"id":"7e33356e.69f75c","type":"rbe","z":"4cdd06f6.faf528","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":1010,"y":640,"wires":[["25c1da1.d10c026"]]},{"id":"25c1da1.d10c026","type":"batch","z":"4cdd06f6.faf528","name":"","mode":"count","count":"2","overlap":"1","interval":10,"allowEmptySequence":false,"topics":[],"x":1030,"y":560,"wires":[["2fd32e9f.cc1392"]]},{"id":"2fd32e9f.cc1392","type":"join","z":"4cdd06f6.faf528","name":"","mode":"auto","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1150,"y":580,"wires":[["c366ba11.e881b8"]]},{"id":"c366ba11.e881b8","type":"function","z":"4cdd06f6.faf528","name":"","func":"var z0 = msg.payload;\nif(z0[1] == \"OFF\" || z0[2] == \"OFF\")\n{\n    msg.payload = \"OFF\";\n}\nelse\n{\n    msg.payload = z0[1];\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1180,"y":680,"wires":[["648539b3.8086b8","40b73f31.4f798"]]},{"id":"648539b3.8086b8","type":"debug","z":"4cdd06f6.faf528","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":740,"wires":[]},{"id":"40b73f31.4f798","type":"ui_text","z":"4cdd06f6.faf528","group":"843bc78a.c5b078","order":4,"width":3,"height":1,"name":"","label":"Pump","format":"{{msg.payload}}","layout":"row-right","x":1290,"y":620,"wires":[]},{"id":"843bc78a.c5b078","type":"ui_group","z":"","name":"logo","tab":"43cae785.290cd8","order":3,"disp":false,"width":8,"collapse":false},{"id":"43cae785.290cd8","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

When you say "it" should say faulty, are you referring to the switch label? So the switch label would have three states, on - off - faulty?

By "it", i mean the Text named as "Pump".

Pump OFF -> Switch is OFF
Pump ON -> Switch is ON, and there is flow
Pump Faulty -> Switch is ON, and there is no flow

Edit: To be clear, the left side is how you see it on the GUI. Right side is the condition for when you see that corresponding status.

Where does the flow signal go in? From the previous thread I think that is a sequence of messages indicating the pump is running.

I am using another node that I downloaded. It sends 1 on every rising edge. Normally, I didn't want to combine messages. I thought maybe batch and join could fix my problem, but they didn't. At least, I couldn't do it.

Here is an example flow. There is lot's of extra code in here but I left it in to kind of show what is going on. To run flow use the inject nodes to the side to simulate switch and sensor changing states.

[{"id":"cad269da.8079","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"900284cb.d40658","type":"inject","z":"cad269da.8079","name":"flow sensor stopped","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"3","topic":"flow","payload":"false","payloadType":"bool","x":630,"y":80,"wires":[["fad0204d.78b45"]]},{"id":"b69fcdf8.f0b578","type":"ui_switch","z":"cad269da.8079","name":"","label":"START/STOP","tooltip":"","group":"85b0dcc6.aced68","order":7,"width":5,"height":1,"passthru":true,"decouple":"false","topic":"switch","style":"","onvalue":"true","onvalueType":"bool","onicon":"mi-power_settings_new ","oncolor":"yellow","offvalue":"false","offvalueType":"bool","officon":"mi-power_settings_new ","offcolor":"black","x":620,"y":320,"wires":[["2dbcceb2.6df9e2","fad5faaa.d06128"]]},{"id":"2dbcceb2.6df9e2","type":"function","z":"cad269da.8079","name":"","func":"var b0 = msg.payload;\n\nif(b0 === false)\n{\n    msg.payload = \"OFF\";\n}\nelse if(b0 === true)\n{\n    msg.payload = \"ON\";\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":320,"wires":[["be29e3d2.348db"]]},{"id":"fad5faaa.d06128","type":"switch","z":"cad269da.8079","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":750,"y":400,"wires":[["f340b6ce.57c278"]]},{"id":"f340b6ce.57c278","type":"trigger","z":"cad269da.8079","name":"","op1":"ON","op2":"FAULTY","op1type":"str","op2type":"str","duration":"1","extend":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":900,"y":400,"wires":[["c3acb9fe.6802b8"]]},{"id":"c3acb9fe.6802b8","type":"rbe","z":"cad269da.8079","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":1070,"y":400,"wires":[["be29e3d2.348db"]]},{"id":"be29e3d2.348db","type":"batch","z":"cad269da.8079","name":"","mode":"count","count":"2","overlap":"1","interval":10,"allowEmptySequence":false,"topics":[],"x":1030,"y":320,"wires":[["d6271362.b29d78"]]},{"id":"d6271362.b29d78","type":"join","z":"cad269da.8079","name":"","mode":"auto","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1170,"y":320,"wires":[["d8e82c9a.575538"]]},{"id":"d8e82c9a.575538","type":"function","z":"cad269da.8079","name":"","func":"var z0 = msg.payload;\nif(z0[1] == \"OFF\" || z0[2] == \"OFF\")\n{\n    msg.payload = \"OFF\";\n}\nelse\n{\n    msg.payload = z0[1];\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1300,"y":320,"wires":[["51ea4d6f.746ee4"]]},{"id":"51ea4d6f.746ee4","type":"debug","z":"cad269da.8079","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1490,"y":320,"wires":[]},{"id":"29cc2365.924c7c","type":"ui_text","z":"cad269da.8079","group":"85b0dcc6.aced68","order":4,"width":3,"height":1,"name":"","label":"Pump","format":"{{msg.payload}}","layout":"row-right","x":1250,"y":140,"wires":[]},{"id":"fad0204d.78b45","type":"function","z":"cad269da.8079","name":"logic","func":"if (context.get('sensor') === undefined){\n    context.set('sensor','off')\n}\nif (context.get('toggle') === undefined){\n    context.set('toggle','off')\n}\n\nif (msg.payload === true && msg.topic == 'toggle'){\n    toggle = 'on';\n    context.set('toggle','on');\n}\n\nif (msg.payload === false && msg.topic == 'toggle'){\n    toggle = 'off';\n    context.set('toggle','iff');\n}\n\nif (msg.payload === false && msg.topic == 'flow'){\n    sensor = 'off';\n    context.set('sensor','off');\n}\n\nif (msg.payload === true && msg.topic == 'flow'){\n    sensor = 'on';\n    context.set('sensor','on');\n}\n\nif (context.get('toggle') == 'on' && context.get('sensor') == 'off'){\n    msg.payload = 'faulty';\n    node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n    return msg;\n}\n\nif (context.get('toggle') == 'on' && context.get('sensor') == 'on'){\n    msg.payload = 'on';\n    node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n    return msg;\n}\n\nif (context.get('toggle') == 'off'){\n    msg.paylaod = 'off'\n    node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n    return msg;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":970,"y":140,"wires":[["29cc2365.924c7c"]]},{"id":"a8356d4.978359","type":"inject","z":"cad269da.8079","name":"flow sensor running","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"flow","payload":"true","payloadType":"bool","x":630,"y":140,"wires":[["fad0204d.78b45"]]},{"id":"3ba9663e.17899a","type":"inject","z":"cad269da.8079","name":"switch on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"toggle","payload":"true","payloadType":"bool","x":600,"y":200,"wires":[["fad0204d.78b45"]]},{"id":"966f081a.459dc","type":"inject","z":"cad269da.8079","name":"switch off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"toggle","payload":"false","payloadType":"bool","x":600,"y":260,"wires":[["fad0204d.78b45"]]},{"id":"85b0dcc6.aced68","type":"ui_group","z":"","name":"logo","tab":"7d6ced48.3610c4","order":3,"disp":false,"width":8,"collapse":false},{"id":"7d6ced48.3610c4","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

sorry, found a couple of typo's

[{"id":"cad269da.8079","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"900284cb.d40658","type":"inject","z":"cad269da.8079","name":"flow sensor stopped","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"3","topic":"flow","payload":"false","payloadType":"bool","x":630,"y":80,"wires":[["fad0204d.78b45"]]},{"id":"b69fcdf8.f0b578","type":"ui_switch","z":"cad269da.8079","name":"","label":"START/STOP","tooltip":"","group":"85b0dcc6.aced68","order":7,"width":5,"height":1,"passthru":true,"decouple":"false","topic":"switch","style":"","onvalue":"true","onvalueType":"bool","onicon":"mi-power_settings_new ","oncolor":"yellow","offvalue":"false","offvalueType":"bool","officon":"mi-power_settings_new ","offcolor":"black","x":620,"y":320,"wires":[["2dbcceb2.6df9e2","fad5faaa.d06128"]]},{"id":"2dbcceb2.6df9e2","type":"function","z":"cad269da.8079","name":"","func":"var b0 = msg.payload;\n\nif(b0 === false)\n{\n    msg.payload = \"OFF\";\n}\nelse if(b0 === true)\n{\n    msg.payload = \"ON\";\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":320,"wires":[["be29e3d2.348db"]]},{"id":"fad5faaa.d06128","type":"switch","z":"cad269da.8079","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":750,"y":400,"wires":[["f340b6ce.57c278"]]},{"id":"f340b6ce.57c278","type":"trigger","z":"cad269da.8079","name":"","op1":"ON","op2":"FAULTY","op1type":"str","op2type":"str","duration":"1","extend":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":900,"y":400,"wires":[["c3acb9fe.6802b8"]]},{"id":"c3acb9fe.6802b8","type":"rbe","z":"cad269da.8079","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":1070,"y":400,"wires":[["be29e3d2.348db"]]},{"id":"be29e3d2.348db","type":"batch","z":"cad269da.8079","name":"","mode":"count","count":"2","overlap":"1","interval":10,"allowEmptySequence":false,"topics":[],"x":1030,"y":320,"wires":[["d6271362.b29d78"]]},{"id":"d6271362.b29d78","type":"join","z":"cad269da.8079","name":"","mode":"auto","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1170,"y":320,"wires":[["d8e82c9a.575538"]]},{"id":"d8e82c9a.575538","type":"function","z":"cad269da.8079","name":"","func":"var z0 = msg.payload;\nif(z0[1] == \"OFF\" || z0[2] == \"OFF\")\n{\n    msg.payload = \"OFF\";\n}\nelse\n{\n    msg.payload = z0[1];\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1300,"y":320,"wires":[["51ea4d6f.746ee4"]]},{"id":"51ea4d6f.746ee4","type":"debug","z":"cad269da.8079","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1490,"y":320,"wires":[]},{"id":"29cc2365.924c7c","type":"ui_text","z":"cad269da.8079","group":"85b0dcc6.aced68","order":4,"width":3,"height":1,"name":"","label":"Pump","format":"{{msg.payload}}","layout":"row-right","x":1250,"y":140,"wires":[]},{"id":"fad0204d.78b45","type":"function","z":"cad269da.8079","name":"logic","func":"if (context.get('sensor') === undefined){\n    context.set('sensor','off')\n}\nif (context.get('toggle') === undefined){\n    context.set('toggle','off')\n}\n\nif (msg.payload === true && msg.topic == 'toggle'){\n    toggle = 'on';\n    context.set('toggle','on');\n}\n\nif (msg.payload === false && msg.topic == 'toggle'){\n    toggle = 'off';\n    context.set('toggle','off');\n}\n\nif (msg.payload === false && msg.topic == 'flow'){\n    sensor = 'off';\n    context.set('sensor','off');\n}\n\nif (msg.payload === true && msg.topic == 'flow'){\n    sensor = 'on';\n    context.set('sensor','on');\n}\n\nif (context.get('toggle') == 'on' && context.get('sensor') == 'off'){\n    msg.payload = 'faulty';\n    node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n    return msg;\n}\n\nif (context.get('toggle') == 'on' && context.get('sensor') == 'on'){\n    msg.payload = 'on';\n    node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n    return msg;\n}\n\nif (context.get('toggle') == 'off'){\n    msg.payload = 'off'\n    node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n    return msg;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":970,"y":140,"wires":[["29cc2365.924c7c"]]},{"id":"a8356d4.978359","type":"inject","z":"cad269da.8079","name":"flow sensor running","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"flow","payload":"true","payloadType":"bool","x":630,"y":140,"wires":[["fad0204d.78b45"]]},{"id":"3ba9663e.17899a","type":"inject","z":"cad269da.8079","name":"switch on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"toggle","payload":"true","payloadType":"bool","x":600,"y":200,"wires":[["fad0204d.78b45"]]},{"id":"966f081a.459dc","type":"inject","z":"cad269da.8079","name":"switch off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"toggle","payload":"false","payloadType":"bool","x":600,"y":260,"wires":[["fad0204d.78b45"]]},{"id":"85b0dcc6.aced68","type":"ui_group","z":"","name":"logo","tab":"7d6ced48.3610c4","order":3,"disp":false,"width":8,"collapse":false},{"id":"7d6ced48.3610c4","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

This should do it

[{"id":"b1b9f109.13889","type":"ui_switch","z":"bdd7be38.d3b55","name":"","label":"START/STOP","tooltip":"","group":"af43d6a0.0b4b98","order":7,"width":5,"height":1,"passthru":true,"decouple":"false","topic":"pump_switch","style":"","onvalue":"true","onvalueType":"bool","onicon":"mi-power_settings_new ","oncolor":"yellow","offvalue":"false","offvalueType":"bool","officon":"mi-power_settings_new ","offcolor":"black","x":120,"y":3900,"wires":[["733d70ff.e55a78","510337a4.f9cd58"]]},{"id":"18c4c429.d71154","type":"ui_text","z":"bdd7be38.d3b55","group":"af43d6a0.0b4b98","order":4,"width":3,"height":1,"name":"","label":"Pump","format":"{{msg.payload}}","layout":"row-right","x":770,"y":3900,"wires":[]},{"id":"510337a4.f9cd58","type":"trigger","z":"bdd7be38.d3b55","name":"","op1":"ON","op2":"FAULTY","op1type":"str","op2type":"str","duration":"1","extend":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":320,"y":4020,"wires":[["7399d2cc.5bf624"]]},{"id":"7399d2cc.5bf624","type":"change","z":"bdd7be38.d3b55","name":"topic: pump","rules":[{"t":"set","p":"topic","pt":"msg","to":"pump","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":4020,"wires":[["733d70ff.e55a78"]]},{"id":"733d70ff.e55a78","type":"join","z":"bdd7be38.d3b55","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":510,"y":3900,"wires":[["f2040d66.1f6b58"]]},{"id":"f2040d66.1f6b58","type":"function","z":"bdd7be38.d3b55","name":"","func":"if (msg.payload.pump_switch) {\n    // pump should be on\n    msg.payload = msg.payload.pump  // this will be ON or FAULTY\n} else {\n    msg.payload = \"OFF\"\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":3900,"wires":[["18c4c429.d71154"]]},{"id":"4092d2e6.a156ac","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"3","topic":"","payload":"false","payloadType":"bool","x":90,"y":3860,"wires":[["b1b9f109.13889"]]},{"id":"582a4c4.bf3a834","type":"comment","z":"bdd7be38.d3b55","name":"Flow pulses go in here ->","info":"","x":120,"y":4020,"wires":[]},{"id":"af43d6a0.0b4b98","type":"ui_group","z":"","name":"logo","tab":"6f169b9f.b92d2c","order":3,"disp":false,"width":8,"collapse":false},{"id":"6f169b9f.b92d2c","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

Note that I have added a topic to the switch node so that the join will work.

Thanks a lot! You are the best!

Glad to be of help. Now can I ask that you study each node and work out what it does and how the flow works please. If you don't understand anything then ask. When trying to understand it you may find it useful to add debug nodes to see what is coming out of each node.

@oncdastan can you mark this as solved please. Click on the three dots at the bottom of the post that provides the solution and select the Solution checkbox.

Done. Thanks again.

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