# How to create a switch (on/off) on my flow

**URL:** https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323
**Category:** General
**Created:** [11 February 2025 15:05 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323 "2025-02-11T15:05:07Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![schnre](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@schnre](https://discourse.nodered.org/u/schnre)
#### Post date: [11 February 2025 15:05 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/1 "2025-02-11T15:05:07Z")

</div>

Hi, i've created a flow that send a msg evry 5 seconds and it's infinite now i have to create a switch that make me able to turn on and off the system without "cutting" the flow. Thx in advance 🙂

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [11 February 2025 15:15 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/2 "2025-02-11T15:15:09Z")

</div>

There are a ton of examples on the forum.

Here is one such way: [Stop inject message - #3 by Steve-Mcl](https://discourse.nodered.org/t/stop-inject-message/44848/3)

> [@Stop inject message](https://discourse.nodered.org/t/stop-inject-message/44848/3):
>
> you can easily achieve this with a function node...
> 
> ![QaWHgFcxqx](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f569845ee88b893b9af06722a9178c1d6c6162d.gif)

You could also install a `gate` node to do the same thing

---

<div class="post-metadata">

### Author: ![schnre](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@schnre](https://discourse.nodered.org/u/schnre)
#### Post date: [11 February 2025 15:18 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/3 "2025-02-11T15:18:04Z")

</div>

can i do it without using code?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [11 February 2025 15:18 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/4 "2025-02-11T15:18:42Z")

</div>

Yes. Use a `change` node to Set/Reset a `flow` or `global` context variable and use a `switch` node to allow/block the passing of messages.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [11 February 2025 15:18 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/5 "2025-02-11T15:18:42Z")

</div>

Hello again! 😀

Two options:

1. Put a switch node after your inject which tests the value of a context variable  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/a/aab37994906890dbd595926069aef56aea45d857.png)

```auto
[{"id":"e43380a154ca2c9a","type":"inject","z":"8c4e3f534dc04c4f","name":"","props":[{"p":"payload"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":480,"wires":[["5016645086a493e7"]]},{"id":"5016645086a493e7","type":"switch","z":"8c4e3f534dc04c4f","name":"flow.messagesblocked == yes?","property":"messagesblocked","propertyType":"flow","rules":[{"t":"eq","v":"no","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":530,"y":480,"wires":[["f2af62755da68fea"]]},{"id":"f2af62755da68fea","type":"debug","z":"8c4e3f534dc04c4f","name":"debug 20","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":760,"y":480,"wires":[]},{"id":"b29fe054b9e09f23","type":"inject","z":"8c4e3f534dc04c4f","name":"block messages","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"yes","payloadType":"str","x":300,"y":380,"wires":[["5f9c03e9e8554e93"]]},{"id":"30fa8ae93edc94f8","type":"inject","z":"8c4e3f534dc04c4f","name":"allow messages","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"no","payloadType":"str","x":300,"y":420,"wires":[["5f9c03e9e8554e93"]]},{"id":"5f9c03e9e8554e93","type":"change","z":"8c4e3f534dc04c4f","name":"","rules":[{"t":"set","p":"messagesblocked","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":400,"wires":[[]]}]

```

1. There are various "gate" nodes available. Take a look at node-red-contrib-simple-gate for instance. It may have example flows you can import via Manage Palette.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [11 February 2025 15:20 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/6 "2025-02-11T15:20:11Z")

</div>

PS, this is really beginner stuff so i assume you are new to node-red?

I recommend watching this playlist (it covers context and switches and change nodes): [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<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: [11 February 2025 15:23 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/8 "2025-02-11T15:23:10Z")

</div>

You can use node-red-contrib-simple-gate which is designed for exactly this.

---

<div class="post-metadata">

### Author: ![schnre](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@schnre](https://discourse.nodered.org/u/schnre)
#### Post date: [11 February 2025 15:28 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/9 "2025-02-11T15:28:44Z")

</div>

and how can i connect?

---

<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: [11 February 2025 15:37 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/10 "2025-02-11T15:37:49Z")

</div>

Put it where you want to block it.  
If you are using the gate node then look at the help text and the readme on the node-red flows page to see how to use it. Also, after installing it, you can import example flows for the node by selecting Import and then Examples.

If you don't understand how to use it then watch the videos linked to earlier and come back if you need further help after that.

---

<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: [11 February 2025 15:41 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/11 "2025-02-11T15:41:20Z")

</div>

Also please add a closing comment to your [previous post](https://discourse.nodered.org/t/help-needed-for-create-a-delay-in-a-node/95319) indicating that that question has been answered, or you can mark one of the answers as the solution.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [11 February 2025 15:51 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/12 "2025-02-11T15:51:53Z")

</div>

> [@schnre](#):
>
> so i have this... how can i edit to obtain the switchh?

Hard to tell from just a picture of your flow, but perhaps you are republishing an MQTT message to the same topic?  
I strongly advise against doing it like this.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [11 February 2025 16:09 UTC](https://discourse.nodered.org/t/how-to-create-a-switch-on-off-on-my-flow/95323/13 "2025-02-11T16:09:06Z")

</div>

If the incoming messages are from a mqtt node. You could use dynamic subscription to subscribe and unsubsrcibe at will.  
examples here [Dynamic MQTT LWT birth, Close & Will Property - #2 by Steve-Mcl](https://discourse.nodered.org/t/dynamic-mqtt-lwt-birth-close-will-property/58951/2)
