Function If Statement Example

Example function if statement example

[{"id":"8fdf0634.fdc3b8","type":"inject","z":"2755f3c2.561704","name":"","topic":"","payload":"PumpOn","payloadType":"str","repeat":"","crontab":"","once":false,"x":112.76666259765625,"y":41.01667022705078,"wires":[["1f7e0169.8c6547","3e0a8cfb.fa66bc"]]},{"id":"c3f62387.fab968","type":"inject","z":"2755f3c2.561704","name":"","topic":"PumpTopic","payload":"PumpOff","payloadType":"str","repeat":"","crontab":"","once":false,"x":144.76666259765625,"y":90.01667022705078,"wires":[["1f7e0169.8c6547","3e0a8cfb.fa66bc"]]},{"id":"9d3a07c0.3dd1e8","type":"inject","z":"2755f3c2.561704","name":"","topic":"SomethingRandom","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":171.76666259765625,"y":140.01666259765625,"wires":[["1f7e0169.8c6547","3e0a8cfb.fa66bc"]]},{"id":"1f7e0169.8c6547","type":"function","z":"2755f3c2.561704","name":"Function","func":"if (msg.payload == 'PumpOn')  \n    {\n    return {topic:'Pump01/',payload:'On'}; \n    } \nelse if (msg.payload == 'PumpOff') \n    {\n    return {topic:'Pump01/',payload:'Off'};    \n    }\nelse\n    {\n    return {topic:'Information/',payload:'Unknown command'};    \n    }\n","outputs":1,"noerr":0,"x":387.76666259765625,"y":46.51667022705078,"wires":[["e01dcd06.291c78"]]},{"id":"e01dcd06.291c78","type":"debug","z":"2755f3c2.561704","name":"","active":true,"console":"false","complete":"true","x":530.7666625976562,"y":46.25000762939453,"wires":[]},{"id":"3e0a8cfb.fa66bc","type":"debug","z":"2755f3c2.561704","name":"","active":true,"console":"false","complete":"true","x":398.75,"y":91.8333511352539,"wires":[]}]

Flow Info

Created 7 years ago
Rating: 1 1

Owner

Actions

Rate:

Node Types

Core
  • debug (x2)
  • function (x1)
  • inject (x3)

Tags

Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option