Skip to content

feat: work out the distance since the last charge for cars that do not report it - #778

Open
arthur-lzrs wants to merge 4 commits into
joszz:mainfrom
arthur-lzrs:feat/distance-since-charge
Open

arthur-lzrs wants to merge 4 commits into
joszz:mainfrom
arthur-lzrs:feat/distance-since-charge

Conversation

@arthur-lzrs

Copy link
Copy Markdown
Contributor

Summary

Some cars send a distance-since-last-charge counter (mileageSinceLastCharge, which starts again from zero when a charge ends) and some never do. For the second kind the Since Charge card on the dashboard never appears, although the readings GarageStack already stores hold everything needed to count it. (On my MG4 Urban the counter is never sent; the other car on the account sends it.)

For a car whose current state has no mileageSinceLastCharge, the API now works it out: the odometer now, less the odometer where the car's last charge ended. The dashboard shows it with an approximate sign ("≈ 42 km"), and the card's info (i) says how it is worked out. A car that reports the counter itself keeps its own value, untouched.

The end of the last charge is the latest of three signs:

  1. the end time the car reports (charging/lastEnd);
  2. the last reading that showed the car charging (dated by its poll's own lastChargeState stamp when that is older than the row, as the charge history does, so a gateway that starts again does not move it). A charge still going counts as ending now, so it reads 0;
  3. a charge no poll caught, found from the state of charge alone. The gateway reads a parked car rarely (every two minutes for a few minutes after it stops, then once a day), so a charge can start and end between two reads. Readings at one odometer value are a car that stood still, and a climb of 2 points or more above the lowest of that stretch is a charge, at that odometer. One point is enough when the climb ends at a full battery (99.5 % or more). Driving never counts: a new odometer value starts a new stretch.

No charge to count from (a car's first charge since GarageStack began recording has not happened yet) means no figure and the card stays hidden.

How it works

  • DistanceSinceCharge (Core) holds the rules; TelemetryRepository.GetMergedLatestAsync, the one place the dashboard, /status, the widget and the live updates read the current state from, fills the figure in. No migration, no new column.
  • The snapshot gets mileageSinceLastChargeCalculated. It is [NotMapped]: not stored, and kept out of the field merge and the data export. The widget DTO carries it too.
  • What it does not touch. A plain hybrid gets nothing (an engine topping its battery up at a standstill would look like a charge). Where the distance was worked out, what divides by it or sets it against the car's own counters uses it only when the car reported it: the dashboard's efficiency since the last charge, the widget's electricSharePercent and the Statistics page's electric share.
  • Cost. The lookup searches in windows of 7, 30, 120 and 400 days and stops at the first that holds a sign of a charge; the state-of-charge scan reads at most 90 days; an end time only counts when the time itself is inside the window; the odometer found is cached for a minute and "no charge found" for two.
  • The demo's HS plug-in hybrid no longer sends the counter, so the "≈" shows in demo mode.
  • documentation/ARCHITECTURE.md has a section with the rules and why the thresholds are what they are.

Checked against real data

On a car that does report the counter, replaying the odometer-only calculation against the stored readings: 94 of 102 readings came out exactly the same, and the other 8 one kilometre under (the car counts with decimals the odometer does not show). A parked car's state of charge moved by at most 0.3 points in the stored readings of both cars here, which is why 2 points is the threshold.

Type of Change

  • Bug fix
  • New feature
  • Refactor / cleanup
  • Documentation
  • Dependency update
  • Other

Related Issues

None.

How it was tested

  • Unit tests for the rules (DistanceSinceChargeTests): the odometer difference, the end of the last charge from each sign and from the later of them, a time from the future, an epoch, a replayed old charge state, the state-of-charge climb in every shape I could think of (slow charge, a drop then a rise, driving, rows that carry only one of the two values, a climb to full, a car that stays full), plain hybrids, windows, caps and the cache.
  • A test against real PostgreSQL for the queries (TelemetryRepositoryPostgresTests).
  • Frontend: the approximate sign, the card info, the cards that must not use a calculated distance.
  • On a real MG4 Urban: the card shows ≈ 0 km while it charges.
  • Demo mode: the HS plug-in hybrid shows the figure with the sign.

Notes for review

  • Gateway polling. The state-of-charge sign exists because of how rarely a parked car is read; with a shorter inactive refresh in the gateway it would matter less.
  • The 2-point threshold is MinimumSocRisePercent, with the reasoning in its comment and in ARCHITECTURE.md. A battery that corrects itself upward at rest by that much would show as a charge, which makes the distance shorter, and it is shown as approximate either way.
  • Cannot see: a charge with the car driven before the next read.
  • Translations: the only text changed is the card's info, in all five files; the text in nl, es and pt-PT is my own draft, so a native check is welcome.
  • dotnet format: on my machine dotnet format GarageStack.slnx --verify-no-changes reports whitespace errors on a clean main too (115, in files this PR does not touch); compared with main, this branch adds none.
  • A flaky test, not related: one run of the whole backend suite had a single failure I did not identify, and passed on three reruns.

Checklist

  • Tests added or updated
  • Checks pass: pnpm check in frontend/, and dotnet test --project src/GarageStack.Tests (see the note on dotnet format)
  • Commit messages follow Conventional Commits (feat: ..., fix(frontend): ...)
  • If trips changed (ITripRepository): done in the demo too, with a step in TripRepositoryContractTests (not applicable)
  • If the API changed: the rewritten documentation/openapi.json is committed
  • New fields or features have demo data in src/GarageStack.Data/Demo/
  • No secrets or personal data committed, and no real VIN or licence plate in test data
  • Responsive on mobile, down to 320 px wide (no layout change: one character in front of a value)
  • Every new UI text is an i18n key, in every file in frontend/src/locales/ (one existing text changed, in all five)
  • If unraid/garagestack.xml changed: synced to joszz/unraid-community-apps after merge (not applicable, it did not change)

🤖 Generated with Claude Code

arthur-lzrs and others added 4 commits November 27, 2026 15:32
…t report it

Some cars send a distance-since-last-charge counter and some never do, so the Since Charge
card on the dashboard simply never appears for the latter, although the readings GarageStack
has already stored hold everything needed to count it.

For a car whose merged snapshot has no mileageSinceLastCharge, GetMergedLatestAsync now fills
it in: the odometer now, less the odometer where the car's last charge ended. The end of the
last charge is the later of the end time the car reports (charging/lastEnd) and the last
reading that showed it charging; a charge still going counts as ending now, so it reads 0.
A charging reading is dated by its poll's own stamp when that is older than its row, so a
gateway that starts again and sends an old charge state in a new row does not move the end of
the last charge. A time from the future is not believed, an odometer that went backwards gives
no distance, and a car with no charge in its readings (its first charge since GarageStack began
recording has not happened) gets none, so the card stays out of sight until there is one to
count from.

A car that reports the counter keeps its own value untouched. The figure comes through
/status, the widget endpoint and the live updates alike, since they all read the merged
snapshot, so the existing card shows up on its own.

A worked-out figure is not passed off as a measured one: the snapshot carries
mileageSinceLastChargeCalculated (not stored, merged or exported: it is [NotMapped] and kept out
of the field merge and the data export), and the dashboard shows the distance, and the efficiency
since the last charge that rests on it, with an approximate sign in front. The card's info text
says how the figure is worked out, in every locale.

The odometer at the last charge is remembered for a minute (an absent charge too), as it is
found by reading back through the readings, up to 400 days. Checked against a car that does
report the counter: from the odometer alone, 94 of 102 readings came out exactly the same and
the rest one kilometre under, from rounding.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ght it

The gateway reads a parked car rarely (every two minutes for a few minutes after it stops, then
once a day), so a charge can start and end between two reads with no reading saying "charging",
and the distance since the last charge then counted on from an older one.

A stretch of readings at one odometer value is a car that did not move. When its state of charge
climbs by two points or more above its lowest in that stretch, the car was charged, at that
odometer. A new odometer value starts a new stretch, so driving, which regenerates and wobbles the
figure, never counts. Two points is well clear of what a parked car does in the stored readings
(tenths of a point at most, from both cars here) and still finds a top-up worth the name. The
latest of this, a reading that showed the car charging and the end time the car reports is the
last charge. The scan reads only the readings after the last charge otherwise known, or the last
60 days when none is.

Limits: a charge with the car driven before the next read (the odometer changed in between) is
not seen this way, and a battery that corrects itself upward at rest by two points would count
as a charge, which shows as a shorter distance and is shown with the approximate sign anyway.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ument the rules

A parked car read at 98.3 % and then at 100 % was charged, but that is 1.7 points, under the two
the climb must make to be taken for a charge no poll caught. When the climb ends at a full
battery (99.5 % or more) a point is enough, so a top-up to full is not missed by a few tenths. A
car already full that stays full still counts as nothing.

ARCHITECTURE.md now says how the distance since the last charge is worked out for a car that does
not report it: the signs the end of the last charge is taken from, the two points and the one
point to full, why the figure is not stored, and what it cannot see.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…und its lookups

A review of the calculated distance since the last charge found what it should not touch and
what it costs to find.

A plain hybrid never charges from a plug, and an engine topping its battery up at a standstill
would have read as one: the repository now gives it nothing (ChargeSessionHistory.Applies, with
the car's own type setting first, as the charge history does).

Where the distance was worked out, nothing that divides by it or sets it against the car's own
counters uses it: the dashboard's efficiency since the last charge (the car's energy counter over
a distance that starts elsewhere), the widget's electricSharePercent and the Statistics page's
electric share are left to a car that reported the distance itself. The widget carries
mileageSinceLastChargeCalculated, as the snapshot does.

The lookups are bounded. The last charge is searched for in windows of 7, 30, 120 and 400 days and
the search stops at the first that holds one, so a car that charges often reads a week; a reported
end time only counts when the time itself is inside the window, so a zero or an epoch says
nothing (it used to be able to send the state-of-charge scan back to 1970); that scan never reads
further back than 90 days; and "no charge found", the answer that costs most to find, is
remembered for two minutes instead of one.

The demo's plug-in hybrid no longer sends the distance, so its dashboard shows the figure worked
out, with the approximate sign.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant