Skip to content

IF / THEN / ELSE Functional Keywords

Feature Since Version Notes
IF / THEN >v1.4.4 Core conditional logic
ELSE >v1.4.4 Optional alternative block

Overview

  • Description: These keywords implement conditional execution flow. An IF statement evaluates a condition; if true, the THEN block executes. Optionally, an ELSE block can be provided to execute if the condition is false. Every IF block must be terminated with ENDIF.

Signature

  • Syntax (with ELSE):
    IF <condition> THEN
        <statements_if_true>
    ELSE
        <statements_if_false>
    
  • Syntax (without ELSE):
    IF <condition> THEN
        <statements_if_true>
    

Parameters

  • <condition>: An Expression that evaluates to a boolean (true or false). Typically involves comparisons (==, >, <, etc.) and logical operators (AND, OR, NOT).
  • <statements_if_true>: One or more LOT statements to execute if the condition is true.
  • <statements_if_false>: (Optional) One or more LOT statements to execute if the condition is false.

Usage Examples

Basic Example (No ELSE)

Publish an alert only if the temperature exceeds a threshold.

DEFINE ACTION TemperatureAlert
ON TOPIC "sensors/temperature" DO
    IF PAYLOAD > 30 THEN
        PUBLISH TOPIC "alerts/temp" WITH "Temperature High!"

Intermediate Example (With ELSE)

Set a status based on a value.

DEFINE ACTION SetDeviceStatus
ON TOPIC "device/input" DO
    IF PAYLOAD == "ON" THEN
        KEEP TOPIC "internal/status" WITH "Active"
    ELSE
        KEEP TOPIC "internal/status" WITH "Inactive"

Advanced Example (Nested IF and Logical Operators)

Handle multiple conditions for complex alerting.

DEFINE ACTION ComplexAlerting
ON TOPIC "system/monitor" DO
    SET "cpu" = GET TOPIC "system/cpu_usage"
    SET "mem" = GET TOPIC "system/mem_usage"

    IF {cpu} > 90 OR {mem} > 95 THEN
        PUBLISH TOPIC "alerts/system" WITH "Critical Resource Usage!"
    ELSE
        IF {cpu} > 75 OR {mem} > 80 THEN
            PUBLISH TOPIC "warnings/system" WITH "High Resource Usage"

Example in Models

Conditional property definition within a DEFINE MODEL block.

DEFINE MODEL SensorStatus WITH TOPIC "sensor/status"
    ADD "value" WITH TOPIC "sensor/raw" AS TRIGGER
    ADD "statusText" WITH IF value > 100 THEN "Overload" ELSE "Normal"

Notes & Additional Information

  • Code blocks within IF and ELSE are defined by indentation.
  • Conditions rely heavily on Comparison and Logical Operators.
  • IF statements can be nested.