Beginner Question; Only pass payload if another value is true

Hi guys,

I have a simple question.

I have a node calculating values and generating an on/off comand, that works works fine.

Now I want to block the command if a switch from the UI is false. How do I have to do this?

[{"id":"58b13ec1.6e319","type":"ui_switch","z":"d719370b.e5d548","name":"","label":"Smart Charge","tooltip":"","group":"6b8a6c67.042ff4","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":140,"y":320,"wires":[["e1067a88.af1748"]]},{"id":"a30aba8e.2e1ed8","type":"debug","z":"d719370b.e5d548","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":600,"wires":[]},{"id":"e37457d1.603df8","type":"mqtt in","z":"d719370b.e5d548","name":"","topic":"/PV/AC_Power","qos":"2","datatype":"auto","broker":"72a511b3.b8702","x":180,"y":600,"wires":[["e1067a88.af1748"]]},{"id":"e1067a88.af1748","type":"function","z":"d719370b.e5d548","name":"","func":"var conv = parseFloat(msg.payload)\nif (conv>=1250) {\n    msg.payload=\"ON\"\n} else {\n    msg.payload=\"OFF\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":480,"wires":[["a30aba8e.2e1ed8"]]},{"id":"6b8a6c67.042ff4","type":"ui_group","name":"E_Auto","tab":"a5fa57f2.6a9058","order":1,"disp":true,"width":"6","collapse":false},{"id":"72a511b3.b8702","type":"mqtt-broker","name":"MQTT Mosquito Node-RED machine","broker":"192.168.178.204","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"a5fa57f2.6a9058","type":"ui_tab","name":"Lade Steuerung","icon":"dashboard","disabled":false,"hidden":false}]

There are various "gate" nodes you can use or you can use a function node. Typically, you store the UI switch in a varaiable and then can test it when the command flow is triggered.

The Creating Nodes category if for help with creating new node-types rather than help with function nodes. I will move this to the General category.

Have a look at node-red-contrib-simple-gate you can wire your switch to that to block or allow your commands through.

here is an example using context storage, this could also be done with a join node, or you can install a contrib node like simple gate.

[{"id":"e37457d1.603df8","type":"mqtt in","z":"5a245aa1.510164","name":"","topic":"/PV/AC_Power","qos":"2","datatype":"auto","broker":"72a511b3.b8702","x":130,"y":2560,"wires":[["eebe529d.c4b0c8"]]},{"id":"eebe529d.c4b0c8","type":"switch","z":"5a245aa1.510164","name":"","property":"shallNotPass","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":340,"y":2560,"wires":[["e1067a88.af1748"]]},{"id":"e1067a88.af1748","type":"function","z":"5a245aa1.510164","name":"","func":"var conv = parseFloat(msg.payload)\nif (conv>=1250) {\n    msg.payload=\"ON\"\n} else {\n    msg.payload=\"OFF\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":510,"y":2560,"wires":[["a30aba8e.2e1ed8"]]},{"id":"757323b.daf715c","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1300","payloadType":"str","x":140,"y":2620,"wires":[["eebe529d.c4b0c8"]]},{"id":"9bb54f30.d4e81","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1100","payloadType":"str","x":130,"y":2660,"wires":[["eebe529d.c4b0c8"]]},{"id":"a30aba8e.2e1ed8","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":2560,"wires":[]},{"id":"58b13ec1.6e319","type":"ui_switch","z":"5a245aa1.510164","name":"","label":"Smart Charge","tooltip":"","group":"6b8a6c67.042ff4","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"smart","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":170,"y":2460,"wires":[["6ab36af.892c894"]]},{"id":"4c303768.31d6f8","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":190,"y":2420,"wires":[["6ab36af.892c894"]]},{"id":"ce5ff0f4.fd6d7","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":180,"y":2380,"wires":[["6ab36af.892c894"]]},{"id":"6ab36af.892c894","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"shallNotPass","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":2460,"wires":[[]]},{"id":"72a511b3.b8702","type":"mqtt-broker","name":"MQTT Mosquito Node-RED machine","broker":"192.168.178.204","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"6b8a6c67.042ff4","type":"ui_group","name":"E_Auto","tab":"a5fa57f2.6a9058","order":1,"disp":true,"width":"6","collapse":false},{"id":"a5fa57f2.6a9058","type":"ui_tab","name":"Lade Steuerung","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

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