Skip to content
Stand with Ukraine flag

Calculate Delta from Pulse Counter

  • Calculated Field
  • 283 installs
  • v1.0.0
  • Jun 15, 2026
  • MIT license
  • v4.2+

Compute the per-reading delta from a pulse counter — water meters, gas meters, kWh totals, lifetime production counts, or any counter that only goes up.

Type
  • Script
Category
  • Custom Formula
Use Cases
  • Smart Metering
  • Smart Energy
  • Industrial Automation

Who it’s for

Water utility operators, energy managers, and LoRaWAN deployment teams asking “how much was used since the last reading?”, “how can I show per-reading deltas instead of running totals on the dashboard?” — when your meter publishes only a lifetime counter, and what you need on the dashboard is the difference between two consecutive readings.

What it does

Reads the pulseCount telemetry as a rolling window holding the two most recent readings, and emits the difference between them as readingsDelta — the amount the counter moved since the previous reading:

Reading delta from pulse counter diagram

Counter resets and rollovers (which cause the value to go down) are ignored — the script returns 0 instead of a negative delta. The first evaluation also returns 0, because there is no previous value to compare to yet.

To turn readingsDelta into a consumption view, you have three paths:

  • For totals that other dashboards, alarms, or APIs need — install one of the aggregation calculated fields in See also below. The CF stores each interval’s total as time-series, so it’s available to other dashboards, alarm rules, the REST API, and downstream calculated fields.
  • For a roll-up that lives only on one chart — enable aggregation in the chart widget’s time-window settings. Computed at view time and not stored, so it’s quick to set up but invisible outside that chart.
  • For a per-reading trend — plot readingsDelta directly on a chart.

How to set up

The CF reads pulseCount through a rolling-window argument. Rolling arguments don’t fall back to a default, so the device must already have at least one pulseCount reading in its telemetry history before the CF can initialize. Until then the platform reports Required arguments are missing: counter and the CF stays inactive.

How to customize

  • To read a different meter or counter — change the Time series key on the counter argument (e.g. pulseCountkwhCounter, gasMeter, unitsProducedLifetime).
  • To rename the output for a different domain — open the script and replace readingsDelta in the return object with the key your downstream dashboards and alarms expect (e.g. waterUsed, kwhConsumed, gasConsumed).
  • To tune the window to your reporting frequency — the Time window is how far back the CF looks to find a previous reading. The Limit is fixed at 2, so only the two most recent readings inside the window are ever compared. Defaults to 2.5 days, sized for daily reporters with margin for late uploads. Decrease it for high-frequency reporters, increase it for slower-than-daily ones.
  • To change the output precision — adjust Decimals by default on the output to whatever precision your dashboards and alarms need.
  • To timestamp the result with server time instead of the latest input reading — the script currently wraps its return with ctx.latestTs under a values envelope, so each record is stamped at the timestamp of the latest counter reading. Drop that envelope and return the output key at the top level instead. The record then uses server time at the moment of evaluation.

See also

For per-day, per-week, or per-month totals (calendar-aligned, with configurable interval):

Daily Energy Consumption Total Dmytro Shvaika

For per-shift totals (8 / 12 / 6 hour windows, with configurable shift start time):

Manufacturing Plant Output Per 8-Hour Shift Dmytro Shvaika

Share Your Calculated Field with the Community

Built a useful data transformation? Export it as a JSON from ThingsBoard and publish it to the IoT Hub. Share it with thousands of ThingsBoard developers worldwide.