Repository navigation
[codex] Fix cumulative EVCC energy export - #1
Conversation
fdcb450 to
5c8c173
Compare
|
Thanks — clean fix, the logic is right. Two small things before merging: Unit is kWh, confirmed. Cross-checked on a live Source switch breaks monotonicity. Victron's total_import = float(lp.charge_total_import or 0.0)
if total_import > 0:
candidate = total_import
elif status != STATUS_DISCONNECTED:
candidate = float(lp.charged_energy) / 1000.0
else:
candidate = None
if candidate is not None:
previous = float(s.get("/Ac/Energy/Forward") or 0.0)
s["/Ac/Energy/Forward"] = max(candidate, previous)Plus a test that flips the source mid-run (high → 0 → high, no regression). Side note worth a line in the description: Happy to push a follow-up commit if easier. |
Victron expects /Ac/Energy/Forward to be monotonically increasing. The dynamic source switch (chargeTotalImport -> chargedEnergy/1000) can roll the counter backwards if EVCC briefly returns chargeTotalImport: null/0 — observed on a loadpoint without an attached meter. For heating loadpoints chargedEnergy is structurally 0 (IntegratedDevice chargers have no session semantics), so the fallback would publish 0 to VRM and cause a counter reset. Guard the write with max(candidate, previous_vrm_value); no new state, identical behaviour while EVCC is stable. Also publish the cumulative counter when status == DISCONNECTED but a positive chargeTotalImport is present (lifetime meter semantics). Tests: - monotonic across high -> 0 -> high source switch - DISCONNECTED + positive total_import still publishes /Ac/Energy/Forward - existing per-phase-voltages test now uses per-key getitem mock (previous return_value=5 conflicted with the monotonic guard)
Summary
chargeTotalImportfrom/api/state.chargeTotalImportfor Victron/Ac/Energy/Forwardso continuous/heating devices withchargedEnergy == 0still publish a cumulative kWh counter to VRM.chargedEnergy / 1000as fallback for older/missing EVCC payloads.Root Cause
On a live heat-pump loadpoint, EVCC reports a valid cumulative import meter but keeps the session energy at zero:
The bridge previously exported
/Ac/Energy/ForwardfromchargedEnergy / 1000, so VRM received0 kWheven though the loadpoint had a valid cumulative meter value.The EVCC setup uses a
sgready-relaycharger with a separate Shelly 3EM meter. EVCC logs confirm that loadpoint 3 hascharge total importvalues, so the bridge can forward that cumulative meter directly.Validation
Run with:
nix-shell -p python3 python3Packages.pytest python3Packages.requests python3Packages.requests-mock shellcheck --run 'python3 -m pytest -q'