Skip to content

Soc: extrapolate from charged energy when vehicle api is unavailable - #33122

Merged
andig merged 3 commits into
masterfrom
soc-extrapolation
Aug 24, 2026
Merged

Soc: extrapolate from charged energy when vehicle api is unavailable#33122
andig merged 3 commits into
masterfrom
soc-extrapolation

Conversation

@andig

@andig andig commented Aug 23, 2026

Copy link
Copy Markdown
Member

fixes #33116

When the vehicle backend becomes unavailable during charging, the estimator so far froze the last known soc, so a configured soc limit could be exceeded until the backend returned.

The estimator's fetchedSoc == nil branch now extrapolates the soc from charged energy using the already sampled state (prevSoc + energyDelta/energyPerSocStep) — the same formula as the existing interpolation branch, so the estimate stays continuous once the backend returns. The sampled state itself remains untouched and the next fetched soc always takes precedence. The estimate is clamped to 100% and kept monotonic so a mid-outage session energy reset cannot move it backwards. This implements the nil | value | prevsoc + delta case documented in core/soc/README.md.

publishSocAndRange now passes a missing soc through to the estimator instead of skipping it. A freshly created estimator without any prior sample returns 0 and does not overwrite a known soc (same-vehicle reconnect must keep the published value). As a side effect, the published soc now also advances with charged energy between regular soc polls.

🤖 Generated with Claude Code

@andig andig added the enhancement New feature or request label Aug 23, 2026
@github-actions github-actions Bot added the bug Something isn't working label Aug 23, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="core/soc/estimator.go" line_range="97-98" />
<code_context>
-		s.log.WARN.Println("missing vehicle soc- ignored by estimator")
+		// extrapolate soc from charged energy while no vehicle soc is available,
+		// never below the current estimate to stay monotonic across energy resets
+		if energyDelta := max(chargedEnergy, 0) - s.prevChargedEnergy; s.initialSoc > 0 && energyDelta >= 0 {
+			s.vehicleSoc = min(max(s.vehicleSoc, s.prevSoc+energyDelta/s.energyPerSocStep), 100)
+			s.log.DEBUG.Printf("soc extrapolated: %.2f%%", s.vehicleSoc)
+		}
</code_context>
<issue_to_address>
**issue (bug_risk):** A vehicle whose first valid fetched SOC is exactly 0% never enters the nil-SOC extrapolation branch because `initialSoc > 0` is false, so the estimator freezes at 0% throughout a backend outage despite charged energy increasing.

**Triggers:** When charging starts at 0% SOC and the vehicle API becomes unavailable before the next SOC sample.

**Suggested fix:** Track whether a valid SOC sample exists separately from the SOC value, instead of using `initialSoc > 0` as the initialization test.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: core/soc/estimator.go:98


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread core/soc/estimator.go Outdated
@andig
andig requested a review from premultiply August 23, 2026 15:41
@andig andig removed the bug Something isn't working label Aug 23, 2026
@andig andig changed the title Soc: extrapolate soc from charged energy when vehicle api is unavailable Soc: extrapolate from charged energy when vehicle api is unavailable Aug 23, 2026
@andig
andig merged commit b15f81a into master Aug 24, 2026
11 checks passed
@andig
andig deleted the soc-extrapolation branch August 24, 2026 08:45
@andig andig added the bug Something isn't working label Aug 24, 2026
@andig

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Opened backport pull request on release/0.314.4: #33149

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

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extrapolate current SoC from last known SoC and battery capacity if backend is unavailable

1 participant