> ## 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>

<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.
</Tip>

## 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    |

<CardGroup cols={2}>
  <Card title="Actions" icon="bolt" href="./actions/overview">
    Event-driven logic triggered by time or MQTT topics.
  </Card>

  <Card title="Models" icon="database" href="./models/overview">
    Data schemas that structure MQTT messages into JSON.
  </Card>

  <Card title="Rules" icon="shield" href="./rules/overview">
    Access control for topics and operations.
  </Card>

  <Card title="Routes" icon="route" href="./routes/overview">
    Connect to databases, brokers, and external services.
  </Card>
</CardGroup>

***

## 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.0/lot-language/models/overview), [Actions](/v2.0/lot-language/actions/overview), [Routes](/v2.0/lot-language/routes/overview), and [Rules](/v2.0/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 trusted clients may publish to alert topics
DEFINE RULE RestrictAlertPublish WITH PRIORITY 10 FOR Publish TO TOPIC "alerts/#"
    IF USER HAS AllowedSystemAlerts 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 has `AllowedSystemAlerts` 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:

<CodeGroup>
  ```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
  ```
</CodeGroup>

| 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

<CardGroup cols={2}>
  <Card title="Best Practices & Patterns" icon="book-open" href="/v2.0/quick-start/best-practices">
    Naming, topic design, reusable logic, and pitfalls to avoid.
  </Card>

  <Card title="Actions Overview" icon="bolt" href="/v2.0/lot-language/actions/overview">
    Learn how Actions automate MQTT data processing.
  </Card>
</CardGroup>
