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:
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.

Writing Python Scripts

Every Python script in LoT must declare its name on the first line using a # Script Name: comment. That name is what you use in CALL PYTHON "ScriptName.function_name"—the part before the dot must match the header exactly.
The first line must be # Script Name: YourScriptName. Without this header, the broker does not register the script and CALL PYTHON cannot find it.
Define one or more functions below the header. Each function is callable from LoT by name.

CALL PYTHON Syntax

Parameter Passing

Single Parameter

Multiple Parameters

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 script:
LoT action calling the function:

JSON Objects

For richer data, return a dictionary—it becomes JSON that LoT can parse: Python script returning a dictionary:
LoT action extracting fields from the result:

Error Handling

Best Practices

Put # Script Name: YourScriptName on line 1 of every Python script. The name must match the script name in CALL PYTHON "YourScriptName.function_name". If calls fail with an unknown script, check spelling and that the header is the very first line.
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

Operations Reference

Review all available LoT operations.

Actions Overview

Return to the Actions overview for pattern guidance.
Last modified on June 2, 2026