Skip to content
Stand with Ukraine flag

Calculate Hourly Water Consumption Delta

Use a Script calculated field to compute the difference between the last two water meter readings and derive a new value—the water usage delta.

The Water Meter A1 device reports hourly water consumption readings using the waterConsumption key.

ThingsBoard:

  • collects the two most recent waterConsumption readings within a 1-hour rolling window
  • computes the difference between them
  • stores the result as waterConsumptionHourly on the device

Create a device:

  • Name: Water Meter A1
  • Device profile: Water Meter

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

Option 1. Import calculated field

  • Download the Water Consumption Hourly 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: Water Consumption Hourly
  • Entity type: Device profile
  • Device profile: Water Meter
  • Type: Script

Click Add argument and configure the rolling window:

  • Entity type: Current entity
  • Argument type: Time series rolling
  • Time series key: waterConsumption
  • Argument name: waterConsumption
  • Time window: 1 hour
  • Max values: 2

This argument collects the two most recent waterConsumption readings from the last hour.

The script iterates over the two collected readings, computes the delta between the latest and the previous value, clamps negative results to zero (e.g. on meter reset), and returns the result timestamped to the latest reading.

var previousItem = null;
var currentItem = null;
foreach(item: ctx.args.waterConsumption.values) {
previousItem = currentItem;
currentItem = item;
}
if (currentItem == null) {
return {};
}
var waterConsumptionHourly = 0;
if (previousItem != null) {
waterConsumptionHourly = currentItem.value - previousItem.value;
if (waterConsumptionHourly < 0) {
waterConsumptionHourly = 0;
}
}
waterConsumptionHourly = toFixed(waterConsumptionHourly, 2);
return {
ts: currentItem.ts,
values: {
waterConsumptionHourly: waterConsumptionHourly
}
};
  • Output type: Time series
  • Strategy: Process right away

Click Add to save.

Post two waterConsumption readings to Water Meter A1 within the configured time window. Replace $ACCESS_TOKEN with the device access token.

First reading:

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

Second reading:

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

Expected result: Open Water Meter A1Latest telemetry tab. A new waterConsumptionHourly key appears with value 5.0 — the delta between the two readings.

Download the Water Consumption dashboard as a JSON file and import it.

The dashboard includes:

  • a summary widget showing the latest hourly consumption value
  • a time series table displaying waterConsumption and waterConsumptionHourly readings over time