Node-RED guide series
Node-RED, the "switch" node
The "switch" node allows your flow to execute one of several possible paths depending on the rules you have specified.
The "switch" node is like the "if" statement in a text programming language.
Like the "if" statement, you can use the "switch" node to define one or more logical rules. According to the truthfulness (or falseness") of those rules, the "switch" node can be configured to trigger one or more paths of execution.
You can find the "switch" node in the "function" group of the left tool bar.
Setup the "switch" node
To explain how to use the "switch" node, I have create this simple flow:
The flow begins with an "inject" node which will emit a numerical "6" when you click on the button.
The payload will go into the switch node, which will evaluate it. If the payload is larger than 5, then the top output will be triggered. If the payload is equal or less than 5, then the bottom output will be triggered.
Take a look inside the "switch" node:
First, you must set the property to evaluate. In this case, I have configure the node to get the value from the message payload.
Next, you must define at least one rule to be evaluated. The rule is defined by a boolean comparator, a value, and the output that will be triggered if the rule is evaluated to be true.
The available comparators are comprehensive:
The last field in the edit menu allows you to set the evaluation mode. You can set the node to stop when it finds a true rule, or to evaluate all rules.
Go ahead and try out this example. Deploy the flow, and then click on the "inject" node's button. The output will be "6" from node ">5", as expected:
Learn Node-RED and how to use it with the ESP32
This course will guide you through the construction of an automated control system. The deliverable is a Terrarium controller.
Along the way, you will learn a great deal about useful technologies such as the Node Red programming environment and MQTT.
Jump to another article
1. Introduction to Node-RED
2. Install Node-RED on the Raspberry Pi
6. Messages and Variables
7. The "complete" node
8. The "catch" node
9. The "link out" and "link in" nodes
10. The "switch" node
11. The "range" node
12. The "delay" node
13. The "trigger" node
14. The "RBE" (Report by Exception) node