Skip to content

Estimate a Tesla's battery capacity from its rated range - #172

Merged
vcanuel merged 2 commits into
mainfrom
feat/tesla-estimated-capacity
Oct 29, 2026
Merged

vcanuel merged 2 commits into
mainfrom
feat/tesla-estimated-capacity

Conversation

@vcanuel

@vcanuel vcanuel commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Closes #167

A Tesla's battery capacity, estimated the way TeslaMate's Battery Health reads it, from the rated range and the energy the car measures during a charge.

What changes

  • Readings: usable_battery_level is read (core.Snapshot.UsableSoC, covered by FieldSoC). A charge keeps the rated range read with its start and end SoC, and the usable level read with its end (migration 0027).
  • Rated efficiency (kWh per km of rated range): energy_measured_kwh / (end_range − start_range) on observed charges of 10 min or more ending at 95 % at most; the median over the whole history.
  • rated estimate, one per charge: end_range / end_usable × 100 × efficiency, when the usable level ends at 50 % or more. Same plausibility bounds as the other sources; two new reasons a charge gives none (no_efficiency, low_soc).
  • Basis: once there are 5 rated estimates, the current capacity, its change and the monthly medians rest on them alone (basis: "rated"); the power and billed estimates stay in the series.
  • API GET /vehicles/{vehicle}/battery: basis, rated_efficiency, source rated, excluded.no_efficiency and excluded.low_soc; spec and golden files regenerated (battery-rated.json).
  • Battery page: the rated points (in the median's color when they are its basis; a fifth chart color, chart-5, for the points it does not rest on), the rated efficiency in kWh/100 km under the capacity, the new reasons for a Tesla only, and the range at 100 % said to follow the battery for a Tesla.
  • Simulator: the rated range follows the usable level; an end-to-end test finds the model3 scenario's 75 kWh.
  • Docs: the battery guide (a Tesla), the Tesla guide, the update notes (rebuild), AGENTS.md.

Update

Migration 0027; the past charges give a rated estimate only after a rebuild (update notes).

Tests

task unit (Go lint, tests, govulncheck, build; front lint, Vitest, audit, build) and the store tests against PostgreSQL. Checked on the simulator only: no real charge recorded yet.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Battery capacity estimates now incorporate Tesla rated-range data, using rated estimates alone when at least five qualify.
    • Battery charts distinguish rated estimates from other estimate types, with updated labels, colors, and explanations.
    • Battery details show rated efficiency and explain when estimates are excluded.
  • Documentation

    • Updated battery and Tesla guides with estimation details and eligibility criteria.
    • Documented that rebuilding past charge data is required for historical rated estimates.

vcanuel and others added 2 commits October 29, 2026 15:12
A Tesla tells its usable level (usable_battery_level) beside its SoC, and
its range (battery_range) is the rated one, a fixed conversion of that
level. A charge now keeps the range read with its start and end SoC, and
the usable level read with its end (migration 0027), which a capacity
estimate from the rated range rests on. FieldSoC covers the usable level,
told in the same block: Field stays on 16 bits.

The simulator's rated range follows its usable level, a point below the
SoC, as the real car's does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
As TeslaMate reads it: the rated efficiency is the median, over the
observed charges of 10 minutes or more ending at 95 % at most, of the
energy the car measured over the rated range it added; each charge ending
with 50 % usable or more gives a rated estimate, its rated range brought
back to 100 % of the usable level, times that efficiency.

Once there are 5 rated estimates, the current capacity, its change and the
monthly medians rest on them alone (the API's basis): the car's own count
is far more precise than a SoC change over whole kW. The power and billed
estimates stay in the series. The battery page draws the rated points,
gives the rated efficiency, the two new reasons a charge gives none, and
says a Tesla's range at 100 % follows the battery, not the season. A fifth
chart color tells the points the median does not rest on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 7562cfc and 75c6a60.

Files ignored due to path filters: 5
  • api/openapi.yaml is excluded by !api/openapi.yaml
  • internal/api/testdata/battery-api-capacity.json is excluded by !**/testdata/**
  • internal/api/testdata/battery-empty.json is excluded by !**/testdata/**
  • internal/api/testdata/battery-rated.json is excluded by !**/testdata/**
  • internal/api/testdata/battery.json is excluded by !**/testdata/**
Files selected for processing: 32
  • AGENTS.md
  • documentation/guide/battery.md
  • documentation/guide/tesla.md
  • documentation/self-hosting/updating.md
  • internal/api/battery.go
  • internal/api/battery_test.go
  • internal/api/openapi_test.go
  • internal/core/capacity.go
  • internal/core/capacity_test.go
  • internal/core/charge.go
  • internal/core/derive_test.go
  • internal/core/event.go
  • internal/core/snapshot.go
  • internal/simulator/teslaapi/handler_test.go
  • internal/simulator/teslaapi/render.go
  • internal/store/events.go
  • internal/store/events_test.go
  • internal/store/migrations/0027_charge_rated_range.sql
  • internal/tesla/normalize.go
  • internal/tesla/normalize_test.go
  • internal/tesla/simulator_test.go
  • web/e2e/battery.spec.ts
  • web/src/shared/i18n/locales/en.json
  • web/src/shared/i18n/locales/fr.json
  • web/src/shared/i18n/locales/sv.json
  • web/src/shared/theme/themes/tiles.ts
  • web/src/shared/theme/types.ts
  • web/src/widgets/battery-charts/ui/BatteryCharts.vue
  • web/src/widgets/battery-tiles/ui/BatteryTiles.vue
  • web/test/app/theme.test.ts
  • web/test/widgets/battery-charts/BatteryCharts.test.ts
  • web/test/widgets/battery-tiles/BatteryTiles.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

The change adds Tesla rated-range capacity estimates. It captures and stores rated range and usable-charge readings, calculates rated efficiency and per-charge estimates, and exposes the results in the battery API, charts, and capacity tile.

Changes: Tesla rated-capacity flow

Layer / File(s) Summary
Capture and persist rated-range data
internal/tesla/*, internal/core/charge.go, internal/core/snapshot.go, internal/core/event.go, internal/store/*, internal/simulator/teslaapi/*, internal/tesla/simulator_test.go, documentation/self-hosting/updating.md
Tesla usable-charge readings and charge range values flow through snapshots and derived charges into new nullable database columns. Simulator data and tests cover the updated readings. The migration note states that rebuilding is required for past charges to receive these values.
Calculate rated estimates and select the trend basis
internal/core/capacity.go, internal/core/capacity_test.go, AGENTS.md
Rated efficiency uses qualifying measured-energy and range data. Eligible charges produce rated estimates. When the rated estimate count meets the configured threshold, rated estimates alone determine current capacity, change, and monthly capacity; all estimates remain available.
Expose and display rated estimates
internal/api/battery.go, internal/api/battery_test.go, internal/api/openapi_test.go, web/src/widgets/battery-charts/ui/BatteryCharts.vue, web/src/widgets/battery-tiles/ui/BatteryTiles.vue, web/src/shared/i18n/locales/*, web/src/shared/theme/*, web/test/*, web/e2e/battery.spec.ts, documentation/guide/*, AGENTS.md
The battery response adds the estimate basis, rated efficiency, and rated-exclusion counts. The interface plots rated estimates, distinguishes their chart color and basis, and displays rated-related notes and exclusion reasons. Guides and translations describe the calculations and chart distinctions.

Possibly related issues

  • runsten-app/runsten#134 — The rated-efficiency and capacity calculations use the measured Tesla charge energy that this issue adds to the data flow.

Possibly related PRs

  • runsten-app/runsten#170 — Stores Tesla-reported charge energy, which this change uses to calculate rated efficiency.
  • runsten-app/runsten#148 — Adds the Tesla simulator data path that this change extends and tests for rated capacity.
  • runsten-app/runsten#137 — Adds Tesla normalization that this change extends to retain usable battery level.

Priority: Normal

Merge Risk: Minimal · up to 75c6a

No confirmed issue prevents merging after normal checks.

Security architecture risk: Low · up to 75c6a

The new estimates retain existing account and vehicle access checks, and the added storage fields support missing historical readings. No introduced security-boundary failure was established. Deployment, downgrade, and concurrent recovery behavior were not fully assessed.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The inspected new response data remains scoped to the resolved vehicle within the authenticated account. No broader data-read authority was established through the new fields. This does not establish a service-wide resource-exhaustion bound.

Trust Boundaries and Controls

  • observed — The battery route uses the existing authenticated operation wrapper. Before reading history, accountVehicle validates the vehicle identifier and resolves it using the session account; PeriodEvents receives that same account and resolved vehicle. Another account's vehicle is treated as not found.

Resilience and Maintainability Implications

  • observed — The new persisted readings use the existing account-scoped transaction mechanism, which sets the account context before executing writes. The PR's added insert parameters do not replace that containment mechanism or separate charge updates from cursor recovery state.
Pre-merge checks: Passed 3 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Linked Issues check Warning Issue #167 is directly linked and open. The implementation covers the Tesla readings, rated-efficiency calculation, per-charge estimate, rated basis, API, Battery page, simulator test, and guides. The… Update ADR 0023 or add a new ADR. Document the Tesla rated-range method and how rated estimates combine with the existing estimates.
Passed Passed checks: 3 passed
Check name Status Explanation
Title check Passed The title is concise, imperative, in English, and accurately describes the Tesla rated-range battery-capacity estimate.
Description check Passed The description explains the purpose, implementation, API and UI changes, migration impact, tests, and issue reference. It does not reproduce every template checkbox or use the exact "How it was teste…
Out of Scope Changes check Passed The reviewed changes stay within issue #167. Data-model, persistence, migration, normalization, simulator, API, UI, localization, tests, guides, and update notes support the Tesla capacity estimate or…
Full details: Description check

Explanation

The description explains the purpose, implementation, API and UI changes, migration impact, tests, and issue reference. It does not reproduce every template checkbox or use the exact "How it was tested" heading, but it provides the required information in substance.

Full details: Out of Scope Changes check

Explanation

The reviewed changes stay within issue #167. Data-model, persistence, migration, normalization, simulator, API, UI, localization, tests, guides, and update notes support the Tesla capacity estimate or its required presentation and rollout. No unrelated change is demonstrated.

Full details: Linked Issues check

Explanation

Issue #167 is directly linked and open. The implementation covers the Tesla readings, rated-efficiency calculation, per-charge estimate, rated basis, API, Battery page, simulator test, and guides. The changes also include automated tests for the core, API, chart, tile, and simulator behavior. The reviewed changes do not update ADR 0023 or add a new ADR that documents the Tesla method and its combination with existing estimates. The PR objective text also identifies this missing update.

Resolution

Update ADR 0023 or add a new ADR. Document the Tesla rated-range method and how rated estimates combine with the existing estimates.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@vcanuel

vcanuel commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

On the linked-issue warning: the ADRs are kept outside this repository; ADR 0023 has been updated there with the Tesla rated-range method and how its estimates combine with the others. The user-facing method is documented in documentation/guide/battery.md ("A Tesla").

@vcanuel
vcanuel merged commit 0f5de4b into main Oct 29, 2026
8 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Nov 3, 2026
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tesla: estimate the battery capacity from the rated range and the measured energy

1 participant