> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreflux.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Introduction to LoT

> Learn what LoT (Language of Things) is and see it in action with simple examples

## What is LoT?

LoT (Language of Things) is a human-readable language for IoT automation. It uses near-English syntax to define logic, data structures, and integrations—all executed directly within the Coreflux MQTT broker.

Everything in LoT is built by defining four types of entities — **Actions**, **Models**, **Routes**, and **Rules**. Each one handles a different part of your automation, and inside them you use commands like `PUBLISH`, `SET`, and `GET` to make things happen.

> [!TIP]
>
> Think of LoT as SQL for real-time MQTT data. Just as SQL transforms database records, LoT transforms live MQTT streams.

> [!TIP]
>
> **Like building with LEGO blocks.** You pick the right block for the job — Actions for logic, Models for data structure, Routes for connections, Rules for security — and snap them together into a complete system.

## The Four Building Blocks

Everything in LoT is built from four core constructs, each created with the `DEFINE` keyword:

| Construct | Purpose | Trigger Types |
| - | - | - |
| **Actions** | Execute logic when events occur | Time, Topic, System events, Manual |
| **Models** | Structure MQTT data into JSON schemas | Topic values, Action calls |
| **Routes** | Connect to external systems | Topic patterns |
| **Rules** | Control who can publish/subscribe | Client identity, Topic patterns |

> **[Actions](./actions/overview)**
>
> Event-driven logic triggered by time or MQTT topics.

> **[Models](./models/overview)**
>
> Data schemas that structure MQTT messages into JSON.

> **[Rules](./rules/overview)**
>
> Access control for topics and operations.

> **[Routes](./routes/overview)**
>
> Connect to databases, brokers, and external services.

***

## How They Work Together

A typical LoT solution combines all four building blocks. Each definition below uses valid LoT syntax — the same patterns you will see in the [Models](/v2.14/lot-language/models/overview), [Actions](/v2.14/lot-language/actions/overview), [Routes](/v2.14/lot-language/routes/overview), and [Rules](/v2.14/lot-language/rules/overview) references.

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
-- 1. MODEL: When raw temperature updates, publish structured JSON per sensor
DEFINE MODEL SensorReading WITH TOPIC "sensors/+/formatted"
    ADD STRING "sensor_id" WITH TOPIC "sensors/+/id"
    ADD DOUBLE "temperature" WITH TOPIC "sensors/+/raw" AS TRIGGER
    ADD STRING "unit" WITH "celsius"
    ADD STRING "timestamp" WITH TIMESTAMP "UTC"

-- 2. ACTION: React to formatted readings and raise alerts
DEFINE ACTION TemperatureAlert
ON TOPIC "sensors/+/formatted" DO
    SET "sensor_id" WITH TOPIC POSITION 2
    SET "temp" WITH (GET JSON "temperature" IN PAYLOAD AS DOUBLE)
    IF {temp} > 80 THEN
        PUBLISH TOPIC "alerts/" + {sensor_id} + "/high_temp" WITH "Temperature above threshold"

-- 3. ROUTE: Persist sensor traffic to PostgreSQL
DEFINE ROUTE SensorDB WITH TYPE POSTGRESQL
    ADD SQL_CONFIG
        WITH SERVER "postgres.example.com"
        WITH PORT '5432'
        WITH DATABASE "iot_data"
        WITH USERNAME "iot_user"
        WITH PASSWORD "secure_password"
    ADD EVENT StoreSensorData
        WITH SOURCE_TOPIC "sensors/#"
        WITH QUERY "INSERT INTO sensor_readings (topic, payload) VALUES ('{topic}', '{value.json}')"

-- 4. RULE: Only supervisors (or root) may publish to alert topics
DEFINE RULE RestrictAlertPublish WITH PRIORITY 100 FOR Publish TO TOPIC "alerts/#"
    IF USER IN GROUP "supervisors" OR USER IS "root" THEN
        ALLOW
    ELSE
        DENY
```

| Step | What Happens |
| - | - |
| 1 | When `sensors/temp001/raw` updates, the Model publishes JSON to `sensors/temp001/formatted` |
| 2 | The Action reads `temperature` from that JSON and publishes to `alerts/temp001/high_temp` when it exceeds 80 |
| 3 | The Route inserts every message under `sensors/#` (raw, formatted, and related topics) into PostgreSQL |
| 4 | The Rule denies publish to `alerts/#` unless the client is in `supervisors` or is `root` |

***

## What You'll Learn on This Page

Below you'll find practical examples showing LoT in action - from simple heartbeats to data transformation. Each example demonstrates a core capability you can implement immediately.

***

## Hello World

Three examples showing what LoT can do:

```lot Time-Based Action wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ACTION Heartbeat
ON EVERY 5 SECONDS DO
    PUBLISH TOPIC "system/alive" WITH "ok"
```

```lot Topic-Based Action wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ACTION Echo
ON TOPIC "input/message" DO
    PUBLISH TOPIC "output/message" WITH PAYLOAD
```

```lot Data Model wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE MODEL SensorData WITH TOPIC "sensors/formatted"
    ADD STRING "id" WITH "TEMP001"
    ADD DOUBLE "value" WITH TOPIC "sensors/raw" AS TRIGGER
```

| Example | Trigger | What it does |
| - | - | - |
| **Heartbeat** | Every 5 seconds | Publishes "ok" to indicate the system is alive |
| **Echo** | Message on `input/message` | Forwards the payload to another topic |
| **SensorData** | Value on `sensors/raw` | Formats raw sensor data into structured JSON |

***

## Next Steps

> **[Best Practices & Patterns](/v2.14/quick-start/best-practices)**
>
> Naming, topic design, reusable logic, and pitfalls to avoid.

> **[Actions Overview](/v2.14/lot-language/actions/overview)**
>
> Learn how Actions automate MQTT data processing.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.