Functional Weave
Code in Rust

energy.estimate-read@1.0.0

README.md

1,666 bytes · view raw

# energy.estimate-read

Estimates what a meter will read on a date, the way a supplier estimates a
bill when nobody has read the meter:

```
daily average = units between the earlier and latest reads / days between them
units         = daily average x days from the latest read to the date
read          = latest read + units, wrapped past all nines
```

It works for any cumulative meter: electricity kWh, gas units, water m³.

## Decisions

- **One rounding.** The estimate is `consumption x days ahead / days of
  history`, computed exactly and rounded once to a whole unit by `mode`.
  Rounding the daily average first (as a spreadsheet often does) drifts:
  100 units over 30 days is 3.33 a day, and 30 days of "3" is 90, not 100.
- **Rollover both ways.** The history can span a rollover (99,900 to 00,100
  on a five-digit meter is 200 units, via `energy.meter-advance`), and so can
  the estimate: 99,800 plus 600 units shows as 00,400 with `rolledOver`
  true.
- **The average is flat.** Two reads give one average. Seasonal profiles
  (Elexon profile classes, the gas industry's annual quantity) weigh winter
  days more heavily; to use one, pass reads a year apart covering the same
  season, or compute the units yourself.
- **Dates.** The earlier read must be strictly before the latest; the target
  date may equal the latest read (an estimate of zero units) but not precede
  it. Days come from `dates.days-between`, so leap days count.

## Source

Ofgem describes estimated bills as based on the customer's previous usage;
there is no prescribed formula, and this is the simplest defensible one. The
rollover rule is that of `energy.meter-advance`.