Skip to content
Stand with Ukraine flag

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.

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.

Review the Rule Engine documentation before proceeding — it explains message routing and processing concepts used in this guide.

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 temperature is 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.
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.
  1. Download the rule chain configuration file: validate_incoming_telemetry_rule_chain.json
  2. Navigate to Rule chains.
  3. Click + Add rule chain ⇾ Import rule chain.
  4. Upload the configuration file and click Import.
  5. Click Apply changes to finalize.

Create a device that publishes temperature telemetry and assign the imported rule chain to its device profile.

  1. Navigate to Entities ⇾ Devices.
  2. Click + Add device ⇾ Add new device, and set:
    • Device name: Thermometer
    • Device profile: thermostat
  3. Click Add.
  4. Open the thermostat device profile settings and set Validate Incoming Telemetry as the default rule chain for this profile.
  5. Save the changes.

From this point onward, all telemetry published by devices using this profile is validated before being stored.

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:

Terminal window
curl -v -X POST \
--header "Content-Type: application/json" \
--data '{"temperature": 25}' \
http://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/telemetry

2. Temperature below the range — should be discarded:

Terminal window
curl -v -X POST \
--header "Content-Type: application/json" \
--data '{"temperature": -50}' \
http://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/telemetry

3. Temperature above the range — should be discarded:

Terminal window
curl -v -X POST \
--header "Content-Type: application/json" \
--data '{"temperature": 100}' \
http://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/telemetry

4. Message without a temperature field — should pass through unchanged:

Terminal window
curl -v -X POST \
--header "Content-Type: application/json" \
--data '{"humidity": 46}' \
http://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/telemetry

Only messages that pass validation are persisted. Invalid temperature values are discarded silently.