Skip to content

LOT Operators & Expressions

Operators and expressions in LOT allow you to perform calculations, comparisons, and create complex logic. This page documents the available operators, their syntax, and usage in different contexts.

Mathematical Operators

Operator Description Since Version Notes
+ Addition >v1.4.4 Core feature
- Subtraction >v1.4.4 Core feature
* Multiplication >v1.4.4 Core feature
/ Division >v1.4.4 Core feature
% Modulo (remainder) >v1.4.4 Core feature

Mathematical operators work with numeric values and can be used in expressions.

Examples

Simple calculation:

PUBLISH TOPIC "math/result" WITH (5 + 3)  # Publishes 8

With topic values:

PUBLISH TOPIC "sensor/calculated" WITH (GET TOPIC "sensor/raw" * 2.5)

In models:

ADD "fahrenheit" WITH (celsius * 9/5 + 32)
ADD "area" WITH (width * height)
ADD "remainder" WITH (count % 10)

Operator Precedence

Mathematical operators follow standard precedence rules: 1. Parentheses () 2. Multiplication *, Division /, Modulo % 3. Addition +, Subtraction -

To ensure the intended calculation order, use parentheses:

ADD "result" WITH ((a + b) * c)

Comparison Operators

Operator Description Since Version Notes
== or = Equality >v1.4.4 Both forms are supported
!= Inequality >v1.4.4 Core feature
< Less than >v1.4.4 Core feature
> Greater than >v1.4.4 Core feature
<= Less than or equal to >v1.4.4 Core feature
>= Greater than or equal to >v1.4.4 Core feature

Comparison operators return a boolean result (true/false) and are typically used in conditional expressions.

Examples

In conditional statements:

IF GET TOPIC "temperature" > 30 THEN
    PUBLISH TOPIC "alarm" WITH "Temperature too high"

String comparison:

IF GET TOPIC "status" == "active" THEN
    PUBLISH TOPIC "system/log" WITH "System is active"

In model property definitions:

ADD "status" WITH IF temperature >= 25 THEN "hot" ELSE "normal"

Logical Operators

Operator Description Since Version Notes
AND Logical AND >v1.4.4 Both operands must be true
OR Logical OR >v1.4.4 Either operand must be true
NOT Logical NOT >v1.4.4 Inverts a boolean value

Logical operators combine or modify boolean expressions.

Examples

Combining conditions:

IF temperature > 30 AND humidity < 40 THEN
    PUBLISH TOPIC "environment/warning" WITH "Hot and dry conditions"

Using OR:

IF GET TOPIC "door" == "open" OR GET TOPIC "window" == "open" THEN
    PUBLISH TOPIC "security/alert" WITH "Perimeter breach"

Using NOT:

IF NOT (GET TOPIC "system" == "running") THEN
    PUBLISH TOPIC "system/status" WITH "System is not running"

In rules:

IF USER IS "admin" OR USER HAS AdminPrivileges THEN
    ALLOW

String Operations

Feature Description Since Version Notes
String literals Text in double quotes >v1.4.4 Core feature
String concatenation Joining strings >v1.5.8 Using the + operator

Examples

String literals:

PUBLISH TOPIC "greeting" WITH "Hello, world!"

String concatenation (since v1.5):

PUBLISH TOPIC "user/message" WITH ("Hello, " + username + "!")

Parentheses and Grouping

Feature Description Since Version Notes
() Grouping expressions >v1.4.4 Core feature

Parentheses can be used to group expressions and ensure the correct order of operations.

Examples

Ensuring operation order:

PUBLISH TOPIC "result" WITH ((a + b) * c)

Grouping conditions:

IF (temperature > 30 AND humidity < 40) OR alarmActive == "true" THEN

Type Handling

LOT provides automatic type conversion in many cases:

Scenario Behavior Since Version Notes
Numeric operations Auto-convert strings to numbers when possible >v1.4.4 Core feature
Boolean context Convert values to boolean in conditions >v1.4.4 Core feature
Empty value check Compare with empty string >v1.4.4 Core feature

Examples

Automatic number conversion:

# If "counter" contains the string "5", this will convert it to a number and add 1
PUBLISH TOPIC "counter" WITH (GET TOPIC "counter" + 1)

Empty check:

IF GET TOPIC "status" = "" THEN
    PUBLISH TOPIC "status" WITH "initializing"

Best Practices for Expressions

  1. Use parentheses for clarity: Even when not strictly necessary, parentheses can make your expressions more readable.

    ADD "result" WITH ((a + b) * c)  # More readable than a + b * c
    

  2. Avoid deeply nested conditions: Break complex conditions into simpler parts for better readability.

  3. Be mindful of type conversion: When comparing values, be explicit about types.

    # Better to compare with explicit string
    IF GET TOPIC "status" == "active" THEN
    

  4. Comment complex expressions: For complex calculations, add comments explaining the logic.