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:
With topic values:
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:
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:
String comparison:
In model property definitions:
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:
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:
String concatenation (since v1.5):
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:
Grouping conditions:
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:
Best Practices for Expressions
-
Use parentheses for clarity: Even when not strictly necessary, parentheses can make your expressions more readable.
-
Avoid deeply nested conditions: Break complex conditions into simpler parts for better readability.
-
Be mindful of type conversion: When comparing values, be explicit about types.
-
Comment complex expressions: For complex calculations, add comments explaining the logic.