# How to implement a sensor enable/disable node?

**URL:** <https://discourse.nodered.org/t/how-to-implement-a-sensor-enable-disable-node/38228>\
**Category:** General\
**Created:** [29 December 2020 18:43 UTC](https://discourse.nodered.org/t/how-to-implement-a-sensor-enable-disable-node/38228 "2020-12-29T18:43:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jabss](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jabss/32/34422_2.png) [@jabss](https://discourse.nodered.org/u/jabss)\
**Post date:** [29 December 2020 18:43 UTC](https://discourse.nodered.org/t/how-to-implement-a-sensor-enable-disable-node/38228/1 "2020-12-29T18:43:43Z")

</div>

Hi,

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](https://www.zigbee2mqtt.io/devices/SNZB-03.html) 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](https://sensorsiot.github.io/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).

Cheers,  
Jabss

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [29 December 2020 18:51 UTC](https://discourse.nodered.org/t/how-to-implement-a-sensor-enable-disable-node/38228/2 "2020-12-29T18:51:19Z")

</div>

Hello,  
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:

> **[Working with context : Node-RED](https://nodered.org/docs/user-guide/context)**

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:

> **[Route a message based on a context value : Node-RED](https://cookbook.nodered.org/basic/route-on-context)**

Hope this helps, Johannes

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 December 2020 21:18 UTC](https://discourse.nodered.org/t/how-to-implement-a-sensor-enable-disable-node/38228/3 "2020-12-29T21:18:04Z")

</div>

Or an alternative, maybe simpler, would be to use the dashboard switch to enable/disable a [node-red-contrib-simple-gate](https://flows.nodered.org/node/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.

---

<div class="post-metadata">

**Author:** ![jabss](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jabss/32/34422_2.png) [@jabss](https://discourse.nodered.org/u/jabss)\
**Post date:** [29 December 2020 23:25 UTC](https://discourse.nodered.org/t/how-to-implement-a-sensor-enable-disable-node/38228/4 "2020-12-29T23:25:49Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jabss](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jabss/32/34422_2.png) [@jabss](https://discourse.nodered.org/u/jabss)\
**Post date:** [29 December 2020 23:28 UTC](https://discourse.nodered.org/t/how-to-implement-a-sensor-enable-disable-node/38228/5 "2020-12-29T23:28:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 February 2021 23:28 UTC](https://discourse.nodered.org/t/how-to-implement-a-sensor-enable-disable-node/38228/6 "2021-02-27T23:28:04Z")

</div>

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