Skip to content

KEEP TOPIC Functional Keyword

Feature Since Version Notes
KEEP TOPIC >v1.5.5 Stores data internally

1. Overview

  • Description: Stores data internally within the broker, associated with a specified MQTT topic. Unlike PUBLISH TOPIC, this data is not broadcast to external subscribers but is accessible within the broker using GET TOPIC for use in other Actions or Models.

2. Signature

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

3. Parameters

  • "<topicPath>": The internal topic path (string) where the data will be stored.
  • <value>: The data to store. Can be a literal (string, number), an Entity like PAYLOAD or TIMESTAMP, or the result of an Expression.

4. Usage Examples

Basic Example

Stores the incoming payload internally when an event occurs.

DEFINE ACTION StoreSensorValueInternally
ON TOPIC "sensors/+/temperature" DO
    KEEP TOPIC "internal/last_temp" WITH PAYLOAD
  • Scenario: A message 25.5 arrives on sensors/A/temperature. The value 25.5 is stored internally at internal/last_temp.

Intermediate Example

Keeps track of the last command received.

DEFINE ACTION TrackLastCommand
ON TOPIC "commands/device/set" DO
    KEEP TOPIC "internal/device/last_command" WITH PAYLOAD
    KEEP TOPIC "internal/device/last_command_time" WITH TIMESTAMP "ISO"

Advanced Example

Stores a calculated value internally.

DEFINE ACTION StoreAverage
ON TOPIC "data/point" DO
    SET current_sum = GET TOPIC "internal/sum"
    SET current_count = GET TOPIC "internal/count"

    IF current_sum == EMPTY THEN SET current_sum = 0
    IF current_count == EMPTY THEN SET current_count = 0

    SET new_sum = current_sum + PAYLOAD
    SET new_count = current_count + 1
    SET new_average = new_sum / new_count

    KEEP TOPIC "internal/sum" WITH new_sum
    KEEP TOPIC "internal/count" WITH new_count
    KEEP TOPIC "internal/average" WITH new_average

5. Notes & Additional Information

  • KEEP TOPIC is essential for maintaining state or storing intermediate values within the broker without exposing them externally.
  • The stored value can be retrieved using GET TOPIC "<topicPath>".