Switch

What is the Switch node used for in Node-RED

The Switch node allows you to route messages based on certain conditions. It acts as a decision-making tool within your flow, allowing you to define rules for directing messages to different output branches.

Here are some common use cases for using the Switch node in Node-RED:

Message Filtering: You can use the Switch node to filter messages based on specific criteria. For example, you might want to filter out messages that don't meet a certain threshold or that don't contain certain keywords.

Conditional Routing: The Switch node enables you to route messages down different paths in your flow based on conditions. You can set up rules that determine which output branch a message should be sent to, depending on its content or properties.

Event Processing: If you're working with events or data streams, the Switch node can help you process different types of events differently. For instance, you might have events related to temperature and humidity readings, and you want to process them separately.

Value Conversion: In cases where you need to convert values from one format to another, the Switch node can route messages to different converters based on the incoming value's properties.

Error Handling: When working with data or APIs, you might receive error messages that need to be handled differently from regular data. The Switch node can direct error messages to a separate branch for appropriate handling.

Language or Region-Based Processing: In applications involving localization or multilingual support, the Switch node can route messages based on language or region information in the message.

Examples

Switch Node Example

Node Documentation

Route messages based on their property values or sequence position.

Details

When a message arrives, the node will evaluate each of the defined rules and forward the message to the corresponding outputs of any matching rules.

Optionally, the node can be set to stop evaluating rules once it finds one that matches.

The rules can be evaluated against an individual message property, a flow or global context property, environment variable or the result of a JSONata expression.

Rules

There are four types of rule:

  1. Value rules are evaluated against the configured property
  2. Sequence rules can be used on message sequences, such as those generated by the Split node
  3. A JSONata Expression can be provided that will be evaluated against the whole message and will match if the expression returns a true value.
  4. An Otherwise rule can be used to match if none of the preceeding rules have matched.

Notes

The is true/false and is null rules perform strict comparisons against those types. They do not convert between types.

The is empty and is not empty rules can be used to test the length of Strings, Arrays and Buffers, or the number of properties an Object has. Neither rule will pass if the property being tested has a boolean, null or undefined value.

Handling message sequences

By default, the node does not modify the msg.parts property of messages that are part of a sequence.

The recreate message sequences option can be enabled to generate new message sequences for each rule that matches. In this mode, the node will buffer the entire incoming sequence before sending the new sequences on. The runtime setting nodeMessageBufferMaxLength can be used to limit how many messages nodes will buffer.