Widgets/Condition

Switch Widget

Switch Widget

A "Condition" type widget. Switch Widget evaluates incoming data against multiple conditions and routes it to different output ports based on which condition is met first. Think of it as a multi-way conditional router that can send data down different paths.

The Switch Widget is ideal for creating branching logic with more than two paths, allowing you to route data through multiple decision points in a single widget.

 

Inputs

  • input (DataType: Anything) The incoming value that will be evaluated against the configured conditions. This value can be accessed as input within expressions.

Outputs

When used in Single Output Mode:

  • out (DataType: Same as evaluation result) Transmits the evaluation result of the first matching condition. The output value can be a template expression (e.g., input * 2 or "value: " + input).

  • else (DataType: Same as input or expression result) Transmits data when none of the conditions are met. By default, passes through the original input.

When used in Multiple Outputs Mode:

  • 1, 2, 3, ... (DataType: Same as evaluation result) Each condition gets its own numbered output port. When a condition is met, the evaluation result is sent to that condition's specific port.

  • else (DataType: Same as input or expression result) Transmits data when none of the conditions are met. By default, passes through the original input.


Custom Settings

  • Multiple Outputs Toggle between single output mode and multiple outputs mode.

    • Single Output Mode: All matching conditions send their results to the same out port. Only the first matching condition executes.

    • Multiple Outputs Mode: Each condition has its own numbered output port (1, 2, 3, etc.). Only the first matching condition executes.

    • Rows Set the number of conditions to evaluate (1-10). Each row represents a condition with its own expression and output value.


Rows Setting


  • Condition Table Configure each condition by setting:

    • Expression: A JavaScript-like expression that evaluates to true or false. You can reference the input using the keyword input. For example: input > 10, input === "active", or input.status === "complete". Supports the same expression syntax as the Expression Eval Widget.

    • Output: The value to output when the condition is met. This can be:

      • A literal value: "success", 100, true
      • A template expression: input * 2, input.toUpperCase(), or "Result: " + input
      • The keyword input to pass through the original value unchanged
      • Any expression supported by the Expression Eval Widget, including global variables ($vars), secrets ($secrets), and helper functions

  • Expression Syntax Both the Expression and Output fields support the same powerful expression evaluation as the Expression Eval Widget. This includes:

    • Access the incoming data using the input keyword
    • Use JavaScript operators: >, <, >=, <=, ===, !==, &&, ||
    • Access object properties: input.name, input.status
    • Use string methods: input.startsWith("test"), input.includes("keyword")
    • Perform math operations: input * 2, input + 10
    • Combine conditions: input > 10 && input < 20
    • Access global variables: $vars.variableName
    • Access secrets: $secrets.secretName
    • Use helper functions: $capitalize(), $uppercase(), $json(), $length(), etc.
    • Access date/time helpers: date.year, date.month, date.dayName, etc.

For complete expression syntax and available helpers, see the Expression Eval Widget documentation.


 

📌 Important Notes

  • Conditions are evaluated in order from top to bottom
  • Only the first matching condition will execute - subsequent conditions are not evaluated
  • If no conditions match, data is routed to the else port
  • The else port output can also be customized with template expressions
  • Variables can be highlighted outside of braces for easier readability in the expression editor
  • Expressions support both comparison operations and template transformations
  • You can access nested object properties using dot notation (e.g., input.user.name)
  • When switching between single and multiple output modes, connections to removed ports are automatically cleaned up

 

Examples

Basic Numeric Routing

This example demonstrates routing numbers to different outputs based on ranges:

Drag the example onto the workspace to explore and experiment!



In this example

  1. The Slider Widget outputs numbers from 0 to 100
  2. The Switch Widget evaluates three conditions:
    • If input < 30, outputs "Low" to port 1, which sends to and displays in the Text Widget
    • If input >= 30 && input < 60, outputs "Medium" to port 2, which sends to and displays in the Text Widget
    • If input >= 60 && input <= 70, outputs "High" to port 3, which sends to and displays in the Text Widget
  3. The else output port shows the original slider value when none of the conditions are met. In this example, the else port displays values when:
    • The input is greater than 70 (since the "High" condition only covers values up to 70)
    • The input is less than 0 (though the slider range is 0-100)

 

String Pattern Matching

This example shows how to route data based on string patterns:

Drag the example onto the workspace to explore and experiment!



In this example

  1. Three Text Widgets provide log messages with different prefixes: "Error: something went wrong.", "Warning: check this before continuing.", and "Successful: everything is ready."
  2. The Button Widgets trigger each Text Widget to send its message to the Switch Widget
  3. The Switch Widget checks the message prefix:
    • If it starts with "Error", routes to port 1Counter Widget
    • If it starts with "Warning", routes to port 2Counter Widget
    • Otherwise, routes to the "else" port → Counter Widget
  4. Each Counter Widget tracks how many messages of each type have been processed

Result: Messages are categorized and counted based on their prefix

 

Object Property Routing

This example demonstrates routing based on object properties and transforming the output:

Drag the example onto the workspace to explore and experiment!



In this example

  1. The Text Widget provides the user name "John"
  2. The Slider Widget controls the status value (0-3) for the user object
  3. The Object Widget creates a user object with name (from Text Widget) and status (from Slider Widget) properties
  4. The Switch Widget evaluates the numeric status and creates formatted messages:
    • If status is 1, outputs "{name} is online"
    • If status is 2, outputs "{name} is busy"
    • If status is 3, outputs "{name} is offline"
    • If status is 0, outputs empty
  5. The formatted message is sent to and displayed in the Text Widget

Result: User status is converted to human-readable messages based on numeric status values

 

📌 Additional Notes

Comparison with Conditional Widget

The Switch Widget and Conditional Widget serve different purposes:

Use Switch Widget when:

  • You need to evaluate multiple conditions (more than 2 paths)
  • Each condition can produce a different transformed output
  • You want conditions evaluated in a specific order (top to bottom)
  • You need powerful expression evaluation for both conditions and outputs

Use Conditional Widget when:

  • You only need a simple true/false branch (2 paths)
  • You want to pass data unchanged based on a comparison
  • You need pre-defined comparison operators (Equal, Greater, Less, etc.)
  • Simpler configuration is preferred

 

Glossary

  • Expression

    A JavaScript-like statement that evaluates to a value. Can include comparisons, calculations, string operations, and object property access.

  • Template Expression

    An expression that transforms data to produce output. For example, input * 2 or "Result: " + input.