Skip to content

fix(sonnen): report battery power with the site sign (2.0.4) - #232

Merged
frahlg merged 1 commit into
srcfl:mainfrom
yogiohlsson-ai:fix/sonnen-pac-sign
Nov 28, 2026
Merged

frahlg merged 1 commit into
srcfl:mainfrom
yogiohlsson-ai:fix/sonnen-pac-sign

Conversation

@yogiohlsson-ai

Copy link
Copy Markdown
Contributor

Need

FTW shows a sonnenBatterie charging when it discharges and the reverse.
sonnen's local JSON API v2 reports Pac_total_W positive while
discharging; the driver passed it through unchanged, but the site
convention wants battery charge positive. The driver comment already said the
original sign came from untested community sample code.

Evidence

One unit: sonnenBatterie, software 1.40.5, JSON API v2 on port 80.

  1. Time-of-use charge window, 2026-11-16 02:00–06:00 (EM_OperatingMode
    10), from a 5-minute snapshot log of /api/v2/latestdata, with the mode
    read from /api/v2/configurations. Between 02:05 and 05:55 every sample
    read negative Pac_total_W (45 samples; one sample at 03:50 failed with
    "No route to host"). Most samples read -4.1 to -4.4 kW: 34 of 45, mean
    -4.3 kW. The full range was -3.5 to -5.3 kW. Over the same window USOC
    rose from 17 % to 99 %, and grid import was +4.9 to +7.5 kW.
    At 06:00 Pac_total_W read 5 W.
  2. Self-consumption (EM_OperatingMode 2), same log, 2026-11-14 14:00 to
    2026-11-16 10:55. 291 samples read Pac_total_W above +100 W. They fall
    in hours when consumption exceeded production, for example 2026-11-14 16:05
    with +3458 W, USOC falling 98 → 96 % and grid near 0. 26 samples read
    below -100 W, all with production above consumption, for example
    2026-11-15 08:20 with -1495 W, production 2330 W and consumption 969 W.
  3. API setpoint, 2026-11-13 20:03 CET, run by the PR author's agent with
    the owner's approval and logged in that session. In manual mode (PUT /api/v2/configurations, EM_OperatingMode=1), POST /api/v2/setpoint/charge/500 was followed by six reads at 5 s intervals.
    /api/v2/latestdata showed Pac_total_W -495 to -504 W and SetPoint_W
    -500, and /api/v2/status showed BatteryCharging: true. Afterwards
    setpoint 0 and EM_OperatingMode=2 were restored and read back.

The unit charges with negative Pac_total_W and discharges with positive
Pac_total_W.

A local copy of 2.0.3 has run read-only on that site's FTW since 2026-11-13
with fresh telemetry. It differs from 2.0.3 in two ways: it negates
Pac_total_W, and it treats an {"error": ...} reply, such as
Unauthorized, as a failed read instead of emitting w = 0. This PR carries
only the negation; the error handling is listed under "Not in this PR".

Change

  • drivers/lua/sonnen.lua: battery.w = -Pac_total_W; sign comment corrected.
    battery_pac_w keeps the vendor sign. Still read_only = true; no other
    behaviour changes.
  • Version 2.0.3 → 2.0.4 via tools/bump_driver.py, manifests/index/devices/
    support status regenerated, CHANGELOG entry.
  • New drivers/tests/test_sonnen_sign.py + harness
    lua_harness/test_sonnen_sign.lua: charging body (-500) must emit +500,
    discharging body (+1200) must emit -1200, SoC/capacity unchanged, driver
    still read-only. The test fails on 2.0.3 (charging read as -500).

Checks

Every step of make check, run with the same tools through a local venv
(Python 3.13, pytest 9.2.0, cryptography 50.0.2) because uv is not
installed on the test host: boundary, manifest sync/validate, index, devices,
support status, history, FTW baseline, host API, sandbox, and
pytest drivers/tests tests — 5091 passed, 951 skipped.

Not in this PR

  • The driver still emits w = 0 when the unit answers
    {"error":"Unauthorized"}; that should become a failed read. Separate fix.
  • Control (manual setpoints, safe default mode) needs host.http_put in FTW
    Core (sonnen answers PATCH/POST on /api/v2/configurations with 404) and
    will be a separate PR.
  • FTW follow-up: move drivers/BUNDLED_SOURCE.json to this commit once merged.

🤖 Generated with Claude Code

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Flips sonnen battery.w to the site sign (+ = charging) by negating Pac_total_W, with a harness test for charge and discharge and a bump to 2.0.4.

  • Checked: sha256 and size_bytes in index.yaml and manifests/sonnen.yaml match the head drivers/lua/sonnen.lua (aee7b5ec…, 6734 bytes). battery_pac_w keeps the vendor sign, driver stays read-only.
  • Note: the old comment said the sample code was unverified, and the new one cites a live reading (1.40.5, -500 W setpoint). The fix reads right to me.
  • Note: no CI reported on this branch, so the new test hasn't run anywhere I can see. Worth a make check before merge.

Safe to merge from my read, once the test has run.

@yogiohlsson-ai
yogiohlsson-ai force-pushed the fix/sonnen-pac-sign branch 2 times, most recently from ad91026 to b6f0dd9 Compare November 19, 2026 19:42
@yogiohlsson-ai

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (one CHANGELOG conflict, kept both entries).
make check: 5092 passed, 951 skipped, 0 failed.
The workflows are waiting for a maintainer's approval to run.

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Flips the sonnen battery sign (negates Pac_total_W so charging is positive) and bumps the driver to 2.0.4, with the matching index/manifest/support-status updates and a Lua harness test.

  • Logic is a one-line negation, and the raw vendor value stays in battery_pac_w, so nothing downstream loses information. Tests cover charge (-500 -> +500), discharge (1200 -> -1200), SOC/capacity passthrough and read-only.
  • Evidence is one unit (software 1.40.5, six reads of a 500 W charge setpoint), so it's a single-device measurement. Worth a note for installs on other firmware.
  • Security screen: no secrets, no new hosts or dependencies. The sha256/size_bytes changes follow the lua edit; I didn't recompute them.

Safe to merge from my read, once the checks on the hashes are green.

@HuggeK

HuggeK commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Are this backed by some documentation or could it be that your installer installed it backwards?

@frahlg

frahlg commented Nov 21, 2026

Copy link
Copy Markdown
Member

Not from documentation. The PR measures it on one unit (software 1.40.5). On 16 Nov, in a 02:00–06:00 charge window, Pac_total_W was negative in all 45 samples, mostly −4.1 to −4.4 kW, while USOC rose from 17 % to 99 % and the grid imported 4.9 to 7.5 kW. On 13 Nov a 500 W charge setpoint read −495 to −504 W with BatteryCharging: true. So negative means charging on this unit. A reading from another unit would add evidence.

@yogiohlsson-ai

Copy link
Copy Markdown
Contributor Author

Adding one independent data point from the same unit's owner: the ioBroker.sonnen adapter
documents its status.pacTotal as "greater than 0 the battery is discharging, less than zero
it is charging" (https://github.com/foxriver76/ioBroker.sonnen). That is likely the same
value as Pac_total_W in the status API, and it matches the sign we measured here. This
also answers the installer question: Pac_total_W is the inverter AC power reported by the
unit itself, so it does not depend on how the installation is wired.

@yogiohlsson-ai
yogiohlsson-ai force-pushed the fix/sonnen-pac-sign branch 2 times, most recently from a53e616 to 8869bdc Compare November 25, 2026 11:28

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Flips the sign of battery.w in the sonnen driver (2.0.3 → 2.0.4) so charge is positive, matching the site convention. battery_pac_w keeps the vendor sign. Version bumps and index/manifest hashes are updated, and there's a harness test for both directions.

  • Evidence is solid: a 500 W charge setpoint read back as Pac_total_W −495…−504 W, and the old comment admitted the sign was from untested community code. One unit, one firmware (1.40.5), so worth noting in the changelog that other firmware is unverified. Not a blocker.
  • Security: nothing in the diff. No new deps or hosts, driver stays read-only.
  • Needs a rebase: the PR is CONFLICTING. index.yaml, manifests/sonnen.yaml, SUPPORT_STATUS.md and support-status.json are generated, so the sha256/size_bytes values will need regenerating after the rebase, not hand-merging.
  • No CI checks reported, so I couldn't confirm the tests pass.

Content looks right to me. Needs a human to rebase and let CI run before merge.

sonnen's JSON API v2 reports Pac_total_W positive while discharging. The
driver passed it through, so FTW showed charge as discharge. Negate it so
battery.w is positive while charging. Measured on a sonnenBatterie with
software 1.40.5 on 2026-11-13: in manual mode, a 500 W charge setpoint
read back over six reads as Pac_total_W -495 to -504 W, SetPoint_W -500
and BatteryCharging true.

The driver stays read-only. battery_pac_w keeps the vendor sign.

Signed-off-by: Jörgen Ohlsson <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@yogiohlsson-ai

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (3 new commits; only CHANGELOG conflicted, and the generated
files were regenerated with the repo's generators). Added a sentence to the 2.0.4 changelog
entry that this is verified on one unit and one firmware (1.40.5) and other firmware versions
are unverified. make check passes: 5453 passed, 927 skipped. The workflows are waiting for a
maintainer's approval to run.

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Flips the sonnen battery.w sign (battery.w = -Pac_total_W), bumps the driver to 2.0.4 and adds a Lua harness test plus pytest wrapper.

  • Logic looks right and the evidence is solid for one unit: three independent observations (TOU charge window, self-consumption, a 500 W setpoint readback) all agree on negative = charging.
  • Note: the PR body says a local 2.0.3 copy already runs on that site with the same negation, so this is verified on 1 unit / firmware 1.40.5 only. The CHANGELOG already says so, good. Anyone on other firmware will see the sign flip with no way to tell which convention they have. Worth a line in release notes.
  • Security screen: no secrets (test uses 192.0.2.10 and a dummy token), no new deps, no new network hosts, driver stays read_only.
  • Minor: index.yaml / manifest sha256 and size_bytes change; I did not recompute them, assume the repo's make target generated them. CI shows no check runs on this PR, so nothing has run the new test yet.

Safe to merge from my read once CI has actually run the harness test.

@frahlg
frahlg merged commit 20f9e8e into srcfl:main Nov 28, 2026
5 checks passed
frahlg added a commit to srcfl/ftw that referenced this pull request Nov 28, 2026
…2093)

Moves the driver pin from srcfl/device-drivers 8aeecee3 to 20f9e8e5.
Only sonnen.lua changes (srcfl/device-drivers#232).

Signed-off-by: Fredrik Ahlgren <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

4 participants