How to implement a sensor enable/disable node?


I admit I'm not 100% aware of node red concepts, and I've been struggling in the last few days to have some basic flows working.

At the moment I have a sonoff PIR connected to zigbee2MQTT --> MQTT --> NodeRed --> InfluxDB/textfile. Whenever the PIR sensor is activated, there is a function node that send the battery level to an influxdb database and an enriched payload to a text file (for logging).

Now I'm trying to have some intermediate node (not sure what type, likely to be a function node) that based on the status of a dashboard switch routes the flow accordingly.
In other words: I'm trying to have a dashboard switch that I can access in NodeRed's dashboard GUI that enables/disables the flow to a third output.
If disabled, the flows go to the textfile (payload) and influxDB (as now).
If enabled, besides the previous two flows, there should be a third flow to some other node (I'm thinking maybe a telegram node that could send me a message).

My difficulty is implementing this "switching logic" based on a dashboard node. This should be a kind of global variable enabled/disabled by a switch in nodered's GUI.

Any suggestion how to do this in NodeRed?

Just for reference, in case it may be relevant, I'm using IOTstack with zigbee2mqtt, Mosquitto and NodeRed on a RPI4. As a zigbee controller, I'm using Slaesh C2652 USB stick (couldn't post the link due to 2 link limitation for new users).


You can use a dashboard switch connected to a change node to save the state of the switch as a context variable in the flow or global context.
You can read more about context storage here:

Than you can use a switch node to route the message to for example the telegram node depending on which value that context variable has. How to route messages like this is explained here:

Hope this helps, Johannes

1 Like

Or an alternative, maybe simpler, would be to use the dashboard switch to enable/disable a node-red-contrib-simple-gate node sitting in the flow you want to disable, to block it when the switch is in the appropriate state.

Thanks for getting back. This context was actually one of the pieces missing in my node-red understanding. Now things make slightly more sense. Thanks!

Just used this alternative. Actually quite simple and it was quite easy to use it to implement make a working flow! Many thanks!

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