data flow "gearbox"

It is useful to be able to "gate" whether a feed of data gets through to a later part of the flow, or not.

In general, being able to select from one of a number of inputs, rather like selecting which gear your (manual gearbox!) car is in, letting one of a number of feeds through from the engine to the wheels.

That's what the gearbox node does.

The data feeds come in on distinct topics (in the example flow, 'A', 'B' and 'C').

To select one of the feeds, send in a message on the "select" topic with the payload set to the feed you want to select for transmission through the gearbox node (e.g. topic: "select", payload: "A").

If you select a topic that doesn't match any of your feeds, none of the feeds passes through the gearbox. This is like putting your car into 'neutral'.

The node indicator shows which feed is currently selected.

There is a "retain" feature (turned on and off by the flag "retained" at the start of the function), which when "true" (activated) holds the latest value from each of the feeds, and releases it as the first message out of the gearbox node as soon as that feed is selected. Rather like retained messages in MQTT, if that helps ;)

[{"id":"e49e5159.865348","type":"inject","z":"cd2c0bd0.ec18a","name":"","topic":"select","payload":"A","payloadType":"str","repeat":"","crontab":"","once":false,"x":170,"y":1920,"wires":[["f3a9421d.68e13"]]},{"id":"f3a9421d.68e13","type":"function","z":"cd2c0bd0.ec18a","name":"gearbox","func":"// do you want a \"retained\" message on each topic?\nvar retained = true;\nvar selected = context.get('selected') || null;\n\nif (!selected)\n{\n    node.status({fill:\"red\",shape:\"ring\",text:\"'neutral'\"});\n}\n\nif (msg.topic == \"select\")\n{\n    selected = msg.payload;\n    context.set('selected', selected);\n    node.status({fill:\"green\",shape:\"dot\",text:selected});\n    if (retained)\n    {\n        var retains = context.get('retains') || [];\n        if (retains[selected]) {\n            return {topic: selected, payload: retains[selected]};\n        }\n    }\n}\nelse \n{\n    if (retained)\n    {\n        var retains = context.get('retains') || [];\n        retains[msg.topic] = msg.payload;\n        context.set('retains', retains);\n    }\n    if (msg.topic == selected) \n    {\n        return msg;\n    }\n}\n// otherwise send nothing\n","outputs":1,"noerr":0,"x":460,"y":2080,"wires":[["af24680d.0bc8c"]]},{"id":"552aeec6.f467","type":"inject","z":"cd2c0bd0.ec18a","name":"","topic":"A","payload":"alpha","payloadType":"str","repeat":"1","crontab":"","once":false,"x":180,"y":2200,"wires":[["f3a9421d.68e13"]]},{"id":"896b5748.60fea","type":"inject","z":"cd2c0bd0.ec18a","name":"","topic":"B","payload":"beta","payloadType":"str","repeat":"1","crontab":"","once":false,"x":180,"y":2240,"wires":[["f3a9421d.68e13"]]},{"id":"d005d45d.9cfac","type":"inject","z":"cd2c0bd0.ec18a","name":"","topic":"C","payload":"charlie","payloadType":"str","repeat":"1","crontab":"","once":false,"x":190,"y":2280,"wires":[["f3a9421d.68e13"]]},{"id":"c57a9e6e.6bc4d","type":"inject","z":"cd2c0bd0.ec18a","name":"","topic":"select","payload":"B","payloadType":"str","repeat":"","crontab":"","once":false,"x":170,"y":1960,"wires":[["f3a9421d.68e13"]]},{"id":"a064120.d4cd57","type":"inject","z":"cd2c0bd0.ec18a","name":"","topic":"select","payload":"C","payloadType":"str","repeat":"","crontab":"","once":false,"x":180,"y":2000,"wires":[["f3a9421d.68e13"]]},{"id":"76d0a103.125b38","type":"inject","z":"cd2c0bd0.ec18a","name":"","topic":"select","payload":"D","payloadType":"str","repeat":"","crontab":"","once":false,"x":180,"y":2040,"wires":[["f3a9421d.68e13"]]},{"id":"af24680d.0bc8c","type":"debug","z":"cd2c0bd0.ec18a","name":"","active":true,"console":"false","complete":"false","x":630,"y":2080,"wires":[]},{"id":"c9f6db7b.d8cba","type":"comment","z":"cd2c0bd0.ec18a","name":"(D means nothing gets through)","info":"","x":150,"y":2080,"wires":[]},{"id":"c0f43757.b0a8c8","type":"comment","z":"cd2c0bd0.ec18a","name":"data feeds","info":"","x":170,"y":2160,"wires":[]}]
andysc

Flow Info

created 4 months, 2 weeks ago

Node Types

Core
  • comment (x2)
  • debug (x1)
  • function (x1)
  • inject (x7)

Tags

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