How can I make switch block data?

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


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

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?

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.

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

[{"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":[]}]

I am a beginner
thanks for help me

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


thanks alot i solve the problem

1 Like

That isn't what I posted.

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

Like this:

[{"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?

1 Like

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