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.When to Use Python vs. LoT
Before diving into syntax, understand when Python makes sense:| Use Python For | Use LoT For |
|---|---|
| Complex calculations (statistics, ML) | Simple math (+, -, *, /) |
| Regex and advanced string parsing | String concatenation |
| Data validation with complex rules | Basic conditionals (IF/THEN) |
| External library calls (numpy, pandas) | MQTT operations (PUBLISH, GET TOPIC) |
| JSON transformation and restructuring | Field 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
| Section | Description |
|---|---|
| CALL PYTHON Syntax | Basic syntax for calling Python functions |
| Parameter Passing | Pass data from LoT to Python |
| Return Value Handling | Handle Python return values in LoT |
| Error Handling | Gracefully handle Python errors |
| Best Practices | Performance and reliability tips |
CALL PYTHON Syntax
Parameter Passing
Single Parameter
Multiple Parameters
Return Value Handling
Python functions can return simple values or complex JSON objects. TheRETURN AS clause captures the result into a LoT variable.
Simple Values
For basic calculations, return a single value: Python function:JSON Objects
For richer data, return a dictionary—it becomes JSON that LoT can parse: Python function returning a dictionary:Error Handling
Best Practices
Keep Functions Fast
Keep Functions Fast
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.
Always Handle Errors
Always Handle Errors
Wrap Python logic in try/except blocks and return structured error responses. This lets your LoT action gracefully handle failures instead of crashing.
Return JSON-Serializable Types
Return JSON-Serializable Types
Return dictionaries, lists, strings, numbers, and booleans. Custom objects and classes won’t serialize properly. When in doubt, convert to a dictionary.
Validate Input Parameters
Validate Input Parameters
Python functions receive parameters as strings by default. Always convert types explicitly (e.g.,
float(value), int(count)) and validate ranges before processing.
