# How can I make switch block data?

**URL:** <https://discourse.nodered.org/t/how-can-i-make-switch-block-data/61979>\
**Category:** Dashboard\
**Tags:** function-node\
**Created:** [29 April 2022 22:33 UTC](https://discourse.nodered.org/t/how-can-i-make-switch-block-data/61979 "2022-04-29T22:33:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohannad\_Alobaid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mohannad_alobaid/32/60208_2.png) [@Mohannad\_Alobaid](https://discourse.nodered.org/u/Mohannad_Alobaid)\
**Post date:** [29 April 2022 22:33 UTC](https://discourse.nodered.org/t/how-can-i-make-switch-block-data/61979/1 "2022-04-29T22:33:15Z")

</div>

Welcome  
Please I need help  
I have data that comes from a sen

 ![image_2022-04-30_01-32-46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3f14cf00d117a9cb67187e6313c036e81a56c6a5.png)  
sor and I want to use a switch to block the sensor and activate it.  
When the switch is set to true, the output value will be zero  
When false, the output value becomes the real sensor value that comes through the MQTT protocol

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 April 2022 22:46 UTC](https://discourse.nodered.org/t/how-can-i-make-switch-block-data/61979/2 "2022-04-29T22:46:31Z")

</div>

Ok, you haven't quite for the idea of the `switch` node.  
No problems.

There are a couple of ways to do this.

Before I go on, mind if I ask:  
How good do you know node-red?

---

<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:** [29 April 2022 23:46 UTC](https://discourse.nodered.org/t/how-can-i-make-switch-block-data/61979/3 "2022-04-29T23:46:07Z")

</div>

A switch on the node-red dashboard doesn't work like a light switch, blocking or permitting data to flow.  
What it does do is send a message such as "on" or "off" when you toggle it.

You can use this message to control a gate node (node-red-contrib-simple-gate, which you have to install via the hamburger menu / manage palette)

The gate will allow messages through if it receives a message with topic set to "control" and payload something like "on" or "open", block messages if it receives "off".

Put the gate between your mqtt and function nodes.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 April 2022 00:42 UTC](https://discourse.nodered.org/t/how-can-i-make-switch-block-data/61979/4 "2022-04-30T00:42:36Z")

</div>

Here is a basic flow to show you how to use a switch to do what you want.

```auto
[{"id":"57fb4f7ab53b9793","type":"inject","z":"0918ee609bf69fc7","name":"Your data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":3800,"y":180,"wires":[["f562bf88970fe1fb"]]},{"id":"1a90e07956bcd8f4","type":"inject","z":"0918ee609bf69fc7","name":"ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ON","payloadType":"str","x":3780,"y":330,"wires":[["9d7bc6c2299e5369"]]},{"id":"54e6bdb5474e136b","type":"inject","z":"0918ee609bf69fc7","name":"OFF","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"OFF","payloadType":"str","x":3780,"y":290,"wires":[["9d7bc6c2299e5369"]]},{"id":"9d7bc6c2299e5369","type":"change","z":"0918ee609bf69fc7","name":"","rules":[{"t":"set","p":"switch1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":3940,"y":310,"wires":[[]]},{"id":"f562bf88970fe1fb","type":"switch","z":"0918ee609bf69fc7","name":"","property":"switch1","propertyType":"flow","rules":[{"t":"eq","v":"ON","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":3940,"y":180,"wires":[["64e2f07cced3321a"]]},{"id":"64e2f07cced3321a","type":"debug","z":"0918ee609bf69fc7","name":"Gauge","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":4110,"y":180,"wires":[]},{"id":"8a9801d6b03deea1","type":"comment","z":"0918ee609bf69fc7","name":"This is basically the switch node's output","info":"","x":3790,"y":260,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Mohannad\_Alobaid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mohannad_alobaid/32/60208_2.png) [@Mohannad\_Alobaid](https://discourse.nodered.org/u/Mohannad_Alobaid)\
**Post date:** [30 April 2022 06:42 UTC](https://discourse.nodered.org/t/how-can-i-make-switch-block-data/61979/5 "2022-04-30T06:42:00Z")

</div>

I am a beginner  
thanks for help me

---

<div class="post-metadata">

**Author:** ![Mohannad\_Alobaid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mohannad_alobaid/32/60208_2.png) [@Mohannad\_Alobaid](https://discourse.nodered.org/u/Mohannad_Alobaid)\
**Post date:** [30 April 2022 06:44 UTC](https://discourse.nodered.org/t/how-can-i-make-switch-block-data/61979/6 "2022-04-30T06:44:45Z")

</div>

I've tried this but it's not the idea I mean

---

<div class="post-metadata">

**Author:** ![Mohannad\_Alobaid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mohannad_alobaid/32/60208_2.png) [@Mohannad\_Alobaid](https://discourse.nodered.org/u/Mohannad_Alobaid)\
**Post date:** [30 April 2022 07:27 UTC](https://discourse.nodered.org/t/how-can-i-make-switch-block-data/61979/7 "2022-04-30T07:27:27Z")

</div>

![image_2022-04-30_10-26-09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/c/2cb4367240c0cceb8fa7a68dbcc462a9a76ac707.png)  
thanks alot i solve the problem

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 April 2022 20:43 UTC](https://discourse.nodered.org/t/how-can-i-make-switch-block-data/61979/8 "2022-04-30T20:43:14Z")

</div>

That isn't what I posted.

As you have/are using a `gate` node you can do it completely differently.

Like this:

```auto
[{"id":"37afa4b10d8ca914","type":"gate","z":"0918ee609bf69fc7","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":3970,"y":380,"wires":[["3ebe9349d1dae62f"]]},{"id":"98651a624e367aec","type":"inject","z":"0918ee609bf69fc7","name":"ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"open","payloadType":"str","x":3830,"y":480,"wires":[["37afa4b10d8ca914"]]},{"id":"3f60c149e579a96a","type":"inject","z":"0918ee609bf69fc7","name":"OFF","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"close","payloadType":"str","x":3830,"y":440,"wires":[["37afa4b10d8ca914"]]},{"id":"3ebe9349d1dae62f","type":"debug","z":"0918ee609bf69fc7","name":"Gauge","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":4100,"y":380,"wires":[]},{"id":"d022659d54ce5de9","type":"inject","z":"0918ee609bf69fc7","name":"Your data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":3820,"y":380,"wires":[["37afa4b10d8ca914"]]}]

```

I didn't suggest this way as using non-standard nodes is a bit confusing to new people who may not have got that far.

Is this way clearer?

---

<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:** [30 May 2022 20:43 UTC](https://discourse.nodered.org/t/how-can-i-make-switch-block-data/61979/9 "2022-05-30T20:43:16Z")

</div>

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