diff --git a/CHANGELOG.md b/CHANGELOG.md index e7844ad3..96e328ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -122,6 +122,49 @@ The driver is read-only, so no command went the wrong way. Not yet checked on hardware: on a Growatt, `battery.w` should be positive while the SoC rises. +### goodwe 2.3.0 + +The driver now reads PV, battery and meter values from the registers in +GoodWe's own map, "Modbus Protocol Hybrid for Energy Storage Inverters" +(2 February 2024). evcc's goodwe-hybrid template and the goodwe library read +the same registers and give the signs (srcfl/device-drivers#264). Not yet +checked on hardware. + +- **Battery.** Power is the S32 at 35182-35183, voltage 35180, current + 35181, SoC 37007 from the BMS block. GoodWe counts discharge as positive, + so the driver turns power and current round. Earlier releases read power + from 35180 (the battery voltage), voltage and current from 35178/35179 + (the BUS and NBUS voltages) and SoC from 35182. SoC is sent only while + the BMS talks to the inverter (37011), like the other BMS figures. When + 35184 says there is no battery, no battery stream goes out. +- **PV.** Power is the sum of the four MPPT powers (U32 W at 35105, 35109, + 35113, 35117). Earlier releases read PV1 power alone and scaled it by 0.1. + PV2 voltage and current are 35107/35108, not 35109/35110. Grid frequency + is 35123, not 35113 (PV3 power). +- **Meter.** The meter comes from the meter block: total power 36025, power + per phase 36019/36021/36023 (S32), frequency 36014, energy sold and bought + 36015/36017 (float32 Wh), and voltage and current per phase 36052-36057. + GoodWe counts export as positive; the driver turns it round. Earlier + releases read the inverter's own phase registers at 35121-35141 and the + energy counters at 35195/35199, which are inverter counters at best and, + for 35199, a daily value. No meter is sent while 36004 says the inverter + does not hear its meter. +- **Never fabricate.** A read that fails is left out, never sent as 0. The + driver is clean in `invented-number-baseline.json` (was 23 fields). +- **Old firmware.** GoodWe's map puts meter power at 36019-36041 only + above ARM firmware version 9 (35019, read once), and at 36005-36008 + before that. No source gives the sign of the old registers, so on ARM 9 + or older, or while the version is unknown, no meter is sent rather than + a 0 W one. +- **No-value sentinels.** A PV power or PV energy of 0xFFFFFFFF (as evcc + reads them) is left out, and an MPPT without a value adds nothing to PV + power. A SoC above 100 % (0xFFFF) is left out. +- **Tests.** `test_goodwe_map.py` encodes the map. The four Lua files under + `drivers/tests/lua_harness/` for the `community-v1` and `gw8kn-et-hk3000` + profiles are removed: no test ran them, and the driver lost those + profiles in #27. So is the `THIRD_PARTY_NOTICES.md` entry for the + `gw8kn-et-hk3000` profile, since no file derives from it any more. + ### goodwe 2.2.0, solis 2.2.0, sofar 2.2.0, solax 1.2.0, saj 1.3.0, growatt 2.2.0, solinteg 1.2.0 These drivers report what the battery can do now, where the device says so diff --git a/SUPPORT_STATUS.md b/SUPPORT_STATUS.md index 035e94bb..2948ba68 100644 --- a/SUPPORT_STATUS.md +++ b/SUPPORT_STATUS.md @@ -68,8 +68,8 @@ Catalog source is not proof that a target can install or run a driver. | goe | 1.1.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | goe_http | 1.1.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | | goe_http | 1.1.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | -| goodwe | 2.2.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | -| goodwe | 2.2.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | +| goodwe | 2.3.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | +| goodwe | 2.3.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | growatt | 2.2.2 | ftw-core | not_assessed | — | not_recorded | not_assessed | | growatt | 2.2.2 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | hardybarth | 1.1.1 | ftw-core | not_assessed | — | not_recorded | not_assessed | diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index ea5bf4dc..6ab7fa33 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -4,31 +4,3 @@ Copyright © 1994–2024 Lua.org, PUC-Rio. Lua is distributed under the MIT license. The full notice is retained at the end of `tools/lua55-src/lua.h`. - -## GoodWe GW8KN-ET register profile - -The `gw8kn-et-hk3000` register profile in `drivers/lua/goodwe.lua` derives from -`srcfl/hugin-drivers/drivers/goodwe_et.lua` at commit -`3125960a80b5237e3a5ac609963ddb1302367938`. frekes81 contributed the source. - -MIT License - -Copyright (c) 2026 Sourceful Labs - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/drivers/lua/goodwe.lua b/drivers/lua/goodwe.lua index 39cbca6e..5076935f 100644 --- a/drivers/lua/goodwe.lua +++ b/drivers/lua/goodwe.lua @@ -4,7 +4,9 @@ -- -- Register type: HOLDING (FC 0x03) -- Byte/word order: Big-Endian for multi-register values --- Tested: ET-Plus, EH series (GoodWe v2 LAN+WiFi dongle required) +-- Written for: ET-Plus, EH (GoodWe's ET platform, the 35000 register map). +-- Not checked on hardware yet. ES, EM and BP inverters speak another +-- protocol and are not covered. -- READ-ONLY: control not implemented. DRIVER = { @@ -13,7 +15,7 @@ DRIVER = { id = "goodwe", name = "GoodWe hybrid inverter", manufacturer = "GoodWe", - version = "2.2.0", + version = "2.3.0", protocols = { "modbus" }, capabilities = { "meter", "pv", "battery" }, read_only = true, @@ -66,40 +68,87 @@ local function probe_read(addr, count, kind) end ---------------------------------------------------------------------------- --- What the battery can do now (docs/WRITING-A-DRIVER.md) +-- Register map -- --- From GoodWe's own map, "GoodWe Modbus Protocol Hybrid for Energy Storage --- Inverters" (2 February 2024), which the goodwe library also follows: --- 35176 heat sink temperature S16 x 0.1 C --- 35180 battery 1 voltage U16 x 0.1 V (the BMS's --- value when it talks) --- 35184 battery 1 mode 0 = no battery, or --- disconnected +-- Addresses, types and scales from GoodWe's own map, "GoodWe Modbus +-- Protocol Hybrid for Energy Storage Inverters" (2 February 2024, in the +-- manifest's upstream_docs). evcc's goodwe-hybrid template and the goodwe +-- library (et.py) read the same registers. Neither document states a sign +-- in words; both integrations treat battery discharge and meter export as +-- positive (evcc scales meter power by -1 and passes battery power through +-- as "positive = discharging"; the goodwe library's house consumption adds +-- battery power and subtracts meter power). The driver turns both round at +-- the boundary. +-- +-- Inverter block, read as the goodwe library does from 35100: +-- 35103-35118 PV1..PV4: voltage U16 x 0.1 V, current U16 x 0.1 A, +-- power U32 W (35105, 35109, 35113, 35117) +-- 35123 R phase frequency, U16 x 0.01 Hz +-- 35176 heat sink temperature, S16 x 0.1 C +-- 35180 battery 1 voltage, U16 x 0.1 V (the BMS's value when it +-- talks to the inverter) +-- 35181 battery 1 current, S16 x 0.1 A +-- 35182-35183 battery 1 power, S32 W +-- 35184 battery 1 mode: 0 no battery or disconnected, 1 standby, +-- 2 discharging, 3 charging (Table 8-9) +-- 35191-35192 PV energy total, U32 x 0.1 kWh +-- 35019 ARM firmware version, U16, read once (device info block) +-- 0xFFFFFFFF in a U32 power or energy means "no value" (evcc reads these +-- as uint32 with that sentinel); it is left out, never added. +-- Meter block, from the meter the inverter reads (36000 and up): +-- 36004 meter communication, 1 OK, 0 failed +-- 36014 meter frequency, U16 x 0.01 Hz +-- 36015-36016 total energy sold to the grid, float32 Wh +-- 36017-36018 total energy bought from the grid, float32 Wh +-- 36019-36024 meter power per phase, S32 W +-- 36025-36026 meter total power, S32 W +-- 36052-36057 meter voltage per phase U16 x 0.1 V, then current per +-- phase U16 x 0.1 A +-- The float32 energy unit is not in GoodWe's table; evcc and the goodwe +-- library both divide by 1000 for kWh, so the raw value is Wh. +-- +-- Earlier releases read most of these from the wrong place +-- (srcfl/device-drivers#264): battery power from 35180 (the voltage), +-- battery voltage and current from 35178/35179 (the BUS and NBUS voltages), +-- SoC from 35182, total PV from 35105 alone scaled by 0.1, PV2 from 35109, +-- grid frequency from 35113 (PV3 power), and the meter from the inverter's +-- own phase registers at 35121-35141. +-- +-- Known limits, left for a field report: +-- * GoodWe's table says to read 36019-36041 when the ARM firmware +-- (35019) is above version 9, and 36005-36008 (S16) before that. On +-- older firmware 36019 and up would read 0, which looks like a site in +-- balance. No source gives the sign of 36005-36008, so on ARM 9 or +-- older, or while the version is unknown, no meter is sent. +-- * 36052-36057 are listed in GoodWe's February 2024 table. The goodwe +-- library reads them only on newer units, so on an older one the read +-- may fail; it is given up after three tries and the rest of the meter +-- still reports. +-- * evcc notes that 36017 reads 0 on some newer models (ETA-G20). +-- +-- What the battery can do now (docs/WRITING-A-DRIVER.md): -- 37000-37023 "BMS Operation Data", read as one block like the -- goodwe library does +-- 37003 BMS pack temperature x 0.1 C +-- 37004 max BMS charge current A +-- 37005 max BMS discharge current A +-- 37007 SoC of battery 1 % -- 37011 battery protocol, the codes of GoodWe's Table 8-33: -- 0x101 Pylontech, 0x102/0x105/0x106 BYD, 0x104 LG, -- 0x11E Dyness and others, 0x122 GoodWe. 0x11F, "EMS -- Battery", means the inverter has no direct link to the -- battery, and 0 means no protocol at all. --- 37003 BMS pack temperature x 0.1 C --- 37004 max BMS charge current A --- 37005 max BMS discharge current A -- 37020 max cell temperature x 0.1 C -- 37021 min cell temperature x 0.1 C -- The charge and discharge currents come from the BMS, so they move with -- temperature and charge level: a live limit, not a rating. -- -- A battery mode other than 0 says a battery is there, not that a BMS --- talks to the inverter. Without one the block answers zeros, and 0 C and --- 0 W would read as a cold battery that may not charge. So the block's --- figures count only while 37011 names a BMS protocol (not 0, 0x11F or --- 0xFFFF). A 0 A limit from a BMS that does talk is sent as 0 W. --- --- GoodWe's map puts the BUS voltage at 35178 and battery power at --- 35182-35183, where the battery reads in driver_poll still look. Those --- reads are left as they were (srcfl/device-drivers#264); this block reads --- its own voltage at 35180. +-- talks to the inverter. Without one the block answers zeros, and 0 C, +-- 0 W and 0 % would read as a cold, empty battery that may not charge. So +-- the block's figures, SoC included, count only while 37011 names a BMS +-- protocol (not 0, 0x11F or 0xFFFF). A 0 A limit from a BMS that does talk +-- is sent as 0 W. ---------------------------------------------------------------------------- -- A temperature outside -40..100 C or a power outside 0..100 kW is a @@ -138,6 +187,56 @@ local function bms_talks(protocol) and protocol ~= GOODWE_NO_DIRECT_BMS end +-- Unsigned and signed 32-bit values from two registers, high word first. +-- Both work on the 16-bit halves, so the sign survives a Lua build with +-- 32-bit integers (docs/WRITING-A-DRIVER.md, "Numbers"). +local function u32(hi, lo) + return (hi % 0x10000) * 0x10000 + (lo % 0x10000) +end + +-- A U32 power or energy, or nil for the 0xFFFFFFFF "no value" sentinel. +local function u32_value(hi, lo) + if hi % 0x10000 == 0xFFFF and lo % 0x10000 == 0xFFFF then return nil end + return u32(hi, lo) +end + +local function s32(hi, lo) + hi = hi % 0x10000 + lo = lo % 0x10000 + if hi >= 0x8000 then + return (hi - 0x10000) * 0x10000 + lo + end + return hi * 0x10000 + lo +end + +-- IEEE 754 float32, high word first, on the 16-bit halves. NaN and +-- infinity mean "no value" and give nil. +local function f32(hi, lo) + hi = hi % 0x10000 + lo = lo % 0x10000 + local sign = 1 + if hi >= 0x8000 then sign = -1; hi = hi - 0x8000 end + local exponent = math.floor(hi / 128) + local mantissa = (hi % 128) * 65536 + lo + if exponent == 0xFF then return nil end + if exponent == 0 then return sign * mantissa * 2 ^ -149 end + return sign * (1 + mantissa / 0x800000) * 2 ^ (exponent - 127) +end + +-- A lifetime energy counter in Wh, or nil when it is not a count. +local function energy_wh(hi, lo) + local wh = f32(hi, lo) + if wh == nil or wh < 0 then return nil end + return wh +end + +-- 0xFFFF in a meter voltage or current means "no value" (evcc decodes +-- these registers as uint16 with that sentinel). +local function tenths(raw) + if raw == nil or raw == 0xFFFF then return nil end + return raw * 0.1 +end + ---------------------------------------------------------------------------- -- Initialization ---------------------------------------------------------------------------- @@ -148,48 +247,34 @@ end ---------------------------------------------------------------------------- -- Telemetry polling +-- +-- A stream goes out only when its defining read answered, and each field +-- only from a register that answered (docs/WRITING-A-DRIVER.md, "Never +-- fabricate"). ---------------------------------------------------------------------------- -function driver_poll() - ------------------------------------------------------------------------ - -- PV - ------------------------------------------------------------------------ - - -- PV total power: 35105-35106, U32 BE × 0.1 W - local pvw_regs = probe_read(35105, 2, "holding") - local pv_w = 0 - if pvw_regs then - pv_w = host.decode_u32_be(pvw_regs[1], pvw_regs[2]) * 0.1 - end - - -- PV1: voltage @ 35103 (U16 × 0.1 V), current @ 35104 (U16 × 0.1 A) - local m1_regs = probe_read(35103, 2, "holding") - local mppt1_v, mppt1_a = 0, 0 - if m1_regs then - mppt1_v = m1_regs[1] * 0.1 - mppt1_a = m1_regs[2] * 0.1 - end - - -- PV2: voltage @ 35109 (U16 × 0.1 V), current @ 35110 (U16 × 0.1 A) - local m2_regs = probe_read(35109, 2, "holding") - local mppt2_v, mppt2_a = 0, 0 - if m2_regs then - mppt2_v = m2_regs[1] * 0.1 - mppt2_a = m2_regs[2] * 0.1 - end - - -- Grid frequency: 35113, U16 × 0.01 Hz - local hz_regs = probe_read(35113, 1, "holding") - local hz = 0 - if hz_regs then - hz = hz_regs[1] * 0.01 +local function poll_pv() + -- 35103-35123 in one read: four MPPTs, PV mode, then the R phase. + local regs = probe_read(35103, 21, "holding") + if not regs or regs[21] == nil then return end + + -- PV1..PV4 power at 35105, 35109, 35113, 35117. An MPPT without a + -- value adds nothing; with no value at all there is no PV reading. + local pv_w = nil + for _, i in ipairs({3, 7, 11, 15}) do + local w = u32_value(regs[i], regs[i + 1]) + if w ~= nil then pv_w = (pv_w or 0) + w end end - - -- PV lifetime generation: 35191-35192, U32 BE × 0.1 kWh - local pvgen_regs = probe_read(35191, 2, "holding") - local pv_gen_wh = 0 - if pvgen_regs then - pv_gen_wh = host.decode_u32_be(pvgen_regs[1], pvgen_regs[2]) * 0.1 * 1000 + if pv_w == nil then return end + local mppt1_v, mppt1_a = regs[1] * 0.1, regs[2] * 0.1 + local mppt2_v, mppt2_a = regs[5] * 0.1, regs[6] * 0.1 + local hz = regs[21] * 0.01 + + local lifetime_wh = nil + local energy = probe_read(35191, 2, "holding") + if energy and energy[2] ~= nil then + local tenths_kwh = u32_value(energy[1], energy[2]) + if tenths_kwh ~= nil then lifetime_wh = tenths_kwh * 100 end end host.emit("pv", { @@ -198,88 +283,58 @@ function driver_poll() mppt1_a = mppt1_a, mppt2_v = mppt2_v, mppt2_a = mppt2_a, - lifetime_wh = pv_gen_wh, + lifetime_wh = lifetime_wh, }) - -- Diagnostics: long-format TS DB host.emit_metric("pv_mppt1_v", mppt1_v) host.emit_metric("pv_mppt1_a", mppt1_a) host.emit_metric("pv_mppt2_v", mppt2_v) host.emit_metric("pv_mppt2_a", mppt2_a) host.emit_metric("grid_hz", hz) +end - ------------------------------------------------------------------------ - -- Battery - ------------------------------------------------------------------------ - - -- Battery voltage: 35178, U16 × 0.1 V - local bv_regs = probe_read(35178, 1, "holding") - local bat_v = 0 - if bv_regs then - bat_v = bv_regs[1] * 0.1 - end - - -- Battery current: 35179, I16 × 0.1 A - local ba_regs = probe_read(35179, 1, "holding") - local bat_a = 0 - if ba_regs then - bat_a = host.decode_i16(ba_regs[1]) * 0.1 - end - - -- Battery power: 35180, I16, W (positive=charge, negative=discharge — matches site convention) - local bw_regs = probe_read(35180, 1, "holding") - local bat_w = 0 - if bw_regs then - bat_w = host.decode_i16(bw_regs[1]) - end - - -- Battery SoC: 35182, U16, percent - local bsoc_regs = probe_read(35182, 1, "holding") - local bat_soc = 0 - if bsoc_regs then - bat_soc = bsoc_regs[1] / 100 -- percent → 0-1 fraction - end - +local function poll_battery() + -- 35180-35184: voltage, current, power (two words), mode. + local regs = probe_read(35180, 5, "holding") + if not regs or regs[5] == nil then return end + local mode = regs[5] + if mode == 0 then return end -- no battery, or disconnected + + local volts = regs[1] * 0.1 + -- GoodWe counts discharge as positive; the site counts charge as + -- positive. The current carries the same sign as the power, since the + -- voltage is never negative. + local amps = -host.decode_i16(regs[2]) * 0.1 local battery = { - w = bat_w, - v = bat_v, - a = bat_a, - soc = bat_soc, + w = -s32(regs[3], regs[4]), + v = volts, + a = amps, } - -- Battery 1 voltage and mode: 35180-35184. The BMS block is read only - -- while the mode says a battery is connected, and its figures count - -- only while 37011 says a BMS talks (see the top of this file). - local b1_regs = probe_read(35180, 5, "holding") - local b1_v, b1_mode - if b1_regs and b1_regs[5] ~= nil then - b1_v = b1_regs[1] * 0.1 - b1_mode = b1_regs[5] + local bms_regs = probe_read(37000, 24, "holding") + if bms_regs and bms_regs[24] == nil then bms_regs = nil end + if bms_regs and not bms_talks(bms_regs[12]) then -- 37011 + bms_regs = nil end - local bms_regs - if b1_mode ~= nil and b1_mode ~= 0 then - bms_regs = probe_read(37000, 24, "holding") - if bms_regs and bms_regs[24] == nil then bms_regs = nil end - if bms_regs and not bms_talks(bms_regs[12]) then -- 37011 - bms_regs = nil + if bms_regs then + if bms_regs[8] <= 100 then -- 37007 + battery.soc = bms_regs[8] / 100 -- 0xFFFF: no value end - end - if bms_regs then local bat_temp = goodwe_temp(bms_regs[4]) -- 37003 if bat_temp ~= nil then battery.temp_c = bat_temp host.emit_metric("battery_temp_c", bat_temp) end - local charge_w = limit_w(bms_regs[5], b1_v) -- 37004 + local charge_w = limit_w(bms_regs[5], volts) -- 37004 if charge_w ~= nil then battery.available_charge_W = charge_w host.emit_metric("battery_charge_limit_a", bms_regs[5]) host.emit_metric("battery_available_charge_w", charge_w) end - local discharge_w = limit_w(bms_regs[6], b1_v) -- 37005 + local discharge_w = limit_w(bms_regs[6], volts) -- 37005 if discharge_w ~= nil then battery.available_discharge_W = discharge_w host.emit_metric("battery_discharge_limit_a", bms_regs[6]) @@ -297,94 +352,81 @@ function driver_poll() end host.emit("battery", battery) - host.emit_metric("battery_dc_v", bat_v) - host.emit_metric("battery_dc_a", bat_a) - - -- Inverter heat sink temperature: 35176, S16 × 0.1 C. - local itemp_regs = probe_read(35176, 1, "holding") - if itemp_regs then - local inv_temp = goodwe_temp(itemp_regs[1]) - if inv_temp ~= nil then - host.emit_metric("inverter_temp_c", inv_temp) - end - end - - ------------------------------------------------------------------------ - -- Meter (grid connection point) - ------------------------------------------------------------------------ - - -- GoodWe reports grid meter power with the OPPOSITE sign from site - -- convention (positive = export). Negate at the boundary so everything - -- above the driver layer sees positive = import. - - -- Total: 35140-35141, I32 BE, W - local mw_regs = probe_read(35140, 2, "holding") - local meter_w = 0 - if mw_regs then - meter_w = -host.decode_i32_be(mw_regs[1], mw_regs[2]) - end - - -- Per-phase V/A interleaved at 35121-35126: L1_V, L1_A, L2_V, L2_A, L3_V, L3_A - -- (U16, voltage × 0.1 V, current × 0.1 A). One read, six registers. - local va_regs = probe_read(35121, 6, "holding") - local l1_v, l1_a, l2_v, l2_a, l3_v, l3_a = 0, 0, 0, 0, 0, 0 - if va_regs then - l1_v = va_regs[1] * 0.1 - l1_a = va_regs[2] * 0.1 - l2_v = va_regs[3] * 0.1 - l2_a = va_regs[4] * 0.1 - l3_v = va_regs[5] * 0.1 - l3_a = va_regs[6] * 0.1 - end + host.emit_metric("battery_dc_v", volts) + host.emit_metric("battery_dc_a", amps) +end - -- Per-phase power at 35132-35137: L1, L2, L3 (I32 BE each pair). One read. - local lw_regs = probe_read(35132, 6, "holding") - local l1_w, l2_w, l3_w = 0, 0, 0 - if lw_regs then - l1_w = -host.decode_i32_be(lw_regs[1], lw_regs[2]) - l2_w = -host.decode_i32_be(lw_regs[3], lw_regs[4]) - l3_w = -host.decode_i32_be(lw_regs[5], lw_regs[6]) - end +-- ARM firmware version (35019), read once. nil until it answers. +local arm_version = nil +local old_firmware_logged = false - -- Total import energy: 35195-35196, U32 BE × 0.1 kWh - local imp_regs = probe_read(35195, 2, "holding") - local import_wh = 0 - if imp_regs then - import_wh = host.decode_u32_be(imp_regs[1], imp_regs[2]) * 0.1 * 1000 +local function poll_meter() + if arm_version == nil then + local fw = probe_read(35019, 1, "holding") + if fw and fw[1] ~= nil then arm_version = fw[1] end end - - -- Total export energy: 35199-35200, U32 BE × 0.1 kWh - local exp_regs = probe_read(35199, 2, "holding") - local export_wh = 0 - if exp_regs then - export_wh = host.decode_u32_be(exp_regs[1], exp_regs[2]) * 0.1 * 1000 + -- Meter power sits at 36019-36026 only above ARM version 9 (see the + -- top of this file). Older or unknown firmware: no meter. + if arm_version == nil then return end + if arm_version <= 9 then + if not old_firmware_logged then + old_firmware_logged = true + host.log("info", string.format( + "GoodWe: ARM firmware %d keeps meter power at 36005-36008, " .. + "whose sign no source gives; no meter is sent", arm_version)) + end + return end - host.emit("meter", { - w = meter_w, + -- 36004-36026 in one read. Without the meter (36004 not 1) the + -- inverter has no grid reading to give, so no meter is sent. + local regs = probe_read(36004, 23, "holding") + if not regs or regs[23] == nil then return end + if regs[1] ~= 1 then return end + + -- GoodWe counts export as positive; the site counts import as positive. + local l1_w = -s32(regs[16], regs[17]) -- 36019 + local l2_w = -s32(regs[18], regs[19]) -- 36021 + local l3_w = -s32(regs[20], regs[21]) -- 36023 + local meter = { + w = -s32(regs[22], regs[23]), -- 36025 l1_w = l1_w, l2_w = l2_w, l3_w = l3_w, - l1_v = l1_v, - l2_v = l2_v, - l3_v = l3_v, - l1_a = l1_a, - l2_a = l2_a, - l3_a = l3_a, - hz = hz, - import_wh = import_wh, - export_wh = export_wh, - }) + hz = regs[11] * 0.01, -- 36014 + export_wh = energy_wh(regs[12], regs[13]), -- 36015 + import_wh = energy_wh(regs[14], regs[15]), -- 36017 + } host.emit_metric("meter_l1_w", l1_w) host.emit_metric("meter_l2_w", l2_w) host.emit_metric("meter_l3_w", l3_w) - host.emit_metric("meter_l1_v", l1_v) - host.emit_metric("meter_l2_v", l2_v) - host.emit_metric("meter_l3_v", l3_v) - host.emit_metric("meter_l1_a", l1_a) - host.emit_metric("meter_l2_a", l2_a) - host.emit_metric("meter_l3_a", l3_a) + local va = probe_read(36052, 6, "holding") + if va and va[6] ~= nil then + meter.l1_v, meter.l2_v, meter.l3_v = tenths(va[1]), tenths(va[2]), tenths(va[3]) + meter.l1_a, meter.l2_a, meter.l3_a = tenths(va[4]), tenths(va[5]), tenths(va[6]) + for _, key in ipairs({"l1_v", "l2_v", "l3_v", "l1_a", "l2_a", "l3_a"}) do + if meter[key] ~= nil then host.emit_metric("meter_" .. key, meter[key]) end + end + end + + host.emit("meter", meter) +end + +function driver_poll() + poll_pv() + poll_battery() + + -- Inverter heat sink temperature: 35176, S16 x 0.1 C. + local itemp_regs = probe_read(35176, 1, "holding") + if itemp_regs then + local inv_temp = goodwe_temp(itemp_regs[1]) + if inv_temp ~= nil then + host.emit_metric("inverter_temp_c", inv_temp) + end + end + + poll_meter() return 5000 end diff --git a/drivers/tests/invented-number-baseline.json b/drivers/tests/invented-number-baseline.json index 3bf2d056..1aa5f20d 100644 --- a/drivers/tests/invented-number-baseline.json +++ b/drivers/tests/invented-number-baseline.json @@ -24,7 +24,6 @@ "ctek_v2": 9, "deye": 26, "fronius": 10, - "goodwe": 23, "hello": 3, "janitza": 13, "kostal": 21, diff --git a/drivers/tests/lua_harness/goodwe_fixtures.lua b/drivers/tests/lua_harness/goodwe_fixtures.lua deleted file mode 100644 index a8694683..00000000 --- a/drivers/tests/lua_harness/goodwe_fixtures.lua +++ /dev/null @@ -1,76 +0,0 @@ --- Synthetic fixtures. Replace or add masked hardware captures after the pilot. - -return { - ["community-v1"] = { - batches = { - {35105, 2}, {35103, 2}, {35109, 2}, {35113, 1}, {35191, 2}, - {35178, 1}, {35179, 1}, {35180, 1}, {35182, 1}, {35183, 1}, - {35140, 2}, {35132, 2}, {35134, 2}, {35136, 2}, - {35121, 1}, {35123, 1}, {35125, 1}, - {35122, 1}, {35124, 1}, {35126, 1}, - {35195, 2}, {35199, 2}, - }, - registers = { - [35103] = {3500, 80}, - [35105] = {0, 50000}, - [35109] = {3400, 70}, - [35113] = {5000}, - [35191] = {0, 2000}, - [35178] = {5000}, - [35179] = {65436}, - [35180] = {1200}, - [35182] = {75}, - [35183] = {250}, - [35140] = {65535, 64036}, - [35132] = {65535, 65036}, - [35134] = {65535, 65036}, - [35136] = {65535, 65036}, - [35121] = {2300}, - [35123] = {2310}, - [35125] = {2290}, - [35122] = {50}, - [35124] = {55}, - [35126] = {60}, - [35195] = {0, 1000}, - [35199] = {0, 500}, - }, - failure_address = 35134, - expected = { - pv_w = -5000, - meter_w = 1500, - l1_w = 500, - battery_w = 1200, - soc = 0.75, - hz = 50, - import_wh = 100000, - export_wh = 50000, - }, - }, - ["gw8kn-et-hk3000"] = { - batches = { - {35107, 4}, {35123, 1}, {35125, 11}, {35138, 3}, - {35145, 13}, {35164, 5}, {35178, 6}, {35195, 5}, - }, - registers = { - [35107] = {3500, 80, 0, 2800}, - [35123] = {5000}, - [35125] = {1000, 0, 0, 0, 0, 1100, 0, 0, 0, 0, 1200}, - [35138] = {5000, 0, 64036}, - [35145] = {2300, 0, 0, 0, 0, 0, 2310, 0, 0, 0, 0, 0, 2290}, - [35164] = {1500, 0, 1600, 0, 1700}, - [35178] = {5000, 0, 1200, 0, 75, 250}, - [35195] = {0, 1000, 0, 0, 500}, - }, - failure_address = 35145, - expected = { - pv_w = -5000, - meter_w = 1500, - l1_w = 500, - battery_w = 1200, - soc = 0.75, - hz = 50, - import_wh = 100000, - export_wh = 50000, - }, - }, -} diff --git a/drivers/tests/lua_harness/test_all_drivers.lua b/drivers/tests/lua_harness/test_all_drivers.lua index 3f24af49..d1a2e1f5 100644 --- a/drivers/tests/lua_harness/test_all_drivers.lua +++ b/drivers/tests/lua_harness/test_all_drivers.lua @@ -135,9 +135,9 @@ local function setup_modbus_data(driver_name) -- and applies a specific formula. We set these to produce values in 0-1. --------------------------------------------------------------------------- - -- GoodWe: reg 35182, formula = val / 100 -> set to 50 => 0.50 - holding[35182] = 50 - input[35182] = 50 + -- GoodWe: reg 37007, formula = val / 100 -> set to 50 => 0.50 + holding[37007] = 50 + input[37007] = 50 -- Sungrow: reg 13022 (4th of 13019+4), formula = val * 0.1 / 100 -- bat_regs[4] * 0.1 / 100 -> set to 500 => 0.50 @@ -176,7 +176,7 @@ local function setup_modbus_data(driver_name) -- or 500 (=> 0.50 with *0.1/100) local soc_50_addrs = { -- Direct /100 pattern - 35182, -- GoodWe + 37007, -- GoodWe 843, -- Victron 588, -- Deye 1068, -- VARTA @@ -271,6 +271,11 @@ local function setup_modbus_data(driver_name) input[40960] = 1 input[40971] = 1 input[40972] = 5000 + elseif driver_name == "goodwe" then + -- The inverter hears its meter (36004 = 1) and the BMS talks + -- (37011 = 0x101 Pylontech). SoC 37007 is set with the others below. + holding[36004] = 1 + holding[37011] = 0x101 end --------------------------------------------------------------------------- @@ -280,7 +285,7 @@ local function setup_modbus_data(driver_name) -- Pattern: val / 100 => set to 50 local soc_div100 = { - 35182, -- GoodWe + 37007, -- GoodWe 843, -- Victron modbus 588, -- Deye 1068, -- VARTA diff --git a/drivers/tests/lua_harness/test_goodwe_failures.lua b/drivers/tests/lua_harness/test_goodwe_failures.lua deleted file mode 100644 index d60c6be2..00000000 --- a/drivers/tests/lua_harness/test_goodwe_failures.lua +++ /dev/null @@ -1,67 +0,0 @@ -local profile = arg[1] -local scenario = arg[2] -if not profile or not scenario then - error("profile and failure scenario are required") -end - -dofile("drivers/tests/lua_harness/host_mock.lua") -local fixtures = dofile("drivers/tests/lua_harness/goodwe_fixtures.lua") -local fixture = fixtures[profile] -if not fixture then error("unknown fixture profile: " .. tostring(profile)) end - -local function load_fixture() - host.reset() - for address, registers in pairs(fixture.registers) do - host._modbus_registers.holding[address] = registers - end - dofile("drivers/lua/goodwe.lua") - driver_init({profile = profile}) -end - -local function emission_count() - local count = 0 - for _, stream in ipairs({"pv", "battery", "meter"}) do - count = count + ((host._emitted[stream] and #host._emitted[stream]) or 0) - end - return count -end - -load_fixture() - -if scenario == "all" then - host._modbus_read_error = "simulated Modbus timeout" - local ok = pcall(driver_poll) - if ok then error("total timeout did not fail the poll") end - if emission_count() ~= 0 then error("total timeout emitted telemetry") end -elseif scenario == "middle" then - host._modbus_read_fail_addresses[fixture.failure_address] = "simulated batch timeout" - local ok = pcall(driver_poll) - if ok then error("middle batch timeout did not fail the poll") end - if emission_count() ~= 0 then error("middle batch timeout emitted telemetry") end -elseif scenario == "short" then - local last = fixture.batches[#fixture.batches] - host._modbus_read_short_counts[last[1]] = last[2] - 1 - local ok = pcall(driver_poll) - if ok then error("short batch did not fail the poll") end - if emission_count() ~= 0 then error("short batch emitted telemetry") end -elseif scenario == "sentinel" then - if profile ~= "gw8kn-et-hk3000" then error("sentinel is only a GW8KN fixture") end - fixture.registers[35138][1] = 65535 - local ok = pcall(driver_poll) - if ok then error("negative PV sentinel did not fail the poll") end - if emission_count() ~= 0 then error("negative PV sentinel emitted telemetry") end -elseif scenario == "recover" then - driver_poll() - if emission_count() ~= 3 then error("first complete poll did not emit all streams") end - host._modbus_read_fail_addresses[fixture.failure_address] = "simulated mute session" - local ok = pcall(driver_poll) - if ok then error("mute session did not fail the poll") end - if emission_count() ~= 3 then error("failed recovery poll emitted telemetry") end - host._modbus_read_fail_addresses = {} - driver_poll() - if emission_count() ~= 6 then error("post-reconnect poll did not recover") end -else - error("unknown failure scenario: " .. scenario) -end - -if host._modbus_write_attempts ~= 0 then error("read-only failure path attempted a write") end diff --git a/drivers/tests/lua_harness/test_goodwe_ftw_contract.lua b/drivers/tests/lua_harness/test_goodwe_ftw_contract.lua deleted file mode 100644 index ef2ad609..00000000 --- a/drivers/tests/lua_harness/test_goodwe_ftw_contract.lua +++ /dev/null @@ -1,65 +0,0 @@ -dofile("drivers/tests/lua_harness/host_mock.lua") -host.reset() - --- Model the FTW v1 host exactly for the calls GoodWe uses. The old generic --- aliases do not exist in FTW and must not hide a source compatibility bug. -host.decode_u32 = nil -host.decode_i32 = nil -host.decode_u32_be = function(hi, lo) - return (hi & 0xFFFF) * 65536 + (lo & 0xFFFF) -end -host.decode_i32_be = function(hi, lo) - local value = (hi & 0xFFFF) * 65536 + (lo & 0xFFFF) - if value >= 0x80000000 then return value - 0x100000000 end - return value -end -host.log = function(level, message) - if type(level) ~= "string" or type(message) ~= "string" then - error("FTW host.log requires level and message") - end -end - -dofile("drivers/lua/goodwe.lua") - -if type(DRIVER) ~= "table" or DRIVER.id ~= "goodwe" or type(DRIVER.version) ~= "string" then - error("GoodWe FTW identity metadata is wrong") -end -if DRIVER.host_api_min ~= 1 or DRIVER.host_api_max ~= 1 or DRIVER.read_only ~= true then - error("GoodWe FTW host API or read-only metadata is wrong") -end -if type(DRIVER.protocols) ~= "table" or DRIVER.protocols[1] ~= "modbus" then - error("GoodWe FTW protocol metadata is wrong") -end -if DRIVER.connection_defaults.unit_id ~= 1 then - error("GoodWe legacy connection default changed") -end -if type(driver_init) ~= "function" or type(driver_poll) ~= "function" or - type(driver_command) ~= "function" or type(driver_default_mode) ~= "function" then - error("GoodWe FTW lifecycle is incomplete") -end - -local fixture = dofile("drivers/tests/lua_harness/goodwe_fixtures.lua")["gw8kn-et-hk3000"] -for address, registers in pairs(fixture.registers) do - host._modbus_registers.holding[address] = registers -end - -driver_init({profile = "gw8kn-et-hk3000"}) -local ok, poll_error = pcall(driver_poll) -if not ok then error("GoodWe failed against the FTW v1 host: " .. tostring(poll_error)) end -if host._make ~= "GoodWe" then error("GoodWe did not report its make") end -if not host._emitted.pv or host._emitted.pv[1].w ~= -5000 then - error("GoodWe FTW PV decode or sign is wrong") -end -if not host._emitted.meter or host._emitted.meter[1].w ~= 1500 then - error("GoodWe FTW meter decode or sign is wrong") -end - -local writes_before = host._modbus_write_attempts -if driver_command("battery", 1000, {}) ~= false then - error("GoodWe read-only command did not reject control") -end -driver_default_mode() -driver_cleanup() -if host._modbus_write_attempts ~= writes_before then - error("GoodWe read-only lifecycle attempted a write") -end diff --git a/drivers/tests/lua_harness/test_goodwe_profiles.lua b/drivers/tests/lua_harness/test_goodwe_profiles.lua deleted file mode 100644 index abd9c7ff..00000000 --- a/drivers/tests/lua_harness/test_goodwe_profiles.lua +++ /dev/null @@ -1,92 +0,0 @@ -local profile = arg[1] -local mode = arg[2] or "import-charge" -local init_mode = arg[3] or "explicit" -if not profile then error("profile is required") end - -dofile("drivers/tests/lua_harness/host_mock.lua") -host.reset() -local fixtures = dofile("drivers/tests/lua_harness/goodwe_fixtures.lua") -local fixture = fixtures[profile] -if not fixture then error("unknown fixture profile: " .. tostring(profile)) end -if mode == "export-discharge" then - if profile == "community-v1" then - fixture.registers[35132] = {0, 500} - fixture.registers[35134] = {0, 500} - fixture.registers[35136] = {0, 500} - fixture.registers[35140] = {0, 1500} - fixture.registers[35180][1] = 64336 - else - fixture.registers[35138][3] = 1500 - fixture.registers[35164][1] = 500 - fixture.registers[35164][3] = 600 - fixture.registers[35164][5] = 700 - fixture.registers[35178][3] = 64336 - end - fixture.expected.meter_w = -1500 - fixture.expected.l1_w = -500 - fixture.expected.battery_w = -1200 -elseif mode == "zero-voltage" then - if profile ~= "gw8kn-et-hk3000" then error("zero-voltage is only a GW8KN fixture") end - fixture.registers[35145][1] = 0 - fixture.registers[35145][7] = 0 - fixture.registers[35145][13] = 0 -elseif mode == "night-zero" then - if profile ~= "gw8kn-et-hk3000" then error("night-zero is only a GW8KN fixture") end - fixture.registers[35138][1] = 0 - fixture.expected.pv_w = 0 -elseif mode ~= "import-charge" then - error("unknown fixture mode: " .. mode) -end -for address, registers in pairs(fixture.registers) do - host._modbus_registers.holding[address] = registers -end - -dofile("drivers/lua/goodwe.lua") -if init_mode == "legacy-default" then - driver_init({}) -else - driver_init({profile = profile}) -end -driver_poll() - -local function close(actual, expected, name) - if actual == nil or math.abs(actual - expected) > 0.000001 then - error(string.format("%s=%s, expected=%s", name, tostring(actual), tostring(expected))) - end -end - -local expected = fixture.expected -close(host._emitted.pv[1].w, expected.pv_w, "pv.w") -close(host._emitted.meter[1].w, expected.meter_w, "meter.w") -close(host._emitted.meter[1].l1_w, expected.l1_w, "meter.l1_w") -close(host._emitted.battery[1].w, expected.battery_w, "battery.w") -close(host._emitted.battery[1].soc, expected.soc, "battery.soc") -close(host._emitted.meter[1].hz, expected.hz, "meter.hz") -close(host._emitted.meter[1].import_wh, expected.import_wh, "meter.import_wh") -close(host._emitted.meter[1].export_wh, expected.export_wh, "meter.export_wh") -if mode == "zero-voltage" then - if host._emitted.meter[1].l1_a ~= nil or host._emitted.meter[1].l2_a ~= nil or - host._emitted.meter[1].l3_a ~= nil then - error("zero voltage fabricated a phase current") - end -end - -local reads = {} -for _, call in ipairs(host._calls) do - if call.func == "modbus_read" then table.insert(reads, call.args) end -end -if #reads ~= #fixture.batches then - error(string.format("profile %s made %d reads, expected %d", profile, #reads, #fixture.batches)) -end -for index, batch in ipairs(fixture.batches) do - local read = reads[index] - if read[1] ~= batch[1] or read[2] ~= batch[2] or read[3] ~= "holding" then - error(string.format("read %d was %s/%s/%s", index, tostring(read[1]), tostring(read[2]), tostring(read[3]))) - end -end - -local writes_before = host._modbus_write_attempts -if driver_command("battery", 1000, {}) ~= false then error("read-only command was accepted") end -driver_default_mode() -driver_cleanup() -if host._modbus_write_attempts ~= writes_before then error("read-only lifecycle attempted a write") end diff --git a/drivers/tests/test_goodwe_battery_now.py b/drivers/tests/test_goodwe_battery_now.py index 554a505c..25ff22c4 100644 --- a/drivers/tests/test_goodwe_battery_now.py +++ b/drivers/tests/test_goodwe_battery_now.py @@ -145,9 +145,14 @@ def test_goodwe_zero_limit_means_not_allowed_now(): def test_goodwe_no_battery_mode_leaves_bms_figures_out(): - """Mode 0 is "no battery": the BMS block is not read and nothing is sent.""" + """Mode 0 is "no battery": the BMS block is not read and nothing is sent. + + Since 2.3.0 no battery stream goes out either (docs/WRITING-A-DRIVER.md, + "A hybrid inverter may have no battery"); the goodwe library also treats + mode 0 as "no battery". + """ out = poll(registers(mode=0)) - assert out["BATTERY"] == "true" + assert out["BATTERY"] == "false" for field in FIELDS: assert out[f"F_{field}"] == "nil", field for metric in METRICS[:-1]: diff --git a/drivers/tests/test_goodwe_map.py b/drivers/tests/test_goodwe_map.py new file mode 100644 index 00000000..27b74010 --- /dev/null +++ b/drivers/tests/test_goodwe_map.py @@ -0,0 +1,401 @@ +"""GoodWe ET / EH: PV, battery and meter come from GoodWe's documented map. + +Up to 2.2.0 the driver read most of these values from the wrong registers +(srcfl/device-drivers#264): battery power from 35180 (the battery voltage), +battery voltage from 35178 (the BUS voltage), SoC from 35182 (the high word +of battery power), total PV from 35105 (PV1 power alone, scaled by 0.1), +grid frequency from 35113 (PV3 power), and the meter from the inverter's +own phase registers. These tests encode GoodWe's map: + +- GoodWe, "Modbus Protocol Hybrid for Energy Storage Inverters", + 2 February 2024 (manifest upstream_docs): addresses, types and scales. +- evcc's goodwe-hybrid template and the goodwe library (et.py): the same + addresses, and the signs. GoodWe counts battery discharge and meter + export as positive; the driver turns both round. + +All values are synthetic, not a capture. +""" + +import math +import struct +import subprocess +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[2] +LUA = ROOT / "lua55" +HARNESS = ROOT / "drivers" / "tests" / "lua_harness" +DRIVER = ROOT / "drivers" / "lua" / "goodwe.lua" + +pytestmark = pytest.mark.skipif( + not LUA.exists(), reason="run make check to build ./lua55") + +PYLONTECH = 0x101 # GoodWe Table 8-33, a BMS that talks + + +def u32(value: int) -> list[int]: + value &= 0xFFFFFFFF + return [value >> 16, value & 0xFFFF] + + +def s16(value: int) -> int: + return value & 0xFFFF + + +def f32(value: float) -> list[int]: + hi, lo = struct.unpack(">HH", struct.pack(">f", value)) + return [hi, lo] + + +NAN = [0x7FC0, 0x0000] + + +def pv_block(*, p1=2800, p2=2300, p3=0, p4=0, hz_raw=5001) -> list[int]: + """35103-35123: PV1..PV4 V/A/P, PV mode, R-phase V/A/Hz.""" + return ([3500, 80] + u32(p1) # 35103 V, 35104 A, 35105-35106 W + + [3400, 70] + u32(p2) # 35107-35110 + + [0, 0] + u32(p3) # 35111-35114 + + [0, 0] + u32(p4) # 35115-35118 + + [0, 0] # 35119-35120 PV mode + + [2300, 50, hz_raw]) # 35121 V, 35122 A, 35123 Hz + + +def battery_block(*, volts_raw=5120, amps=39, watts=2000, mode=2) -> list[int]: + """35180-35184. GoodWe signs: positive current and power = discharging.""" + return [volts_raw, s16(amps)] + u32(watts) + [mode] + + +def bms_block(*, soc=76, protocol=PYLONTECH) -> list[int]: + bms = [0] * 24 + bms[7] = soc # 37007, % + bms[11] = protocol # 37011 + return bms + + +def meter_block(*, comm=1, total=1200, phases=(400, 500, 300), + export_wh=1234500.0, import_wh=6789000.0, + hz_raw=5002) -> list[int]: + """36004-36026. GoodWe signs: positive meter power = export.""" + regs = [0] * 23 + regs[0] = comm # 36004 meter communication + regs[10] = hz_raw # 36014, x 0.01 Hz + regs[11:13] = (export_wh if isinstance(export_wh, list) + else f32(export_wh)) # 36015-36016 float + regs[13:15] = (import_wh if isinstance(import_wh, list) + else f32(import_wh)) # 36017-36018 float + regs[15:17] = u32(phases[0]) # 36019-36020 S32 + regs[17:19] = u32(phases[1]) # 36021-36022 + regs[19:21] = u32(phases[2]) # 36023-36024 + regs[21:23] = u32(total) # 36025-36026 + return regs + + +def site(over: dict[int, list[int]] | None = None) -> dict[int, list[int]]: + regs = { + 35103: pv_block(), + 35191: u32(123456), # x 0.1 kWh + 35176: [455], + 35180: battery_block(), + 37000: bms_block(), + 35019: [19], # ARM firmware version + 36004: meter_block(), + 36052: [2301, 2302, 2303, 51, 62, 73], + } + regs.update(over or {}) + return regs + + +def poll(regs: dict[int, list[int]], fail: tuple[int, ...] = (), + polls: int = 1) -> dict[str, str]: + setup = "\n".join( + f"host._modbus_registers.holding[{addr}] = " + "{" + ", ".join(str(v) for v in vals) + "}" + for addr, vals in regs.items()) + setup += "\n" + "\n".join( + f'host._modbus_read_fail_addresses[{addr}] = "Illegal Data Address"' + for addr in fail) + script = f''' +package.path = "{HARNESS}/?.lua;" .. package.path +require("host_mock") +host.reset() +{setup} +dofile("{DRIVER}") +driver_init({{}}) +for i = 1, {polls} do + host._emitted = {{}} + host._metrics = {{}} + local ok, err = pcall(driver_poll) + if not ok then print("POLL_ERROR " .. tostring(err)) os.exit(1) end +end +local reads = {{}} +for _, c in ipairs(host._calls) do + if c.func == "modbus_read" then reads[#reads + 1] = tostring(c.args[1]) end +end +print("READS " .. table.concat(reads, ",")) +for _, der in ipairs({{"pv", "battery", "meter"}}) do + local list = host._emitted[der] + print("EMITTED_" .. der .. " " .. tostring(list ~= nil and #list > 0)) + if list and #list > 0 then + for k, v in pairs(list[#list]) do print(der .. "." .. k .. " " .. tostring(v)) end + end +end +for name, m in pairs(host._metrics) do print("metric." .. name .. " " .. tostring(m.value)) end +''' + result = subprocess.run([str(LUA), "-e", script], + capture_output=True, text=True, cwd=ROOT) + assert result.returncode == 0, result.stdout + result.stderr + return dict(line.split(" ", 1) + for line in result.stdout.strip().splitlines() if " " in line) + + +def val(out: dict[str, str], key: str) -> float: + assert key in out, f"{key} missing from {sorted(out)}" + return float(out[key]) + + +def close(out, key, expected): + assert math.isclose(val(out, key), expected, rel_tol=1e-6, abs_tol=1e-9), \ + f"{key}={out.get(key)}, expected {expected}" + + +# ---------------------------------------------------------------- PV + +def test_pv_power_is_the_sum_of_the_four_mppt_powers(): + # 35105, 35109, 35113, 35117 are U32 watts, one per MPPT. + out = poll(site({35103: pv_block(p1=2800, p2=2300, p3=150, p4=50)})) + close(out, "pv.w", -5300) + + +def test_pv_power_uses_both_words(): + out = poll(site({35103: pv_block(p1=70000, p2=0)})) + close(out, "pv.w", -70000) + + +def test_pv_mppt_voltages_and_currents(): + out = poll(site()) + close(out, "pv.mppt1_v", 350.0) + close(out, "pv.mppt1_a", 8.0) + close(out, "pv.mppt2_v", 340.0) # 35107, not 35109 + close(out, "pv.mppt2_a", 7.0) # 35108 + close(out, "metric.pv_mppt2_v", 340.0) + + +def test_pv_lifetime_energy(): + out = poll(site()) + close(out, "pv.lifetime_wh", 12345600) # 12345.6 kWh + + +def test_grid_frequency_comes_from_35123_not_pv3_power(): + out = poll(site({35103: pv_block(p3=4000, hz_raw=4998)})) + close(out, "metric.grid_hz", 49.98) + + +def test_pv_mppt_without_a_value_is_left_out_of_the_sum(): + # 0xFFFFFFFF is "no value" (evcc reads these as uint32 with that + # sentinel). An unused PV3/PV4 must not add 4.3 GW. + out = poll(site({35103: pv_block(p1=2800, p2=2300, + p3=0xFFFFFFFF, p4=0xFFFFFFFF)})) + close(out, "pv.w", -5100) + + +def test_no_pv_power_at_all_sends_no_pv(): + nan = 0xFFFFFFFF + out = poll(site({35103: pv_block(p1=nan, p2=nan, p3=nan, p4=nan)})) + assert out["EMITTED_pv"] == "false" + + +def test_pv_lifetime_energy_without_a_value_is_left_out(): + out = poll(site({35191: [0xFFFF, 0xFFFF]})) + assert "pv.lifetime_wh" not in out + close(out, "pv.w", -5100) + + +def test_failed_pv_read_sends_no_pv(): + out = poll(site(), fail=(35103,)) + assert out["EMITTED_pv"] == "false" + assert "metric.pv_mppt1_v" not in out + assert "metric.grid_hz" not in out + assert out["EMITTED_meter"] == "true" # the rest still reports + + +def test_failed_pv_energy_read_leaves_only_that_out(): + out = poll(site(), fail=(35191,)) + assert "pv.lifetime_wh" not in out + close(out, "pv.w", -5100) + + +# ----------------------------------------------------------- battery + +def test_battery_discharging_is_negative(): + # GoodWe: +2000 W and +3.9 A while discharging (Table 8-9 mode 2). + out = poll(site()) + close(out, "battery.w", -2000) + close(out, "battery.a", -3.9) + close(out, "battery.v", 512.0) + close(out, "metric.battery_dc_v", 512.0) + close(out, "metric.battery_dc_a", -3.9) + + +def test_battery_charging_is_positive_and_uses_both_words(): + out = poll(site({35180: battery_block(amps=-140, watts=-70000, mode=3)})) + close(out, "battery.w", 70000) + close(out, "battery.a", 14.0) + + +def test_battery_soc_comes_from_the_bms_block(): + out = poll(site()) + close(out, "battery.soc", 0.76) # 37007, not 35182 + + +def test_no_battery_sends_no_battery(): + # 35184 = 0: "No Battery, or battery disconnected". + out = poll(site({35180: battery_block(volts_raw=0, amps=0, watts=0, mode=0)})) + assert out["EMITTED_battery"] == "false" + assert "metric.battery_dc_v" not in out + assert out["EMITTED_pv"] == "true" + + +def test_failed_battery_read_sends_no_battery(): + out = poll(site(), fail=(35180,)) + assert out["EMITTED_battery"] == "false" + assert "metric.battery_dc_v" not in out + assert "metric.battery_dc_a" not in out + + +def test_soc_left_out_without_a_talking_bms(): + out = poll(site({37000: bms_block(soc=0, protocol=0x11F)})) + assert "battery.soc" not in out + close(out, "battery.w", -2000) + + +@pytest.mark.parametrize("raw", [0xFFFF, 101]) +def test_soc_without_a_value_is_left_out(raw): + out = poll(site({37000: bms_block(soc=raw)})) + assert "battery.soc" not in out + close(out, "battery.w", -2000) + + +def test_failed_bms_read_leaves_soc_out(): + out = poll(site(), fail=(37000,)) + assert "battery.soc" not in out + close(out, "battery.w", -2000) + + +# ------------------------------------------------------------- meter + +def test_meter_export_is_negative(): + out = poll(site()) + close(out, "meter.w", -1200) # 36025, GoodWe +1200 = export + close(out, "meter.l1_w", -400) + close(out, "meter.l2_w", -500) + close(out, "meter.l3_w", -300) + close(out, "metric.meter_l1_w", -400) + + +def test_meter_import_is_positive_and_uses_both_words(): + out = poll(site({36004: meter_block(total=-70000, phases=(-40000, -20000, -10000))})) + close(out, "meter.w", 70000) + close(out, "meter.l1_w", 40000) + close(out, "meter.l3_w", 10000) + + +def test_meter_frequency_and_energy(): + out = poll(site()) + close(out, "meter.hz", 50.02) # 36014 + close(out, "meter.export_wh", 1234500.0) # 36015, float Wh + close(out, "meter.import_wh", 6789000.0) # 36017, float Wh + + +def test_meter_phase_voltage_and_current(): + out = poll(site()) + close(out, "meter.l1_v", 230.1) + close(out, "meter.l3_v", 230.3) + close(out, "meter.l1_a", 5.1) + close(out, "meter.l3_a", 7.3) + close(out, "metric.meter_l2_a", 6.2) + + +def test_meter_phase_block_missing_keeps_the_rest(): + out = poll(site(), fail=(36052,)) + assert "meter.l1_v" not in out + assert "meter.l1_a" not in out + close(out, "meter.w", -1200) + + +def test_arm_firmware_9_sends_no_meter(): + """ARM 9 and older keep meter power at 36005-36008, not 36019-36041. + + GoodWe's table says to use 36019-36041 only above ARM version 9 (35019). + On older firmware 36019+ would read 0 and look like a balanced site. + No source gives the sign of the old registers, so no meter is sent. + """ + out = poll(site({35019: [9]})) + assert out["EMITTED_meter"] == "false" + assert "metric.meter_l1_w" not in out + assert "36004" not in out["READS"].split(",") + assert out["EMITTED_pv"] == "true" + + +def test_arm_firmware_10_sends_the_meter(): + out = poll(site({35019: [10]})) + close(out, "meter.w", -1200) + + +def test_unknown_arm_firmware_sends_no_meter(): + out = poll(site(), fail=(35019,)) + assert out["EMITTED_meter"] == "false" + assert out["EMITTED_battery"] == "true" + + +def test_arm_firmware_is_read_once(): + out = poll(site(), polls=3) + assert out["READS"].split(",").count("35019") == 1 + close(out, "meter.w", -1200) + + +def test_arm_firmware_read_gives_up_after_three_tries(): + out = poll(site(), fail=(35019,), polls=6) + assert out["READS"].split(",").count("35019") == 3 + + +def test_no_meter_sends_no_meter(): + # 36004 = 0: the inverter does not hear its meter. + out = poll(site({36004: meter_block(comm=0, total=0, phases=(0, 0, 0))})) + assert out["EMITTED_meter"] == "false" + assert "metric.meter_l1_w" not in out + assert out["EMITTED_pv"] == "true" + assert out["EMITTED_battery"] == "true" + + +def test_failed_meter_read_sends_no_meter(): + out = poll(site(), fail=(36004,)) + assert out["EMITTED_meter"] == "false" + + +def test_nan_energy_is_left_out(): + out = poll(site({36004: meter_block(export_wh=NAN, import_wh=NAN)})) + assert "meter.export_wh" not in out + assert "meter.import_wh" not in out + close(out, "meter.w", -1200) + + +def test_no_read_of_the_old_wrong_registers(): + """35178 (BUS voltage) and 35113 alone are gone; 35132-35141 too.""" + script = f''' +package.path = "{HARNESS}/?.lua;" .. package.path +require("host_mock") +host.reset() +dofile("{DRIVER}") +driver_init({{}}) +pcall(driver_poll) +for _, c in ipairs(host._calls) do + if c.func == "modbus_read" then print(c.args[1] .. " " .. c.args[2]) end +end +''' + result = subprocess.run([str(LUA), "-e", script], + capture_output=True, text=True, cwd=ROOT) + assert result.returncode == 0, result.stdout + result.stderr + starts = {int(line.split()[0]) for line in result.stdout.split("\n") if line} + for old in (35178, 35179, 35182, 35113, 35109, 35140, 35132, 35121, 35195, 35199): + assert old not in starts, f"still reads {old}" diff --git a/index.yaml b/index.yaml index 54fef3cf..6672d7e3 100644 --- a/index.yaml +++ b/index.yaml @@ -299,15 +299,15 @@ drivers: size_bytes: 17091 sha256: "f289d0ba9fbb7a20e48ba26d0c93c44af541cb4cc3fb62576a6a4e1fb21fe002" - name: "goodwe" - version: "2.2.0" + version: "2.3.0" tier: community protocol: modbus connectivity: local setup: [bridge] ders: [pv, battery, meter] control: false - size_bytes: 14972 - sha256: "2bd0ddc5df2754bf8e807aea05529d59809ef65282f4a55a5823e481339be363" + size_bytes: 18014 + sha256: "2c38be160885144bf9baf1ab7f481de9f84bc38e22a5a9e9ba11991e4bdb1637" - name: "growatt" version: "2.2.2" tier: community diff --git a/manifests/goodwe.yaml b/manifests/goodwe.yaml index 6faca3c7..854b5bb0 100644 --- a/manifests/goodwe.yaml +++ b/manifests/goodwe.yaml @@ -1,5 +1,5 @@ name: "goodwe" -version: "2.2.0" +version: "2.3.0" tier: community author: "frekes81 and Sourceful contributors" protocol: modbus @@ -17,13 +17,17 @@ upstream_docs: title: "goodwe library ET/EH register map" kind: register_map url_stability: volatile + - url: "https://raw.githubusercontent.com/evcc-io/evcc/master/templates/definition/meter/goodwe-hybrid.yaml" + title: "evcc goodwe-hybrid template (ET/EH/BH/BT register use and signs)" + kind: register_map + url_stability: volatile min_host_version: "1.10.0-beta.1" -size_bytes: 14972 +size_bytes: 18014 dkb_id: "" -sha256: "2bd0ddc5df2754bf8e807aea05529d59809ef65282f4a55a5823e481339be363" +sha256: "2c38be160885144bf9baf1ab7f481de9f84bc38e22a5a9e9ba11991e4bdb1637" signature: "" bytecode_sha256: "" bytecode_signature: "" bytecode_size: 0 -changelog: "Report BMS pack temperature, charge and discharge limits and cell temperatures from GoodWe's 37000 block; drop the invented 0 C." +changelog: "Read PV, battery and meter from the registers in GoodWe's own map (srcfl/device-drivers#264); send no battery when 35184 says none and no meter when the inverter does not hear one." diff --git a/support-status.json b/support-status.json index dbeae8b9..b6a7eb8a 100644 --- a/support-status.json +++ b/support-status.json @@ -674,7 +674,7 @@ }, { "catalog_source": true, - "catalog_version": "2.2.0", + "catalog_version": "2.3.0", "driver_id": "goodwe", "targets": { "blixt-l1": {