Skip to main content

Overview

While LoT handles most IoT automation elegantly, some tasks need the full power of a programming language. Python integration lets you call Python functions directly from your LoT actions—no external services, no network calls, no containers. Your Python code runs inside the broker alongside your actions.
Think of Python as a specialist you can call for help. Your LoT action handles the routine work (reading sensors, routing messages, checking thresholds), but when it encounters something complex—statistical analysis, regex parsing, or calling an external library—it hands the task to Python and gets the result back instantly.

When to Use Python vs. LoT

Before diving into syntax, understand when Python makes sense:
Use Python ForUse LoT For
Complex calculations (statistics, ML)Simple math (+, -, *, /)
Regex and advanced string parsingString concatenation
Data validation with complex rulesBasic conditionals (IF/THEN)
External library calls (numpy, pandas)MQTT operations (PUBLISH, GET TOPIC)
JSON transformation and restructuringField extraction (GET JSON)
If you can do it in LoT, do it in LoT. Python adds overhead—use it when LoT’s built-in operations aren’t enough.

In This Page

SectionDescription
CALL PYTHON SyntaxBasic syntax for calling Python functions
Parameter PassingPass data from LoT to Python
Return Value HandlingHandle Python return values in LoT
Error HandlingGracefully handle Python errors
Best PracticesPerformance and reliability tips

CALL PYTHON Syntax

CALL PYTHON "ScriptName.function_name"
    WITH (parameter1, parameter2, ...)
    RETURN AS {variable_name}

Parameter Passing

Single Parameter

CALL PYTHON "Processor.validate" 
    WITH (PAYLOAD) 
    RETURN AS {result}

Multiple Parameters

SET "min_val" WITH (GET TOPIC "config/min")
SET "max_val" WITH (GET TOPIC "config/max")

CALL PYTHON "Validator.check_range" 
    WITH (PAYLOAD, {min_val}, {max_val}) 
    RETURN AS {result}

Return Value Handling

Python functions can return simple values or complex JSON objects. The RETURN AS clause captures the result into a LoT variable.

Simple Values

For basic calculations, return a single value: Python function:
def add_numbers(a, b):
    return float(a) + float(b)
LoT action calling the function:
CALL PYTHON "Calculator.add_numbers" WITH (5, 3) RETURN AS {result}
PUBLISH TOPIC "math/result" WITH {result}

JSON Objects

For richer data, return a dictionary—it becomes JSON that LoT can parse: Python function returning a dictionary:
def analyze_data(value):
    return {
        "original": float(value),
        "doubled": float(value) * 2,
        "squared": float(value) ** 2
    }
LoT action extracting fields from the result:
CALL PYTHON "Analyzer.analyze_data" WITH (5) RETURN AS {result}

PUBLISH TOPIC "analysis/full" WITH {result}
PUBLISH TOPIC "analysis/doubled" WITH (GET JSON "doubled" IN {result} AS DOUBLE)

Error Handling

CALL PYTHON "Calculator.safe_division" WITH (10, 2) RETURN AS {result}

SET "success" WITH (GET JSON "success" IN {result} AS BOOL)

IF {success} EQUALS TRUE THEN
    PUBLISH TOPIC "calculation/result" WITH (GET JSON "result" IN {result} AS DOUBLE)
ELSE
    PUBLISH TOPIC "calculation/error" WITH (GET JSON "error" IN {result} AS STRING)

Best Practices

Target execution times under 100ms. Long-running Python functions block your action and can delay message processing. For heavy computations, consider offloading to an external service.
Wrap Python logic in try/except blocks and return structured error responses. This lets your LoT action gracefully handle failures instead of crashing.
Return dictionaries, lists, strings, numbers, and booleans. Custom objects and classes won’t serialize properly. When in doubt, convert to a dictionary.
Python functions receive parameters as strings by default. Always convert types explicitly (e.g., float(value), int(count)) and validate ranges before processing.

Next Steps