From 1b88a04b199d8c5d810e17eacb0a4a57b0235f77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rgen=20Ohlsson?= Date: Mon, 16 Nov 2026 13:19:45 +0100 Subject: [PATCH] fix(sonnen): report battery power with the site sign (2.0.4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Jörgen Ohlsson --- CHANGELOG.md | 11 ++++ SUPPORT_STATUS.md | 4 +- devices.yaml | 2 +- drivers/lua/sonnen.lua | 13 +++-- .../tests/lua_harness/test_sonnen_sign.lua | 58 +++++++++++++++++++ drivers/tests/test_sonnen_sign.py | 27 +++++++++ index.yaml | 6 +- manifests/sonnen.yaml | 6 +- support-status.json | 2 +- 9 files changed, 113 insertions(+), 16 deletions(-) create mode 100644 drivers/tests/lua_harness/test_sonnen_sign.lua create mode 100644 drivers/tests/test_sonnen_sign.py diff --git a/CHANGELOG.md b/CHANGELOG.md index aaf58aa..b1d4197 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ ## [Unreleased] +### sonnen 2.0.4 + +Battery power has the site sign. sonnen reports `Pac_total_W` positive while +discharging and negative while charging; the driver passed it through, so +FTW showed every charge as a discharge and the reverse. It now negates the +value: charging is positive. 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. `battery_pac_w` keeps the vendor's own sign. The driver stays +read-only. + ### easee_cloud 1.4.1 The driver says its session energy is coarse, with `session_wh_coarse`. Easee diff --git a/SUPPORT_STATUS.md b/SUPPORT_STATUS.md index 1574553..b78ddf9 100644 --- a/SUPPORT_STATUS.md +++ b/SUPPORT_STATUS.md @@ -158,8 +158,8 @@ Catalog source is not proof that a target can install or run a driver. | solis | 2.1.1 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | solis_string | 1.1.3 | ftw-core | not_assessed | — | not_recorded | not_assessed | | solis_string | 1.1.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | -| sonnen | 2.0.3 | ftw-core | not_assessed | — | not_recorded | not_assessed | -| sonnen | 2.0.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | +| sonnen | 2.0.4 | ftw-core | not_assessed | — | not_recorded | not_assessed | +| sonnen | 2.0.4 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | sungrow | 1.6.1 | ftw-core | not_assessed | — | not_recorded | not_assessed | | sungrow | 1.6.1 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | tesla_cloud | 0.2.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | diff --git a/devices.yaml b/devices.yaml index 317220b..10da6e4 100644 --- a/devices.yaml +++ b/devices.yaml @@ -2175,7 +2175,7 @@ manufacturers: protocols: - protocol: http driver: "sonnen" - version: "2.0.3" + version: "2.0.4" ders: [battery, meter] control: false firmware_versions: "" diff --git a/drivers/lua/sonnen.lua b/drivers/lua/sonnen.lua index 003cec3..4a3f593 100644 --- a/drivers/lua/sonnen.lua +++ b/drivers/lua/sonnen.lua @@ -19,10 +19,11 @@ -- -- Sign convention (site: positive W flows INTO the site): -- battery.w: positive = charging (sink), negative = discharging (source). --- Sonnen Pac_total_W is reported positive=charging, so it passes through --- unchanged. This is from community-contributed sample code; verify --- on first install by checking that battery.w is positive while the --- site is exporting and the Sonnen is taking the surplus. +-- sonnen reports Pac_total_W positive while DISCHARGING and negative while +-- charging, so the driver negates it. 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. DRIVER = { host_api_min = 1, @@ -30,7 +31,7 @@ DRIVER = { id = "sonnen", name = "sonnenBatterie (local API)", manufacturer = "sonnen", - version = "2.0.3", + version = "2.0.4", protocols = { "http" }, capabilities = { "battery" }, read_only = true, @@ -182,7 +183,7 @@ function driver_poll() local soc_pct = usoc or rsoc or 0 local battery = { - w = pac_w, + w = -pac_w, -- sonnen: + = discharge; site: + = charge soc = soc_pct / 100.0, } if cap_wh then diff --git a/drivers/tests/lua_harness/test_sonnen_sign.lua b/drivers/tests/lua_harness/test_sonnen_sign.lua new file mode 100644 index 0000000..2091ba8 --- /dev/null +++ b/drivers/tests/lua_harness/test_sonnen_sign.lua @@ -0,0 +1,58 @@ +-- sonnen: battery.w sign. sonnen reports Pac_total_W positive while +-- discharging and negative while charging; FTW's battery.w is positive while +-- charging. Bodies follow /api/v2/latestdata from a sonnenBatterie running +-- software 1.40.5. + +dofile("drivers/tests/lua_harness/host_mock.lua") + +local URL = "http://192.0.2.10:80/api/v2/latestdata" + +local function latest(pac_w, setpoint_w) + return '{"Consumption_W":2259,"FullChargeCapacity":18700,' .. + '"GridFeedIn_W":-2240,"Pac_total_W":' .. tostring(pac_w) .. + ',"Production_W":16,"RSOC":45,"SetPoint_W":' .. tostring(setpoint_w) .. + ',"Timestamp":"2026-11-13 20:03:26","USOC":40,"UTC_Offset":1}' +end + +local function poll_battery_w(body) + host.reset() + dofile("drivers/lua/sonnen.lua") + driver_init({ host = "192.0.2.10", port = 80, api_token = "token" }) + host._http_responses[URL] = body + local ok, err = pcall(driver_poll) + if not ok then error("driver_poll failed: " .. tostring(err)) end + local list = host._emitted.battery + if not list or #list == 0 then error("no battery emit") end + return list[#list] +end + +-- Charging: shaped like the readback of a 500 W charge setpoint +-- (measured -495 to -504 W; -500 here). +local charging = poll_battery_w(latest(-500, -500)) +if charging.w ~= 500 then + error("charging must be +500 W in site convention, got " .. tostring(charging.w)) +end +if math.abs(charging.soc - 0.40) > 1e-9 then + error("soc must come from USOC as a fraction, got " .. tostring(charging.soc)) +end +if charging.capacity_wh ~= 18700 then + error("capacity_wh lost: " .. tostring(charging.capacity_wh)) +end + +-- Discharging. +local discharging = poll_battery_w(latest(1200, 0)) +if discharging.w ~= -1200 then + error("discharging must be -1200 W in site convention, got " .. tostring(discharging.w)) +end + +-- The raw vendor value stays available, unconverted, as a metric. +if not host._metrics.battery_pac_w or host._metrics.battery_pac_w.value ~= 1200 then + error("battery_pac_w must keep the vendor sign") +end + +-- Still read-only. +if DRIVER.read_only ~= true or driver_command("battery", 1000, {}) ~= false then + error("sonnen must stay read-only") +end + +print("OK") diff --git a/drivers/tests/test_sonnen_sign.py b/drivers/tests/test_sonnen_sign.py new file mode 100644 index 0000000..c8e4b40 --- /dev/null +++ b/drivers/tests/test_sonnen_sign.py @@ -0,0 +1,27 @@ +"""sonnen: battery.w follows the site convention. + +sonnen reports Pac_total_W positive while discharging; FTW's battery.w is +positive while charging. The harness script polls the driver against +/api/v2/latestdata bodies in the shape a live unit returns. +""" + +from __future__ import annotations + +import subprocess +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[2] +LUA = ROOT / "lua55" + +pytestmark = pytest.mark.skipif( + not LUA.exists(), reason="run make check to build ./lua55") + + +def test_sonnen_battery_sign_follows_site_convention(): + result = subprocess.run( + [str(LUA), "drivers/tests/lua_harness/test_sonnen_sign.lua"], + capture_output=True, text=True, cwd=ROOT) + assert result.returncode == 0, result.stdout + result.stderr + assert "OK" in result.stdout diff --git a/index.yaml b/index.yaml index 0f09d7f..383ac07 100644 --- a/index.yaml +++ b/index.yaml @@ -709,15 +709,15 @@ drivers: size_bytes: 5807 sha256: "5669996a562d10fc94ed89748664542ce14e5d965ff0df85d1e7610ff4dda568" - name: "sonnen" - version: "2.0.3" + version: "2.0.4" tier: community protocol: http connectivity: local setup: [device_ui] ders: [battery, meter] control: false - size_bytes: 6631 - sha256: "94ae5e0065f5793051acb89b7ef04a679b2898908dfac6f4ffd6e8deb77fa2c0" + size_bytes: 6734 + sha256: "aee7b5ecf3b7666561084d434664f2df1019425f085ce36c4c4488baebfd1f76" - name: "sungrow" version: "1.6.1" tier: core diff --git a/manifests/sonnen.yaml b/manifests/sonnen.yaml index d4757fe..7d586f8 100644 --- a/manifests/sonnen.yaml +++ b/manifests/sonnen.yaml @@ -1,5 +1,5 @@ name: "sonnen" -version: "2.0.3" +version: "2.0.4" tier: community author: "Sourceful Labs AB" protocol: http @@ -16,9 +16,9 @@ tested_devices: notes: "Community driver, untested" min_driver_version: "1.0.0" min_host_version: "2.1.0" -size_bytes: 6631 +size_bytes: 6734 dkb_id: "" -sha256: "94ae5e0065f5793051acb89b7ef04a679b2898908dfac6f4ffd6e8deb77fa2c0" +sha256: "aee7b5ecf3b7666561084d434664f2df1019425f085ce36c4c4488baebfd1f76" signature: "" bytecode_sha256: "" diff --git a/support-status.json b/support-status.json index 65543b7..a3abf4b 100644 --- a/support-status.json +++ b/support-status.json @@ -1619,7 +1619,7 @@ }, { "catalog_source": true, - "catalog_version": "2.0.3", + "catalog_version": "2.0.4", "driver_id": "sonnen", "targets": { "blixt-l1": {