Skip to content

Instantly share code, notes, and snippets.

@johnwalicki
Created February 15, 2017 04:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnwalicki/7c67709fecce1ad5992b695028e57646 to your computer and use it in GitHub Desktop.
Save johnwalicki/7c67709fecce1ad5992b695028e57646 to your computer and use it in GitHub Desktop.
Enable / Disable a Flow

Here are a few nodes that you can use to dynamically enable / disable a flow.

My use case: I often create demos using Node-RED. I might have 6 Node-RED flow tabs that each demonstrate a particular capability of a device. The flows each control the device in various ways. If you forget to delete a wire (purposefully breaking the flow), the demos on the various tabs stomp on each other. I wanted to create a "Stop" / "Spigot" node / technique to turn on/off a flow without breaking a flow. If I deploy or share the 6 tabs with all of the flows fully enabled, the demo is a disaster. If I post the flows with missing (broken) wires, you (or me... months later) would not be able to figure out what I'm doing. You would need to understand the flow to recreate the missing wire (obvious to me today but impossible for you).

After some discussion on the Node-RED Slack channel, I build this simple set of nodes using a flow context. Not rocket science but definitely a useful snippet.

[{"id":"f2a57185.7d4d38","type":"inject","z":"21c52910.4ca56e","name":"ON","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"x":110,"y":80,"wires":[["58ebe09e.68959"]]},{"id":"9b1f3cdb.63aef8","type":"inject","z":"21c52910.4ca56e","name":"OFF","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"x":110,"y":120,"wires":[["58ebe09e.68959"]]},{"id":"afe31c2e.19227","type":"switch","z":"21c52910.4ca56e","name":"Is this flow enabled?","property":"start-stop","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","outputs":1,"x":340,"y":200,"wires":[["4bb6caa6.9ee14c"]]},{"id":"ee6a7083.313a08","type":"inject","z":"21c52910.4ca56e","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":true,"x":130,"y":200,"wires":[["afe31c2e.19227","42b522a3.ed6b74"]]},{"id":"4bb6caa6.9ee14c","type":"function","z":"21c52910.4ca56e","name":"Generate Random Data","func":"msg.payload = Math.round(Math.random()*100);\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":200,"wires":[["9b9a4783.79fdd"]]},{"id":"9b9a4783.79fdd","type":"debug","z":"21c52910.4ca56e","name":"","active":true,"console":"false","complete":"false","x":810,"y":200,"wires":[]},{"id":"ec32d77d.694f5","type":"debug","z":"21c52910.4ca56e","name":"","active":true,"console":"false","complete":"false","x":530,"y":100,"wires":[]},{"id":"42b522a3.ed6b74","type":"function","z":"21c52910.4ca56e","name":"","func":"var enabled = flow.get('start-stop')||false;\nmsg.payload = enabled;\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":280,"wires":[["646c1496.fbacf4"]]},{"id":"646c1496.fbacf4","type":"debug","z":"21c52910.4ca56e","name":"","active":false,"console":"false","complete":"false","x":450,"y":280,"wires":[]},{"id":"58ebe09e.68959","type":"change","z":"21c52910.4ca56e","name":"Enable / Disable this flow","rules":[{"t":"set","p":"start-stop","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":100,"wires":[["ec32d77d.694f5"]]}]
@darvvin
Copy link

darvvin commented Mar 29, 2019

Thank you, just what I was looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment