| title | Sensor Data Router |
|---|---|
| category | Action |
| version | 1.0.0 |
| last_updated | 2025-10-25 |
| description | Route sensor data using wildcards to extract sensor IDs and create structured topic hierarchies |
| related_tutorials | Tutorial/01-basics/topic-actions.lotnb |
The Sensor Data Router pattern demonstrates how to use wildcard topics (+) to match multiple sensors and extract context from the topic structure using TOPIC POSITION. This enables scalable, context-aware data routing for any number of sensors.
DEFINE ACTION SensorDataRouter
ON TOPIC "sensors/+/temperature" DO
SET "sensor_id" WITH TOPIC POSITION 1
PUBLISH TOPIC "processed/temperature/" + {sensor_id} WITH PAYLOAD
PUBLISH TOPIC "processed/temperature/" + {sensor_id} + "/timestamp" WITH TIMESTAMP "UTC"
- Wildcard Matching:
sensors/+/temperaturematches any sensor ID in the middle position - Context Extraction:
TOPIC POSITION 1extracts the sensor ID (0-indexed positions) - Dynamic Routing: Routes data to sensor-specific processed topics
- Timestamp Addition: Adds timestamps for data tracking
For topic sensors/temp001/temperature:
TOPIC POSITION 0="sensors"TOPIC POSITION 1="temp001"TOPIC POSITION 2="temperature"
sensors/temp001/temperature- Temperature from sensor temp001sensors/temp002/temperature- Temperature from sensor temp002sensors/pressXYZ/temperature- Temperature from any sensor
processed/temperature/{sensor_id}- Processed temperature readingprocessed/temperature/{sensor_id}/timestamp- Processing timestamp
Publish to: sensors/temp001/temperature
Payload: 25.5
Results:
processed/temperature/temp001:25.5processed/temperature/temp001/timestamp:"2025-10-25T14:30:15Z"
- Multi-Sensor Systems: Handle any number of sensors with one action
- Data Normalization: Transform raw sensor topics to standardized structure
- Timestamp Tracking: Add processing timestamps for audit trails
- Topic Namespace Mapping: Route from raw to processed topic hierarchies
DEFINE ACTION MultiLevelSensorRouter
ON TOPIC "factory/+/+/temperature" DO
SET "line_id" WITH TOPIC POSITION 1
SET "machine_id" WITH TOPIC POSITION 2
PUBLISH TOPIC "processed/" + {line_id} + "/" + {machine_id} + "/temperature" WITH PAYLOAD
PUBLISH TOPIC "processed/" + {line_id} + "/" + {machine_id} + "/timestamp" WITH TIMESTAMP "UTC"
Matches topics like: factory/line1/machine5/temperature
DEFINE ACTION TemperatureConverter
ON TOPIC "sensors/+/temperature/celsius" DO
SET "sensor_id" WITH TOPIC POSITION 1
SET "fahrenheit" WITH (PAYLOAD * 9 / 5 + 32)
PUBLISH TOPIC "sensors/" + {sensor_id} + "/temperature/fahrenheit" WITH {fahrenheit}
PUBLISH TOPIC "sensors/" + {sensor_id} + "/temperature/both" WITH "C: " + PAYLOAD + ", F: " + {fahrenheit}
Demonstrates: Unit conversion during routing
- Simple Echo Action - Basic message forwarding
- Alarm Processor - Processing with conditional logic
- Command Processor - Bidirectional communication patterns