GET TOPIC Functional Keyword
Feature | Since Version | Notes |
---|---|---|
GET TOPIC |
>v1.4.6 | Retrieves data from a topic |
1. Overview
- Description:
Instructs the broker to retrieve the current value stored (either internally via
KEEP TOPIC
or the last retained message published viaPUBLISH TOPIC
) for a specified MQTT topic.
2. Signature
- Syntax:
3. Parameters
"<topicPath>"
: The topic path (string) from which to retrieve the value. Can include wildcards (+
,#
) in some contexts, though typically used with specific paths.
4. Return Value
- Description:
Returns the value stored at the specified topic path. If the topic does not exist or has no stored value, it returns
EMPTY
.
5. Usage Examples
Basic Example
Retrieve a configuration setting and use it in a publish command.
DEFINE ACTION ApplyThreshold
ON TOPIC "sensors/temperature" DO
SET "threshold" = GET TOPIC "config/temp_threshold"
IF PAYLOAD > {threshold} THEN
PUBLISH TOPIC "alerts/temp" WITH "Temperature exceeded threshold!"
ENDIF
Intermediate Example
Check if a device is enabled before processing its data.
DEFINE ACTION ProcessIfEnabled
ON TOPIC "device/A/data" DO
// Assumes "config/device/A/enabled" stores "true" or "false"
IF GET TOPIC "config/device/A/enabled" == "true" THEN
PUBLISH TOPIC "processed/device/A" WITH (PAYLOAD * 0.5)
ENDIF
Advanced Example (Counter)
Increment a counter stored internally.
DEFINE ACTION IncrementCounter
ON TOPIC "events/trigger" DO
SET "currentCount" = GET TOPIC "internal/counter"
IF {currentCount} == EMPTY THEN
SET "newCount" = 1
ELSE
SET "newCount" = {currentCount} + 1
ENDIF
KEEP TOPIC "internal/counter" WITH {newCount} // Store updated count
PUBLISH TOPIC "public/counter" WITH {newCount} // Optionally publish it
6. Notes & Additional Information
GET TOPIC
is fundamental for accessing stored state or configuration within LOT.- Always consider checking the return value against
EMPTY
if there's a possibility the topic might not have a value yet.