Skip to main content
Actions are executable logic blocks triggered by events such as timed intervals, topic updates, or explicit calls. They are the primary way to implement automation in LoT.
Actions run inside the MQTT broker itself, eliminating the need for external middleware or application servers.

In This Page

SectionDescription
Basic SyntaxDEFINE ACTION structure and components
Trigger TypesTime-based, topic-based, and callable actions
Variable OperationsSET, GET TOPIC, GET JSON
Topic OperationsPUBLISH and KEEP
Control FlowIF/THEN/ELSE conditionals
Type CastingConverting between data types
Complete ExampleFull working action

Basic Syntax

DEFINE ACTION <action_name>
ON <EVENT_TRIGGER>
DO
    <action_logic>

Components

action_name
string
required
A unique identifier for the action. Use descriptive names that indicate the action’s purpose (e.g., SensorDataProcessor, HeartbeatMonitor).
EVENT_TRIGGER
trigger
required
Specifies when the action executes. Can be time-based (ON EVERY), topic-based (ON TOPIC), or omitted for callable actions.
action_logic
statements
required
One or more LoT statements that execute when the action triggers. These can include publishing, variable assignment, conditionals, and Python calls.

Action Structure

Every action follows this general structure:
DEFINE ACTION ActionName
ON <trigger_type> DO
    // Variable assignment
    SET "variable" WITH <value>
    
    // Conditional logic
    IF <condition> THEN
        // Statements
    ELSE
        // Alternative statements
    
    // Topic operations
    PUBLISH TOPIC "output/topic" WITH <payload>
    KEEP TOPIC "internal/topic" WITH <payload>

Trigger Types

Execute at regular intervals:
DEFINE ACTION PeriodicTask
ON EVERY 5 SECONDS DO
    PUBLISH TOPIC "system/heartbeat" WITH "alive"
Supported units: SECOND(S), MINUTE(S), HOUR(S), DAY(S), WEEK(S)

Compatible Keywords

Keywords that can be used within action logic:

Variable Operations

KeywordDescription
SETCreate or assign a variable
GET TOPICRetrieve data from a topic
GET JSONExtract fields from JSON payloads
// For topic "sensors/temp001/data": POSITION 1="sensors", POSITION 2="temp001", POSITION 3="data"
SET "sensor_id" WITH TOPIC POSITION 2
SET "temperature" WITH (GET TOPIC "sensors/temp" AS DOUBLE)
SET "humidity" WITH (GET JSON "humidity" IN PAYLOAD AS DOUBLE)

Topic Operations

KeywordDescription
PUBLISH TOPICBroadcast data to all subscribers
KEEP TOPICStore data internally (not broadcast)
PUBLISH TOPIC "output/data" WITH {temperature}
PUBLISH TOPIC "devices/" + {device_id} + "/status" WITH "online"
KEEP TOPIC "cache/last_reading" WITH PAYLOAD

Control Flow

KeywordDescription
IF ... THEN ... ELSEConditional execution
IF {temperature} > 80 THEN
    PUBLISH TOPIC "alerts/high_temp" WITH {temperature}
ELSE
    PUBLISH TOPIC "status/normal" WITH "OK"

Entities

EntityDescription
PAYLOADThe data that triggered the action
TOPIC POSITION nExtract segment from topic path
TIMESTAMPGenerate timestamps (UTC, UNIX)
EMPTYCheck if a topic has no data

Python Integration

KeywordDescription
CALL PYTHONExecute Python functions
Extend LoT with Python: Use CALL PYTHON to execute complex calculations, data validation, or integrate external libraries. See the Python Integration guide for complete syntax and examples.

Type Casting

Use AS to cast values when performing comparisons or calculations:
GET TOPIC "sensors/value" AS DOUBLE
GET TOPIC "config/enabled" AS BOOL
GET TOPIC "settings/count" AS INT
GET JSON "temperature" IN PAYLOAD AS DOUBLE
Supported types: STRING, INT, DOUBLE, BOOL, TIMESTAMP

Complete Example

Putting It All Together: This example demonstrates topic extraction, JSON parsing, conditional logic, and publishing - the core patterns you’ll use in most actions.
DEFINE ACTION SensorDataProcessor
ON TOPIC "sensors/+/raw" DO
    // Extract sensor ID from topic (position 2 = the wildcard)
    // Topic: sensors/temp001/raw → POSITION 1="sensors", POSITION 2="temp001", POSITION 3="raw"
    SET "sensor_id" WITH TOPIC POSITION 2
    
    // Get the temperature value
    SET "temp_value" WITH (GET JSON "temperature" IN PAYLOAD AS DOUBLE)
    
    // Check thresholds
    IF {temp_value} > 80 THEN
        PUBLISH TOPIC "alerts/high_temp/" + {sensor_id} WITH PAYLOAD
    ELSE
        PUBLISH TOPIC "sensors/processed/" + {sensor_id} WITH PAYLOAD
    
    // Always update the last reading timestamp
    PUBLISH TOPIC "sensors/" + {sensor_id} + "/last_update" WITH TIMESTAMP "UTC"

Next Steps