Skip to main content

switch

Route messages to different outputs based on property values or conditions.

Overview

The switch node routes messages based on property values or expressions. Like a traffic controller, it directs each message to one or more outputs based on configurable rules. Essential for branching logic in workflows.

Rules
Based Routing
Multi
Output
JSONata
Expressions
Regex
Matching

Properties

Property Type Default Description
property string "payload" Property to evaluate
rules array [] Array of routing rules
checkall boolean true Check all rules (vs stop at first match)
repair boolean false Recreate message sequences

Rule Types

Comparison Operators

  • == equals
  • != not equal
  • < less than
  • <= less than or equal
  • > greater than
  • >= greater than or equal

Value Checks

  • is true boolean true
  • is false boolean false
  • is null null value
  • is not null has a value
  • is of type type check
  • is empty empty string/array/object

String Operations

  • contains substring match
  • matches regex pattern match
  • starts with prefix match
  • ends with suffix match

Range & Special

  • is between range check
  • JSONata expression
  • sequence message sequence rules
  • otherwise catch-all

Example: Temperature Alert Routing

Route sensor readings to different handlers based on temperature ranges.

// Switch node configuration
{
  "property": "payload.temperature",
  "rules": [
    { "t": "gte", "v": "40", "vt": "num" },     // Output 1: Critical (≥40°C)
    { "t": "btwn", "v": "30", "v2": "40" },     // Output 2: Warning (30-40°C)
    { "t": "btwn", "v": "15", "v2": "30" },     // Output 3: Normal (15-30°C)
    { "t": "lt", "v": "15", "vt": "num" },      // Output 4: Cold (<15°C)
    { "t": "else" }                              // Output 5: Invalid/missing
  ],
  "checkall": false  // Stop at first match
}

Example: Route by MQTT Topic

Route messages based on their MQTT topic.

// Switch node configuration
{
  "property": "topic",
  "rules": [
    { "t": "cont", "v": "sensors/temperature" },  // Output 1
    { "t": "cont", "v": "sensors/humidity" },     // Output 2
    { "t": "cont", "v": "sensors/motion" },       // Output 3
    { "t": "regex", "v": "sensors/.*/alert" },    // Output 4: Any alert
    { "t": "else" }                               // Output 5: Other
  ]
}

// Example topics:
// "sensors/temperature/living-room" → Output 1
// "sensors/motion/garage" → Output 3
// "sensors/door/alert" → Output 4

Example: JSONata Expressions

Use complex expressions for advanced routing logic.

// Switch node with JSONata
{
  "property": "payload",
  "propertyType": "msg",
  "rules": [
    // Check if temperature exceeds dynamic threshold
    { "t": "jsonata", "v": "payload.temperature > payload.threshold" },

    // Check if any sensor has an error
    { "t": "jsonata", "v": "$count(payload.sensors[status='error']) > 0" },

    // Check time-based condition (night hours)
    { "t": "jsonata", "v": "$hour($now()) >= 22 or $hour($now()) < 6" },

    { "t": "else" }
  ]
}

Matching Behavior

Check All Rules

Message can go to multiple outputs if multiple rules match.

// Value: 50
// Rule 1: > 30 ✓ → Output 1
// Rule 2: > 40 ✓ → Output 2
// Rule 3: > 60 ✗
// Result: Sent to outputs 1 AND 2

Stop at First Match

Message only goes to the first matching output.

// Value: 50
// Rule 1: > 30 ✓ → Output 1 (STOP)
// Rule 2: > 40 (not checked)
// Rule 3: > 60 (not checked)
// Result: Sent to output 1 only

Related Nodes