Skip to content
Try for free

ThingsBoard Cloud

Choose your data region

Your data stays in the region you choose, for residency and compliance. No credit card required.

Rather run it yourself? Install on your own servers

Aggregate Telemetry from Related Devices

This guide shows how to use a Related Entities Aggregation calculated field to calculate telemetry from multiple related devices, aggregate the values, and store the result on the parent asset.

A building contains multiple water meters. Each meter sends telemetry: waterConsumption.

Goal: totalWaterConsumption calculated across all meters in the building.

ThingsBoard:

  • finds all devices related to Building A via Contains relations
  • sums up waterConsumption from all meters
  • stores the result as totalWaterConsumption on Building A

Create an asset to represent the building:

  • Name: Building A
  • Asset profile: building

Create two devices with the Water Meter device profile:

  • Name: Water Meter A1
  • Name: Water Meter A2

Link both water meters to the building using outbound Contains relations:

  1. Open Building A details.
  2. Go to the Relations tab.
  3. Add an outbound Contains relation to Water Meter A1.
  4. Add an outbound Contains relation to Water Meter A2.

You can import a ready-made calculated field or build it manually.

Option 1. Import calculated field

  • Download the Building Total Water Consumption calculated field as JSON file and import it into your instance.

Option 2. Create manually

  • Navigate to Calculated fields and click + Add calculated field.
  • Title: Building Total Water Consumption
  • Entity type: Asset
  • Entity: Building A
  • Type: Related entities aggregation
  • Relation direction: Down to child
  • Relation type: Contains

Click Add argument and configure it to read waterConsumption from each related device:

  • Argument type: Latest telemetry
  • Time series key: waterConsumption
  • Argument name: waterConsumption
  • Default value: 0

Click Add metric and configure the aggregation:

  • Metric name: totalWaterConsumption
  • Aggregation: Sum
  • Value source: Key
  • Argument name: waterConsumption
  • Output type: Time series
  • Strategy: Process right away

Click Add to save.

Send waterConsumption telemetry to both meters. Replace $ACCESS_TOKEN with the device access token.

Water Meter A1:

Terminal window
curl -v -X POST -d '{"waterConsumption": 2.75}' https://$THINGSBOARD_HOST/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

Water Meter A2:

Terminal window
curl -v -X POST -d '{"waterConsumption": 6.25}' https://$THINGSBOARD_HOST/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

The calculated field reads the latest value from each related device, sums them, and writes totalWaterConsumption as a time series key on Building A.

Expected result: Open Building A ⇾ Latest telemetry to confirm the aggregated value appears. The totalWaterConsumption value is 9.0 (2.75 + 6.25), updated automatically whenever new data arrives from either meter.