Skip to main content

What are Models?

Models in LoT are like database schemas for MQTT data. They define the structure of your data, ensure consistency across your system, and provide automatic JSON formatting.
Models transform raw MQTT messages into structured, typed data - eliminating the need for external data transformation layers.

Model Capabilities

Data Structuring

Define fields with specific types (STRING, INT, DOUBLE, BOOL, OBJECT, ARRAY) to create consistent data schemas.

Auto JSON Handling

Automatically explode incoming JSON payloads into individual topics for easy field access.

Data Aggregation

Combine data from multiple MQTT topics into a single structured output.

Calculations

Perform inline calculations and conditional logic to derive new values.

How Models Work

Models receive data, structure it according to your schema, and publish it as JSON. Understanding this flow is essential before diving into the different model types.

Receiving JSON Payloads

All models automatically handle incoming JSON payloads by “exploding” them into individual topics. This makes each field accessible for other models and actions:
DEFINE MODEL SensorData WITH TOPIC "sensors/device1/data"
When JSON arrives at sensors/device1/data:
{
  "temperature": 23.5,
  "humidity": 65,
  "status": "active"
}
LoT automatically creates:
  • sensors/device1/data/temperature23.5
  • sensors/device1/data/humidity65
  • sensors/device1/data/status"active"
This “explosion” behavior allows you to subscribe to individual fields or use them in other models and actions.

Producing Structured Output

When a model triggers (either automatically or via action), it publishes structured JSON combining all its fields:
DEFINE MODEL EquipmentStatus WITH TOPIC "equipment/formatted"
    ADD STRING "equipment_id" WITH TOPIC "equipment/id"
    ADD STRING "status" WITH TOPIC "equipment/status" AS TRIGGER
    ADD INT "runtime_hours" WITH TOPIC "equipment/runtime"
    ADD STRING "last_update" WITH TIMESTAMP "UTC"
Produces:
{
  "equipment_id": "PUMP001",
  "status": "RUNNING",
  "runtime_hours": 1250,
  "last_update": "2025-10-25T14:30:15Z"
}

Model Types

LoT supports three model patterns, each suited for different scenarios:
PatternUse When
Basic ModelYou want automatic triggering on data changes
Template Model (COLLAPSED)You need control over when and where data is created
Inheritance (FROM)You have related data types sharing common fields

Basic Models

Basic models automatically trigger and publish structured data when their trigger field updates:
DEFINE MODEL SensorReading WITH TOPIC "sensors/formatted/temperature"
    ADD STRING "sensor_id" WITH "TEMP001"
    ADD DOUBLE "value" WITH TOPIC "sensors/raw/temperature" AS TRIGGER
    ADD STRING "unit" WITH "celsius"
    ADD STRING "timestamp" WITH TIMESTAMP "UTC"
Key characteristics:
  • WITH TOPIC specifies where output is published
  • AS TRIGGER defines which field change causes publication
  • Runs automatically when trigger field updates

Template Models (COLLAPSED)

Template models are reusable schemas for dynamic data creation from within actions. The COLLAPSED keyword indicates they have no automatic trigger:
DEFINE MODEL AlarmRecord COLLAPSED
    ADD STRING "alarm_id"
    ADD STRING "equipment_id"
    ADD STRING "severity"
    ADD STRING "timestamp"

DEFINE ACTION ProcessAlarm
ON TOPIC "alarms/+/input" DO
    SET "equip_id" WITH TOPIC POSITION 2
    
    PUBLISH MODEL AlarmRecord TO "alarms/structured/" + {equip_id} WITH
        alarm_id = (RANDOM UUID)
        equipment_id = {equip_id}
        severity = (GET JSON "severity" IN PAYLOAD AS STRING)
        timestamp = TIMESTAMP "UTC"
Key characteristics:
  • COLLAPSED means no automatic trigger
  • Published explicitly with PUBLISH MODEL
  • Full control over when and where data is created
Learn how to publish template models from actions in Publishing Models.

Model Inheritance (FROM)

Create model families with shared fields using the FROM keyword:
DEFINE MODEL BaseAlert COLLAPSED
    ADD STRING "alert_id"
    ADD STRING "timestamp"
    ADD STRING "severity"

DEFINE MODEL TemperatureAlert FROM BaseAlert
    ADD DOUBLE "temperature_value"
    ADD DOUBLE "threshold"
Key characteristics:
  • Child models inherit all parent fields
  • Add specialized fields as needed
  • Promotes code reuse and consistency
Learn more about designing model hierarchies in Model Inheritance.

Data Sources

Model fields can be populated from various sources:
Use static values for constants like units, default thresholds, or fixed identifiers that never change:
DEFINE MODEL StaticExample WITH TOPIC "output/static"
    ADD STRING "unit" WITH "celsius"
    ADD INT "default_threshold" WITH 100
    ADD BOOL "enabled" WITH TRUE

Next Steps