Skip to content

Instantly share code, notes, and snippets.

@natcl

natcl/README.md Secret

Created August 16, 2018 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natcl/73ff7f7229d8f06c4b1e379d53c3fc65 to your computer and use it in GitHub Desktop.
Save natcl/73ff7f7229d8f06c4b1e379d53c3fc65 to your computer and use it in GitHub Desktop.
Simple gate and per topic gate using the switch node

This flow demonstrates how to use a switch to create a gate to block or let messages pass.

Simple gate

The simple gate uses a flow variable, flow.gate that is set using a change node. The switch then checks if flow.gate is true.

Per topic gate

The per topic gate is a single switch that can block or pass messages according to their topic. This is realized using JSONata. Using a change node we will update a flow variable called flow.topicGate that will contain key-value pairs of topic: boolean values. Using the inject node we inject either true or false with the topic we want to let pass or block. In the change node we merge flow.topicGate with a new key:value pair that comes from the inject node: $merge([$flowContext('topicGate'), {topic: payload}]).

The switch node then checks if the following JSONata expression is true by doing a lookup in the flow.topicGate object: $flowContext('topicGate') ~> $lookup(topic) .

[{"id":"b8aa28e4.aa5cb","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":220,"wires":[["d671a103.a0e918"]]},{"id":"d671a103.a0e918","type":"switch","z":"fd6ad715.04ee08","name":"gate","property":"gate","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":330,"y":220,"wires":[["e2e76159.42cf4"]]},{"id":"a3e6e226.8b2e98","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":100,"wires":[["7d6000d7.bf7bf8"]]},{"id":"b3593b69.1d4658","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":140,"wires":[["7d6000d7.bf7bf8"]]},{"id":"e2e76159.42cf4","type":"debug","z":"fd6ad715.04ee08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":220,"wires":[]},{"id":"7d6000d7.bf7bf8","type":"change","z":"fd6ad715.04ee08","name":"","rules":[{"t":"set","p":"gate","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":120,"wires":[[]]},{"id":"87ec16bc.8c1028","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"cat","payload":"purr","payloadType":"str","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":740,"wires":[["bc4036d6.2b5448"]]},{"id":"2960822c.71545e","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"dog","payload":"bark","payloadType":"str","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":780,"wires":[["bc4036d6.2b5448"]]},{"id":"bc9315c9.225c6","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"brocoli","payload":"eat","payloadType":"str","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":820,"wires":[["bc4036d6.2b5448"]]},{"id":"bc4036d6.2b5448","type":"switch","z":"fd6ad715.04ee08","name":"topicGate","property":"$flowContext('topicGate') ~> $lookup(topic)","propertyType":"jsonata","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":360,"y":800,"wires":[["f03b3526.545d08"]]},{"id":"a21566f8.af4e7","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"cat","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":400,"wires":[["b792555b.34b708"]]},{"id":"382268aa.127bf8","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"dog","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":540,"wires":[["b792555b.34b708"]]},{"id":"540fc4e9.894834","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"brocoli","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":600,"wires":[["b792555b.34b708"]]},{"id":"caafe33a.6617","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"cat","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":440,"wires":[["b792555b.34b708"]]},{"id":"bd8e34d3.79168","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"dog","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":500,"wires":[["b792555b.34b708"]]},{"id":"87fe9d31.cd6fc8","type":"inject","z":"fd6ad715.04ee08","name":"","topic":"brocoli","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":640,"wires":[["b792555b.34b708"]]},{"id":"b792555b.34b708","type":"change","z":"fd6ad715.04ee08","name":"","rules":[{"t":"set","p":"topicGate","pt":"flow","to":"$merge([$flowContext('topicGate'), {topic: payload}])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":354,"y":502,"wires":[[]]},{"id":"f03b3526.545d08","type":"debug","z":"fd6ad715.04ee08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":800,"wires":[]},{"id":"275ce8be.8bc0b8","type":"comment","z":"fd6ad715.04ee08","name":"Simple gate using a flow variable","info":"","x":190,"y":40,"wires":[]},{"id":"5f50454f.dce2e4","type":"comment","z":"fd6ad715.04ee08","name":"Per topic gate using a flow variable","info":"","x":200,"y":340,"wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment