evcc icon indicating copy to clipboard operation
evcc copied to clipboard

Site: one failed battery SoC read halves the site SoC when there are two batteries

Open ghaeprv opened this issue 1 month ago • 2 comments

Describe the bug

With two battery meters, a single failed SoC read on one of them halves the site SoC for that cycle. The failed meter counts as 0 % but still counts in the denominator.

#31211 fixed the case where all battery SoC reads fail and mentions per-meter handling as a possible follow-up. This is that follow-up, with a real occurrence.

Observed on 0.316.0, two batteries of 11 kWh each. Battery 2 SoC read failed once with "connection reset by peer". Values from the evcc InfluxDB table batterySoc (per id, and without id for the site):

Time (UTC) Battery 1 Battery 2 Site
2026-09-27 04:19:28 38.91 40.50 39.71
2026-09-27 04:19:59 38.89 (read failed) 19.45
2026-09-27 04:20:33 38.87 40.47 39.67

19.45 = (38.89 + 0) / 2.

Why, in core/site.go (0.318.0 and master):

  1. Capacity is only read when the SoC read succeeds (around L807-818). A failed SoC read also leaves Capacity nil.
  2. With any capacity missing, the code falls back to sumOfSocs(mm) / len(site.batteryMeters) (L877-880).
  3. sumOfSocs returns 0 for the failed meter (L950-957).

So configuring capacity on both meters does not help: the failed meter's capacity is never read, and the unweighted branch runs.

Impact: for that cycle, prioritySoc, bufferSoc, bufferStartSoc, the battery boost limit and the optimizer see half the real SoC, and the value is published to MQTT and InfluxDB. Any threshold between the real and the halved value can flip for one cycle.

Possible fixes, either works:

  1. Per meter, keep the last known SoC and capacity when a read fails (WARN log). Same idea as #31211, one level down. Site capacity stays stable for the UI and the optimizer.
  2. Leave failed meters out of both sides of the average (numerator and denominator), weighted by the remaining capacities.

Steps to reproduce

  1. Configure two battery meters.
  2. Make the SoC read of one of them fail for one cycle (any transient error, e.g. timeout or connection reset).
  3. Site SoC drops to about half for that cycle (with equal SoCs), then recovers.

Configuration details

Two battery meters, both with capacity 11 kWh. Battery 1: Modbus. Battery 2: custom http meter reading SoC and power from a home automation controller. Battery control not enabled (controllable: false).

Log details

Original log no longer available (container recreated on updates since). The InfluxDB rows above show the effect. The read error on battery 2 at 04:19:59 UTC was "connection reset by peer".

What type of operating system or environment does evcc run on?

Docker container (Raspberry Pi 5, Debian 13)

External automation

  • [ ] I have made sure that no external automation like HomeAssistant or Node-RED is active or accessing any of the mentioned devices when this issue occurs.

Home Assistant reads the same controller (read only). The bug is in the averaging logic and does not depend on why the read failed.

Nightly build

  • [ ] I have verified that the issue is reproducible with the latest nightly build

Not run on nightly. Code path unchanged on master as of 2026-10-25.

Version

0.318.0 (occurrence on 0.316.0)

ghaeprv avatar Oct 25 '26 09:10 ghaeprv

Confirmed against the code in core/site.go (updateBatteryMeters). Capacity is only read inside the successful-SoC branch, so a failed SoC read leaves both Soc and Capacity nil for that meter. The "any capacity missing" branch then runs sumOfSocs(mm) / len(site.batteryMeters), where sumOfSocs counts the failed meter as 0. The existing retain-last-value guard (socFailed) only covers the case where all meters fail. The numbers in the report (19.45 = (38.89 + 0) / 2) match exactly.

This is a genuine defect and looks fixable in a small, local change. The two options differ in behavior (stale value vs. excluding the meter from the average), so a maintainer should pick the approach before a PR.

Related: #31211 (all-meters-failed case, as mentioned in the report).

🤖 Generated with Claude Code

github-actions[bot] avatar Oct 25 '26 09:10 github-actions[bot]

Thanks for the quick turnaround @andig.

One suggestion, now that it is in code: the kept value has no time limit. That is right for a short blip, but if a battery stays unreachable for hours, the site SoC keeps its frozen level and only the ERROR log shows the problem. The all-failed path from #31211 works the same way.

Suggestion: keep the last value for a short window only (a few minutes), then leave that battery out of the average, weighted by the remaining capacities. A long outage then shows the real state of the batteries that still report, instead of a stale number.

ghaeprv avatar Oct 25 '26 12:10 ghaeprv