Validate Incoming Telemetry
Validate incoming telemetry data in ThingsBoard and discard invalid values before they are stored. This guide uses an importable rule chain configuration so you can examine the logic and adapt it to your own scenario.
Use case
Section titled “Use case”Assume a device with a temperature sensor periodically sends telemetry to ThingsBoard. The sensor supports measurements in the range -40 °C to +80 °C.
Your objective is to:
- Accept and store temperature readings only if they fall within the valid range.
- Discard telemetry values outside this range.
- Allow all other telemetry data (that does not contain temperature) to pass through unchanged.
Prerequisites
Section titled “Prerequisites”Review the Rule Engine documentation before proceeding — it explains message routing and processing concepts used in this guide.
Validation logic
Section titled “Validation logic”Incoming messages may or may not contain the temperature field. Validation follows these rules:
- If the message does not contain
temperature, it is considered valid. - If
temperatureis present, its value must be within the range -40 to 80.
Only messages that meet these conditions are stored in the database.
The validation is implemented using a rule chain that filters incoming messages before they are saved. The rule chain:
- Routes all messages from devices using this rule chain to a filter node with the temperature validation script.
- Forwards valid telemetry to the save timeseries node, which stores data in the database.
Validation script
Section titled “Validation script”return typeof msg.temperature === 'undefined' || (msg.temperature >= -40 && msg.temperature <= 80);How it works:
typeof msg.temperature === 'undefined'— allows messages that do not include temperature data to pass through unchanged.msg.temperature >= -40 && msg.temperature <= 80— validates that the temperature value is within the supported range.
Step 1. Import the validation rule chain
Section titled “Step 1. Import the validation rule chain”- Download the rule chain configuration file: validate_incoming_telemetry_rule_chain.json
- Navigate to Rule chains.
- Click + Add rule chain ⇾ Import rule chain.
- Upload the configuration file and click Import.
- Click Apply changes to finalize.
Step 2. Add a demo device
Section titled “Step 2. Add a demo device”Create a device that publishes temperature telemetry and assign the imported rule chain to its device profile.
- Navigate to Entities ⇾ Devices.
- Click + Add device ⇾ Add new device, and set:
- Device name:
Thermometer - Device profile:
thermostat
- Device name:
- Click Add.
- Open the
thermostatdevice profile settings and set Validate Incoming Telemetry as the default rule chain for this profile. - Save the changes.
From this point onward, all telemetry published by devices using this profile is validated before being stored.
Test the configuration
Section titled “Test the configuration”Open the device’s Latest telemetry tab to monitor incoming data in real time, then publish the test messages below.
1. Temperature within the valid range — should be stored:
curl -v -X POST \--header "Content-Type: application/json" \--data '{"temperature": 25}' \http://eu.thingsboard.cloud/api/v1/$ACCESS_TOKEN/telemetry2. Temperature below the range — should be discarded:
curl -v -X POST \--header "Content-Type: application/json" \--data '{"temperature": -50}' \http://eu.thingsboard.cloud/api/v1/$ACCESS_TOKEN/telemetry3. Temperature above the range — should be discarded:
curl -v -X POST \--header "Content-Type: application/json" \--data '{"temperature": 100}' \http://eu.thingsboard.cloud/api/v1/$ACCESS_TOKEN/telemetry4. Message without a temperature field — should pass through unchanged:
curl -v -X POST \--header "Content-Type: application/json" \--data '{"humidity": 46}' \http://eu.thingsboard.cloud/api/v1/$ACCESS_TOKEN/telemetryOnly messages that pass validation are persisted. Invalid temperature values are discarded silently.
See also
Section titled “See also”- Rule Engine overview — full reference for message routing and processing
- Script filter node — write custom JavaScript conditions for message filtering