Skip to content

Cookie preferences

We use cookies for our own analytics, to see how our campaigns perform and, if you allow it, to load content from other services such as the Google site search. We never sell your data. Necessary cookies keep the site working and cannot be switched off. See our Cookie Policy for details and our Privacy Policy for how we handle personal data.

Security, load balancing and remembering your cookie choice.

Show us which pages people read, how they find the site and where they get stuck, so we can improve it (Google Analytics, Microsoft Clarity).

Show us which of our ad campaigns bring visitors to the site and remember the campaign or partner link you arrived from (Google Ads, LinkedIn, Meta, partner program). We do not use these cookies to build advertising profiles.

Load the site search from Google when you use it. Google sets its own cookies and shows ads in the search results.

© 2026 The ThingsBoard Authors
22.5k 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

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_HOST/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

Second reading:

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

Expected result: Open Water Meter A1 ⇾ Latest 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