Skip to content

PUBLISH TOPIC Functional Keyword

Feature Since Version Notes
PUBLISH TOPIC >v1.4.4 Core command to send data

1. Overview

  • Description: Instructs the broker to publish (broadcast) a message with a specified payload to a specified MQTT topic. Subscribed clients will receive this message.

2. Signature

  • Syntax:
    PUBLISH TOPIC "<topicPath>" WITH <value>
    

3. Parameters

  • "<topicPath>": The destination topic path (string) for the message.
  • <value>: The payload of the message. Can be a literal (string, number), an Entity like PAYLOAD or TIMESTAMP, the result of GET TOPIC, a {variable}, or an Expression.

4. Usage Examples

Basic Example

Publish a fixed status message.

DEFINE ACTION PublishStatus
ON EVERY 1 MINUTE DO
    PUBLISH TOPIC "system/status" WITH "Operational"

Intermediate Example

Publish the incoming payload to a different topic.

DEFINE ACTION ForwardData
ON TOPIC "raw/data" DO
    PUBLISH TOPIC "processed/data" WITH PAYLOAD

Advanced Example

Publish a calculated value based on multiple inputs.

DEFINE ACTION CalculateAndPublish
ON TOPIC "inputs/trigger" DO
    SET "temp" = GET TOPIC "sensors/temperature"
    SET "humidity" = GET TOPIC "sensors/humidity"

    IF {temp} != EMPTY AND {humidity} != EMPTY THEN
        SET "heatIndex" = {temp} * 1.1 + {humidity} * 0.05 // Simplified example formula
        PUBLISH TOPIC "weather/heat_index" WITH {heatIndex}
    ENDIF

5. Notes & Additional Information

  • PUBLISH TOPIC is the primary way LOT scripts interact with external MQTT clients.
  • Use KEEP TOPIC if you only need to store data internally within the broker.