Calculate Delta from Pulse Counter
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.
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:
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
readingsDeltadirectly 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
counterargument (e.g.pulseCount→kwhCounter,gasMeter,unitsProducedLifetime). - To rename the output for a different domain — open the script
and replace
readingsDeltain 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.latestTsunder avaluesenvelope, so each record is stamped at the timestamp of the latestcounterreading. 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 TotalFor per-shift totals (8 / 12 / 6 hour windows, with configurable shift start time):
Manufacturing Plant Output Per 8-Hour ShiftShare 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.