diff --git a/CHANGELOG.md b/CHANGELOG.md index 750082e..ba3afd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,44 @@ ## [Unreleased] +### alphaess 1.3.0, kstar 1.3.0 + +Both drivers read the registers their vendor's own Modbus map gives +(srcfl/device-drivers#257). Earlier releases read addresses no source +supports, so their PV, battery and meter values were probably wrong. Each +stream now goes out only when its read answered, never as zeros. Written +from the sources in each manifest's `upstream_docs`. Not yet checked on +hardware. + +- **alphaess 1.3.0** follows AlphaESS's parameter address table. Grid meter: + total power at 0x0021 (S32, positive when drawing from the grid), phase A + current at 0x0017, frequency at 0x001A. Battery: power at 0x0126, which + AlphaESS counts positive when discharging, so the driver negates it; + voltage at 0x0100 and SoC at 0x0102 (0.1 %). PV: the sum of the six string + powers at 0x041F-0x0433, with string 1 voltage and current. Earlier + releases read meter energy counters and voltages at 0x0010-0x0028 as PV, + battery and meter values. The grid phase voltage is no longer sent: the + sources disagree on its scale. 0x0111/0x0112 are still not used as live + BMS limits. The meter and PV strings 4 to 6 are read only once 0x074E + (Protocol Version, in the System info block V1.20 added) answers; firmware + on the 2020 map (V1.17, meter at 0x0000-0x0019) gets PV strings 1 to 3 + and no meter. +- **kstar 1.3.0** reads KSTAR's input registers (function 04H) from 3000, as + KSTAR's Hybrid Inverter Modbus V2.9.1 gives them for the KSE, BluE-S and + E-series three-phase models: PV at 3000-3025, battery voltage, power and + SoC at 3063-3066 (beside the temperatures it already read) and the grid + meter at 3097-3108. KSTAR gives no signs; following ha-solarman, the + meter power (positive when exporting) and the battery power (positive + when discharging) are negated. The model at 3045 says whether the S and + T phases exist; a code KSTAR's model table does not name sends no meter. + Earlier releases read holding registers 10-54, which no source has. + +In both drivers, battery voltage and SoC both 0 means no pack is connected, +so the battery stream is left out rather than sent as an empty pack. +Neither vendor map has a flag that says a grid meter is fitted, so with no +meter both drivers send whatever the meter registers hold: 0 W there cannot +be told from a balanced site. + ### fronius 2.3.0 Battery power, voltage and SoC now come from the registers Fronius's GEN24 diff --git a/SUPPORT_STATUS.md b/SUPPORT_STATUS.md index d020626..13ea60f 100644 --- a/SUPPORT_STATUS.md +++ b/SUPPORT_STATUS.md @@ -20,8 +20,8 @@ Catalog source is not proof that a target can install or run a driver. | acuvim | 0.4.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | alfen | 1.1.1 | ftw-core | not_assessed | — | not_recorded | not_assessed | | alfen | 1.1.1 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | -| alphaess | 1.2.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | -| alphaess | 1.2.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | +| alphaess | 1.3.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | +| alphaess | 1.3.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | ambibox | 1.2.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | | ambibox | 1.2.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | ambibox_v2x | 1.1.1 | ftw-core | not_assessed | — | not_recorded | not_assessed | @@ -90,8 +90,8 @@ Catalog source is not proof that a target can install or run a driver. | konja-261-svk | 0.4.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | kostal | 2.2.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | | kostal | 2.2.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | -| kstar | 1.2.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | -| kstar | 1.2.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | +| kstar | 1.3.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | +| kstar | 1.3.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | mennekes | 1.1.1 | ftw-core | not_assessed | — | not_recorded | not_assessed | | mennekes | 1.1.1 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | myuplink | 1.2.4 | ftw-core | not_assessed | — | not_recorded | not_assessed | diff --git a/devices.yaml b/devices.yaml index 75cee1c..1ffad57 100644 --- a/devices.yaml +++ b/devices.yaml @@ -135,7 +135,7 @@ manufacturers: protocols: - protocol: modbus driver: "alphaess" - version: "1.2.0" + version: "1.3.0" ders: [pv, battery, meter] control: false firmware_versions: "" @@ -146,7 +146,7 @@ manufacturers: protocols: - protocol: modbus driver: "alphaess" - version: "1.2.0" + version: "1.3.0" ders: [pv, battery, meter] control: false firmware_versions: "" @@ -761,7 +761,7 @@ manufacturers: protocols: - protocol: modbus driver: "kstar" - version: "1.2.0" + version: "1.3.0" ders: [pv, battery, meter] control: false firmware_versions: "" @@ -772,7 +772,7 @@ manufacturers: protocols: - protocol: modbus driver: "kstar" - version: "1.2.0" + version: "1.3.0" ders: [pv, battery, meter] control: false firmware_versions: "" diff --git a/drivers/lua/alphaess.lua b/drivers/lua/alphaess.lua index 47931f4..7e9dfc5 100644 --- a/drivers/lua/alphaess.lua +++ b/drivers/lua/alphaess.lua @@ -3,8 +3,23 @@ -- Register type: HOLDING (FC 0x03) -- Port: 502 -- Community tier (untested) --- Register map from AlphaESS Modbus protocol v1.30 --- Hex addresses converted to decimal +-- +-- Register map: AlphaESS "Modbus_RTU/TCP Protocol" and its "Parameter +-- address table" (protocol history up to V1.39, 2024-11-29), as published +-- in ramonvanraaij/ha-alphaess-modbus docs/. The vendor's "Household +-- Register Map" table gives the same meter (0x0010-0x0036), battery +-- (0x0100-0x0148) and inverter (0x0400-0x0452) blocks for SMILE-SP, +-- SMILE-G3 (S3.6, S5, B5, S6, S8, T4-T20), SMILE-S6-HV, SMILE-T10-HV and +-- B3/B3PLUS. evcc's alpha-ess-smile template, dxoverdy/Alpha2MQTT and +-- senalse/ha-alphaess-modbus read the same addresses; they were used for +-- addresses, types and signs only. +-- +-- Older firmware: protocol V1.17 (2020) put the grid meter at +-- 0x0000-0x0019 (total power at 0x0006) and the inverter temperature at +-- 0x0429, where V1.39 has PV4. V1.19 (2020-10-20) changed the meter table +-- and V1.20 (2020-10-25) added the System info block, 0x0740-0x074E. The +-- driver uses the current meter and PV strings 4 to 6 only once 0x074E +-- answers (see detect_map below). -- No control code has been written for this inverter yet: driver_command -- only refuses. It says read_only so FTW and the catalog tell the owner @@ -15,7 +30,7 @@ DRIVER = { id = "alphaess", name = "AlphaESS inverter", manufacturer = "AlphaESS", - version = "1.2.0", + version = "1.3.0", protocols = { "modbus" }, capabilities = { "pv", "battery", "meter" }, read_only = true, @@ -31,13 +46,21 @@ PROTOCOL = "modbus" -- marked offline. Give a register three chances, then leave it alone until -- restart. Three rather than one: a single failure is not proof, the link may -- just have been slow. +-- +-- A short reply counts as a failure: every register asked for must be there, +-- or a decode further down would do arithmetic on nil. local GIVE_UP_AFTER = 3 local read_failures = {} local function probe_read(addr, count, kind) if (read_failures[addr] or 0) >= GIVE_UP_AFTER then return nil end local ok, regs = pcall(host.modbus_read, addr, count, kind) - if ok and regs and regs[1] ~= nil then + if ok and regs then + for i = 1, count do + if regs[i] == nil then ok = false break end + end + end + if ok and regs then read_failures[addr] = nil return regs end @@ -51,133 +74,145 @@ local function probe_read(addr, count, kind) return nil end +-- A cell temperature outside -40..100 C is a sentinel or a misread. +local function plausible_temp(c) + if c < -40 or c > 100 then return nil end + return c +end + +-- Which map the firmware speaks. 0x074E "Protocol Version" is in the +-- System info block that V1.20 added; the vendor's model table lists it for +-- every model. Its value is not used: Alpha2MQTT's register dump from a +-- real system shows it as 0. So the gate is whether it answers at all. +-- nil while asking; "current" once it answered; "old" after it failed +-- GIVE_UP_AFTER times. A firmware between V1.20 and V1.22, which changed the +-- inverter block again, cannot be told apart from a later one. +local map_version = nil + +local function detect_map() + if map_version ~= nil then return map_version end + if probe_read(0x074E, 1, "holding") then + map_version = "current" + elseif (read_failures[0x074E] or 0) >= GIVE_UP_AFTER then + map_version = "old" + host.log("warn", "AlphaESS: register 0x074E does not answer, so this " .. + "firmware predates protocol V1.20; grid meter and PV strings 4-6 " .. + "left out") + end + return map_version +end + function driver_init(config) host.set_make("AlphaESS") end function driver_poll() - -- ---- PV ---- - - -- PV1 voltage: 0x0010=16, U16 × 0.1V - -- PV1 current: 0x0011=17, U16 × 0.1A - local pv1_regs = probe_read(16, 2, "holding") - local mppt1_v, mppt1_a = 0, 0 - if pv1_regs then - mppt1_v = pv1_regs[1] * 0.1 - mppt1_a = pv1_regs[2] * 0.1 - end + local map = detect_map() - -- PV total power: 0x0012=18, U16, W - local pvw_regs = probe_read(18, 1, "holding") - local pv_w = 0 - if pvw_regs then - pv_w = pvw_regs[1] - end - - -- Grid frequency: 0x0022=34, U16 × 0.01Hz - local hz_regs = probe_read(34, 1, "holding") - local hz = 0 - if hz_regs then - hz = hz_regs[1] * 0.01 + -- ---- PV ---- + -- + -- 0x041D-0x0434 in one read: per string voltage (U16 x 0.1 V), current + -- (U16 x 0.1 A) and power (U32, 1 W), strings 1 to 6, four registers + -- apart. PV power is their sum. Strings 1 to 3 (0x041D-0x0428) sit in + -- the same place in V1.17; until the current map is confirmed only they + -- are read. 0x0453 "Pv total power" is newer than the 0x0400-0x0452 + -- block the vendor lists for every model, so it is not used. A PV meter + -- for an AC-coupled inverter (0x00A1) is not added: that inverter may + -- have its own driver. + local pv_count = (map == "current") and 24 or 12 + local pv_regs = probe_read(0x041D, pv_count, "holding") + if pv_regs then + local pv_w = 0 + for i = 3, pv_count - 1, 4 do + pv_w = pv_w + host.decode_u32_be(pv_regs[i], pv_regs[i + 1]) + end + host.emit("pv", { + W = -pv_w, -- generation is negative (site convention) + mppt1_v = pv_regs[1] * 0.1, + mppt1_a = pv_regs[2] * 0.1, + }) end - -- Emit PV telemetry (W always negative for generation) - host.emit("pv", { - W = -pv_w, - mppt1_v = mppt1_v, - mppt1_a = mppt1_a, - }) - -- ---- Battery ---- - - -- Battery power: 0x0020=32, I16, W (positive=charge, negative=discharge) - local bw_regs = probe_read(32, 1, "holding") - local bat_w = 0 - if bw_regs then - bat_w = host.decode_i16(bw_regs[1]) - end - - -- Battery voltage: 0x0021=33, U16 × 0.1V - local bv_regs = probe_read(33, 1, "holding") - local bat_v = 0 - if bv_regs then - bat_v = bv_regs[1] * 0.1 - end - - -- Battery SoC: 0x0024=36, U16, % - local bsoc_regs = probe_read(36, 1, "holding") - local bat_soc = 0 - if bsoc_regs then - bat_soc = bsoc_regs[1] / 100 -- percent to fraction + -- + -- 0x0100-0x0126 in one read (the "Household Battery" block): + -- 0x0100 battery voltage U16 x 0.1 V + -- 0x0102 battery SOC U16 x 0.1 % + -- 0x010D min cell temperature S16 x 0.1 C + -- 0x0110 max cell temperature S16 x 0.1 C + -- 0x0126 battery power S16, 1 W, "-: Charge, +: Discharge" + -- AlphaESS counts discharge as positive, so the power is negated here + -- (site convention: charging is positive). evcc reads it the same way. + -- senalse's map describes it as positive when charging; the vendor's + -- table, evcc and Alpha2MQTT all say the opposite. + -- + -- The block has no pack temperature, so temperature_C is left out. + -- 0x0111/0x0112 "Battery max charge/discharge current" (U16 x 0.1 A) + -- are not used as live limits: the vendor does not say they move with + -- the BMS, and Alpha2MQTT's author saw the charge value stay at 0 and + -- the discharge value at 100 A on a 50 A pack. A 0 would tell Core the + -- battery may not charge. + -- + -- Voltage and SoC both 0 means no pack is connected, not an empty one + -- (docs/WRITING-A-DRIVER.md, "A hybrid inverter may have no battery"): + -- the battery stream and its metrics are left out. + local bat_regs = probe_read(0x0100, 39, "holding") + if bat_regs and bat_regs[1] == 0 and bat_regs[3] == 0 then + bat_regs = nil end + if bat_regs then + local battery = { + W = -host.decode_i16(bat_regs[39]), -- 0x0126 + V = bat_regs[1] * 0.1, -- 0x0100 + } + local soc_raw = bat_regs[3] -- 0x0102 + if soc_raw <= 1000 then + battery.SoC_nom_fract = soc_raw / 1000 -- 0.1 % per bit + end + host.emit("battery", battery) - -- Emit Battery telemetry - host.emit("battery", { - W = bat_w, - V = bat_v, - SoC_nom_fract = bat_soc, - }) - - -- Cell temperature range, for history: 0x010D=269 lowest cell, 0x0110=272 - -- highest cell, I16 x 0.1 C, in the battery block that starts at 0x0100. - -- Sources, for addresses and scales only: dxoverdy/Alpha2MQTT - -- Definitions.h and senalse/ha-alphaess-modbus docs/register_map.md. - -- Alpha2MQTT reads them as signed (Short); senalse lists uint16 x 0.1. - -- The driver decodes i16, which gives the same value for every - -- temperature at or above 0 C. A sub-zero cell read as uint16 would come - -- out near 6500 C and be dropped by the range check. - -- Both place battery power at 0x0126 and the grid meter at 0x0010..0x0021, - -- which is not what the reads above assume; that map needs its own - -- check against hardware. The block holds no pack temperature, so - -- `temperature_C` is left out. 0x0111/0x0112 (BMS max charge and - -- discharge current) are not read: Alpha2MQTT's author saw the charge - -- value stay at 0 and the discharge value at twice the pack's rating, - -- so they are not trusted as live limits. senalse describes 0x0111 as - -- the BMS's present maximum charge current (x 0.1 A); the two sources - -- disagree, and hardware has to settle it before Core relies on it. - local cell_regs = probe_read(269, 4, "holding") - if cell_regs then - local low_c = host.decode_i16(cell_regs[1]) * 0.1 - local high_c = host.decode_i16(cell_regs[4]) * 0.1 - if low_c >= -40 and low_c <= 100 then + local low_c = plausible_temp(host.decode_i16(bat_regs[14]) * 0.1) -- 0x010D + local high_c = plausible_temp(host.decode_i16(bat_regs[17]) * 0.1) -- 0x0110 + if low_c ~= nil then host.emit_metric("battery_cell_temp_min_c", low_c) end - if high_c >= -40 and high_c <= 100 then + if high_c ~= nil then host.emit_metric("battery_cell_temp_max_c", high_c) end end -- ---- Meter ---- - - -- Phase voltage: 0x0026=38, U16 × 0.1V - local lv_regs = probe_read(38, 1, "holding") - local l1_v = 0 - if lv_regs then - l1_v = lv_regs[1] * 0.1 - end - - -- Phase current: 0x0027=39, U16 × 0.1A - local la_regs = probe_read(39, 1, "holding") - local l1_a = 0 - if la_regs then - l1_a = la_regs[1] * 0.1 + -- + -- 0x0017-0x0022 in one read (the grid meter's running data): + -- 0x0017 current of A phase S16 x 0.1 A + -- 0x001A grid frequency U16 x 0.01 Hz + -- 0x0021 total active power S32, 1 W + -- Total power is positive when drawing from the grid and negative when + -- feeding it (Alpha2MQTT: "Minus = feeding, Positive = drawing"; evcc + -- and senalse read it the same way), which is the site convention, so it + -- is passed through. The vendor's table gives no sign. + -- + -- Phase voltage (0x0014) is left out: the vendor says 1 V per bit, + -- Alpha2MQTT's author read 2421 for 242.1 V, and the sources do not + -- agree on the scale. + -- + -- The map has no flag that says a meter is fitted. 0x0000 "Grid Meter + -- CT Enable" read 0 on a working system in Alpha2MQTT's dump, and + -- 0x0806 "Meter CT Select" only says CT or meter. So with no meter the + -- driver sends whatever these registers hold, and 0 W cannot be told + -- from a balanced site. Read only on the current map (see detect_map). + local m_regs = nil + if map == "current" then + m_regs = probe_read(0x0017, 12, "holding") end - - -- Grid power: 0x0028=40, I16, W (positive=import) - local mw_regs = probe_read(40, 1, "holding") - local meter_w = 0 - if mw_regs then - meter_w = host.decode_i16(mw_regs[1]) + if m_regs then + host.emit("meter", { + W = host.decode_i32_be(m_regs[11], m_regs[12]), -- 0x0021 + L1_A = host.decode_i16(m_regs[1]) * 0.1, -- 0x0017 + Hz = m_regs[4] * 0.01, -- 0x001A + }) end - -- Emit Meter telemetry - host.emit("meter", { - W = meter_w, - L1_V = l1_v, - L1_A = l1_a, - Hz = hz, - }) - return 5000 end diff --git a/drivers/lua/kstar.lua b/drivers/lua/kstar.lua index e63fb42..83485ba 100644 --- a/drivers/lua/kstar.lua +++ b/drivers/lua/kstar.lua @@ -1,8 +1,18 @@ -- KSTAR KSE Series Inverter Driver -- Emits: PV, Battery, Meter --- Register type: HOLDING (FC 0x03) +-- Register type: INPUT (FC 0x04) -- Port: 502 -- Community tier (untested) +-- +-- Register map: KSTAR "Hybrid Inverter MODBUS RS485 Communication Protocol" +-- V2.9.1 (2024-07-21, document SFT-KSE-001), table 3.1 "Inverter basic +-- information", read-only input registers from 3000. Its model table +-- (3.1.5) covers the KSE single-phase models, the BluE-S 3680D/5000D/6000D +-- (and M1) and the E8KT/E10KT/E12KT three-phase models. Signs, which the +-- vendor does not state, are taken from davidrapan/ha-solarman +-- kstar_hybrid.yaml; addresses and scales agree with it and with +-- StephanJoubert/home_assistant_solarman kstar_hybrid.yaml (written from +-- V2.5). No code was copied. -- No control code has been written for this inverter yet: driver_command -- only refuses. It says read_only so FTW and the catalog tell the owner @@ -13,7 +23,7 @@ DRIVER = { id = "kstar", name = "KSTAR inverter", manufacturer = "KSTAR", - version = "1.2.0", + version = "1.3.0", protocols = { "modbus" }, capabilities = { "pv", "battery", "meter" }, read_only = true, @@ -28,13 +38,21 @@ PROTOCOL = "modbus" -- nothing at all, which is worse than reporting one field less. So stop asking -- once a register has proved it is not there. Three tries, because one failure -- proves nothing -- the link may just have been slow. +-- +-- A short reply counts as a failure: every register asked for must be there, +-- or a decode further down would do arithmetic on nil. local GIVE_UP_AFTER = 3 local read_failures = {} local function probe_read(addr, count, kind) if (read_failures[addr] or 0) >= GIVE_UP_AFTER then return nil end local ok, regs = pcall(host.modbus_read, addr, count, kind) - if ok and regs and regs[1] ~= nil then + if ok and regs then + for i = 1, count do + if regs[i] == nil then ok = false break end + end + end + if ok and regs then read_failures[addr] = nil return regs end @@ -48,121 +66,164 @@ local function probe_read(addr, count, kind) return nil end -function driver_init(config) - host.set_make("KSTAR") +local function plausible_temp(c) + if c < -40 or c > 100 then return nil end + return c end -function driver_poll() - -- ---- PV ---- +-- Inverter model, input 3045, KSTAR table 3.1.5. Only the codes the table +-- names are used; the rest are marked "reserve", and a reserved code gets no +-- meter rather than a guess. The S and T phase meter registers (3101-3108) +-- are "only for three phase model". The model does not change, so it is read +-- until it answers once. +local MODEL_PHASES = { + [0] = "single", -- KSE-2K-048S + [1] = "single", -- KSE-3K-048S + [2] = "single", -- KSE-3.6K-048S + [3] = "single", -- KSE-4.6K-048S + [4] = "single", -- KSE-5K-048S + [5] = "single", -- KSE-3.6K-048 + [6] = "single", -- KSE-4.6K-048 + [7] = "single", -- KSE-5K-048 + [8] = "single", -- KSE-6K-048 + [9] = "single", -- BluE-S 3680D + [11] = "single", -- BluE-S 5000D + [12] = "single", -- BluE-S 6000D + [14] = "single", -- KSE-3K-048S M1 + [15] = "single", -- BluE-S 3680D M1 + [17] = "single", -- BluE-S 5000D M1 + [18] = "single", -- BluE-S 6000D M1 + [32] = "three", -- E10KT + [33] = "three", -- E8KT + [34] = "three", -- E12KT +} - -- PV1 voltage: 28, U16 × 0.1V; PV1 current: 29, U16 × 0.1A - local pv1_regs = probe_read(28, 2, "holding") - local mppt1_v, mppt1_a = 0, 0 - if pv1_regs then - mppt1_v = pv1_regs[1] * 0.1 - mppt1_a = pv1_regs[2] * 0.1 +-- nil until known; "single", "three", or "unknown" for a code the table +-- does not name. +local phases = nil + +local function detect_phases() + if phases ~= nil then return phases end + local regs = probe_read(3045, 1, "input") + if not regs then return nil end + phases = MODEL_PHASES[regs[1]] or "unknown" + if phases == "unknown" then + host.log("warn", string.format( + "KSTAR: inverter model code %d is not named in KSTAR's model " .. + "table; grid meter left out", regs[1])) end + return phases +end - -- PV power: 30, U16, W - local pvw_regs = probe_read(30, 1, "holding") - local pv_w = 0 - if pvw_regs then - pv_w = pvw_regs[1] - end +function driver_init(config) + host.set_make("KSTAR") +end - -- Grid frequency: 18, U16 × 0.01Hz - local hz_regs = probe_read(18, 1, "holding") - local hz = 0 - if hz_regs then - hz = hz_regs[1] * 0.01 +function driver_poll() + -- ---- PV ---- + -- + -- 3000-3025 in one read: + -- 3000 PV1 input voltage U16 x 0.1 V + -- 3012 PV1 input current S16 x 0.01 A + -- 3024 PV1 input power S16, 1 W + -- 3025 PV2 input power S16, 1 W + local pv_regs = probe_read(3000, 26, "input") + if pv_regs then + local pv_w = host.decode_i16(pv_regs[25]) + host.decode_i16(pv_regs[26]) + host.emit("pv", { + W = -pv_w, -- generation is negative (site convention) + mppt1_v = pv_regs[1] * 0.1, + mppt1_a = host.decode_i16(pv_regs[13]) * 0.01, + }) end - -- Emit PV telemetry (W always negative for generation) - host.emit("pv", { - W = -pv_w, - mppt1_v = mppt1_v, - mppt1_a = mppt1_a, - }) - -- ---- Battery ---- - - -- Battery power: 50, I16, W (positive=charge, negative=discharge) - local bw_regs = probe_read(50, 1, "holding") - local bat_w = 0 - if bw_regs then - bat_w = host.decode_i16(bw_regs[1]) + -- + -- 3055-3067 in one read: + -- 3055 inverter radiator temperature S16 x 0.1 C + -- 3063 battery voltage U16 x 0.01 V + -- 3065 battery charge/discharge power S16, 1 W + -- 3066 battery SOC U16 x 0.1 % + -- 3067 battery temperature S16 x 0.1 C + -- 3056 is the battery converter's heatsink, not the battery, and is not + -- used. KSTAR does not give the sign of 3065. ha-solarman's KSTAR + -- profile counts it as power into the DC bus (its "Power losses" sensor + -- is PV + battery - inverter output), so positive is discharging, and + -- the driver negates it (site convention: charging is positive). + -- + -- 3070/3071 BAT_CHG_LIMIT_CURR / BAT_DISCHG_LIMIT_CURR (1 A) are not + -- sent as available power: the map does not say whether they come from + -- the BMS or move with it. 3078/3079 are percent settings. + -- + -- Voltage and SoC both 0 means no pack is connected, not an empty one + -- (docs/WRITING-A-DRIVER.md, "A hybrid inverter may have no battery"): + -- the battery stream and the battery temperature are left out. The + -- inverter temperature in the same read still goes out. + local bat_regs = probe_read(3055, 13, "input") + if bat_regs then + local inverter_c = plausible_temp(host.decode_i16(bat_regs[1]) * 0.1) -- 3055 + if inverter_c ~= nil then + host.emit_metric("inverter_temp_c", inverter_c) + end + if bat_regs[9] == 0 and bat_regs[12] == 0 then -- 3063, 3066 + bat_regs = nil + end end - - -- Battery SoC: 54, U16, % - local bsoc_regs = probe_read(54, 1, "holding") - local bat_soc = 0 - if bsoc_regs then - bat_soc = bsoc_regs[1] / 100 -- percent to fraction - end - - -- Temperatures: INPUT registers 3055..3067 in one read. 3055 is the - -- inverter radiator, 3067 the battery; both I16 x 0.1 C. Source, for - -- addresses and scales only: davidrapan/ha-solarman - -- inverter_definitions/kstar_hybrid.yaml, written from KSTAR's Hybrid - -- Inverter Modbus V2.9.1 (2024-07-21). That map puts every reading in - -- input registers 3000 and up, not in the holding registers read above; - -- those need their own check against hardware. 3056 is the battery - -- converter's heatsink, not the battery, and is not used. The map has - -- no live BMS charge or discharge limit: 3078/3079 are percent - -- settings. - local temp_regs = probe_read(3055, 13, "input") - local bat_temp_c, inverter_c = nil, nil - if temp_regs then - local c = host.decode_i16(temp_regs[13]) * 0.1 - if c >= -40 and c <= 100 then bat_temp_c = c end - c = host.decode_i16(temp_regs[1]) * 0.1 - if c >= -40 and c <= 100 then inverter_c = c end - end - - -- Emit Battery telemetry - host.emit("battery", { - W = bat_w, - SoC_nom_fract = bat_soc, - temperature_C = bat_temp_c, - }) - if bat_temp_c ~= nil then - host.emit_metric("battery_temp_c", bat_temp_c) - end - if inverter_c ~= nil then - host.emit_metric("inverter_temp_c", inverter_c) + if bat_regs then + local battery = { + W = -host.decode_i16(bat_regs[11]), -- 3065 + V = bat_regs[9] * 0.01, -- 3063 + } + local soc_raw = bat_regs[12] -- 3066 + if soc_raw <= 1000 then + battery.SoC_nom_fract = soc_raw / 1000 + end + local bat_temp_c = plausible_temp(host.decode_i16(bat_regs[13]) * 0.1) -- 3067 + battery.temperature_C = bat_temp_c + host.emit("battery", battery) + if bat_temp_c ~= nil then + host.emit_metric("battery_temp_c", bat_temp_c) + end end -- ---- Meter ---- - - -- Grid power: 22, I16, W (positive=import) - local mw_regs = probe_read(22, 1, "holding") - local meter_w = 0 - if mw_regs then - meter_w = host.decode_i16(mw_regs[1]) - end - - -- Phase voltage: 10, U16 × 0.1V - local lv_regs = probe_read(10, 1, "holding") - local l1_v = 0 - if lv_regs then - l1_v = lv_regs[1] * 0.1 + -- + -- 3097-3108, four registers per phase (R, S, T): + -- 3097 grid voltage U16 x 0.1 V + -- 3098 grid frequency U16 x 0.01 Hz + -- 3099 meter current S16 x 0.001 A + -- 3100 meter power S16, 1 W + -- KSTAR does not give the sign of the meter power. ha-solarman negates + -- it "to match standard behavior of the other inverters" (import + -- positive), and an owner there reported the raw value negative while + -- importing. So the raw value is positive when exporting, and the + -- driver negates it. The current is sent as read; its sign is not + -- documented. + -- + -- The map has no flag that says a meter or CT is fitted (3264 "CT Or + -- Meter" only says which, and the alarm table has no meter fault). So + -- with none fitted the driver sends whatever these registers hold, and + -- 0 W cannot be told from a balanced site. + local p = detect_phases() + local m_regs = nil + if p == "single" then + m_regs = probe_read(3097, 4, "input") + elseif p == "three" then + m_regs = probe_read(3097, 12, "input") end - - -- Phase current: 11, U16 × 0.1A - local la_regs = probe_read(11, 1, "holding") - local l1_a = 0 - if la_regs then - l1_a = la_regs[1] * 0.1 + if m_regs then + local w = host.decode_i16(m_regs[4]) + if p == "three" then + w = w + host.decode_i16(m_regs[8]) + host.decode_i16(m_regs[12]) + end + host.emit("meter", { + W = -w, + L1_V = m_regs[1] * 0.1, + L1_A = host.decode_i16(m_regs[3]) * 0.001, + Hz = m_regs[2] * 0.01, + }) end - -- Emit Meter telemetry - host.emit("meter", { - W = meter_w, - L1_V = l1_v, - L1_A = l1_a, - Hz = hz, - }) - return 5000 end diff --git a/drivers/tests/invented-number-baseline.json b/drivers/tests/invented-number-baseline.json index 935e481..6ecec62 100644 --- a/drivers/tests/invented-number-baseline.json +++ b/drivers/tests/invented-number-baseline.json @@ -14,7 +14,6 @@ "abb_meter": 13, "acrel": 13, "acuvim": 8, - "alphaess": 10, "atmoce": 13, "carlo_gavazzi": 13, "chint": 13, @@ -26,7 +25,6 @@ "hello": 3, "janitza": 13, "kostal": 21, - "kstar": 9, "schneider_meter": 13, "siemens_pac": 13, "sigenergy": 5, diff --git a/drivers/tests/lua_harness/test_all_drivers.lua b/drivers/tests/lua_harness/test_all_drivers.lua index d1a2e1f..9e0322a 100644 --- a/drivers/tests/lua_harness/test_all_drivers.lua +++ b/drivers/tests/lua_harness/test_all_drivers.lua @@ -147,6 +147,9 @@ local function setup_modbus_data(driver_name) -- the driver reads the full hybrid map. String models report another code. input[4999] = 0x0E0E holding[4999] = 0x0E0E + -- KSTAR inverter model, input 3045: 0x00 is the single-phase KSE-2K, + -- so the driver knows which meter phases the device has. + input[3045] = 0x00 -- Sungrow status reg 13000 = 0 (not discharging) input[13000] = 0 holding[13000] = 0 @@ -186,6 +189,8 @@ local function setup_modbus_data(driver_name) -- *0.1/100 pattern 13022, -- Sungrow 37004, -- Huawei + 3066, -- KSTAR (input) + 258, -- AlphaESS (0x0102) } for _, a in ipairs(soc_50_addrs) do holding[a] = 50 @@ -296,8 +301,6 @@ local function setup_modbus_data(driver_name) 28, -- SolaX 33139, -- Solis 11038, -- FoxESS - 54, -- KSTAR - 36, -- AlphaESS 4248, -- unused by current SAJ map; harmless 1014, -- Growatt } diff --git a/drivers/tests/test_alphaess_battery_now.py b/drivers/tests/test_alphaess_battery_now.py index d5a55a4..180555e 100644 --- a/drivers/tests/test_alphaess_battery_now.py +++ b/drivers/tests/test_alphaess_battery_now.py @@ -4,8 +4,9 @@ highest (0x0110) cell temperature, I16 x 0.1 °C. They go to history as battery_cell_temp_min_c and battery_cell_temp_max_c. The block has no pack temperature, so `temperature_C` stays out of the battery stream, and the BMS -current registers 0x0111/0x0112 are not read: one integration's author saw -them stay at values that cannot be live limits. +current registers 0x0111/0x0112 are not used: one integration's author saw +them stay at values that cannot be live limits. The driver reads the block +0x0100-0x0126 in one go, so a failed read leaves all of it out. """ from __future__ import annotations @@ -80,23 +81,24 @@ def num(out: dict[str, str], key: str) -> float: def test_alphaess_reports_cell_temperature_range(): - out = run({269: i16(-50), 272: 312}) + out = run({0x0100: 5120, 269: i16(-50), 272: 312}) assert num(out, "metric.battery_cell_temp_min_c") == pytest.approx(-5.0, abs=1e-3) assert num(out, "metric.battery_cell_temp_max_c") == pytest.approx(31.2, abs=1e-3) assert "battery.temperature_C" not in out assert "battery.temp_c" not in out assert "battery.available_charge_W" not in out - assert "reads.holding:273" not in out - assert "reads.holding:274" not in out + assert "battery.available_discharge_W" not in out + assert "metric.battery_charge_limit_a" not in out def test_alphaess_failed_cell_temperature_read_is_left_out(): - out = run({269: i16(-50), 272: 312}, fail=(269,)) + out = run({0x0100: 5120, 269: i16(-50), 272: 312}, fail=(0x0100,)) assert "metric.battery_cell_temp_min_c" not in out assert "metric.battery_cell_temp_max_c" not in out + assert not any(k.startswith("battery.") for k in out) def test_alphaess_out_of_range_cell_temperature_is_dropped(): - out = run({269: i16(-500), 272: 1500}) + out = run({0x0100: 5120, 269: i16(-500), 272: 1500}) assert "metric.battery_cell_temp_min_c" not in out assert "metric.battery_cell_temp_max_c" not in out diff --git a/drivers/tests/test_alphaess_map.py b/drivers/tests/test_alphaess_map.py new file mode 100644 index 0000000..1a1dfe1 --- /dev/null +++ b/drivers/tests/test_alphaess_map.py @@ -0,0 +1,217 @@ +"""AlphaESS reads the registers its own Modbus map gives, with the site's signs. + +AlphaESS's "Parameter address table" (protocol history up to V1.39) puts: + +- the grid meter's current of A phase at 0x0017 (S16 x 0.1 A), frequency at + 0x001A (U16 x 0.01 Hz) and total active power at 0x0021 (S32, 1 W), + positive when drawing from the grid (Alpha2MQTT, evcc, senalse); +- battery voltage at 0x0100 (x 0.1 V), SoC at 0x0102 (x 0.1 %) and battery + power at 0x0126 (S16, 1 W, "-: Charge, +: Discharge"); +- PV string voltage, current and power (U32) for strings 1 to 6 at + 0x041D-0x0434, four registers apart. + +Earlier releases read 0x0010-0x0028 as PV, battery and meter. In the +vendor's map those are grid meter energy counters, voltages and powers. +Each stream is sent only when its read answered, never as zeros. + +AlphaESS's protocol V1.17 (2020) had the meter at 0x0000-0x0019 and only +three PV strings. V1.19 changed the meter table and V1.20 added the System +info block (0x0740-0x074E). The driver uses the current meter and strings +4 to 6 only when 0x074E (Protocol Version) answers. Its value is not used: +it read 0 on a real system (Alpha2MQTT's register dump). +""" + +from __future__ import annotations + +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" / "alphaess.lua" + +pytestmark = pytest.mark.skipif( + not LUA.exists(), reason="run make check to build ./lua55") + +REPORT = ''' +local function dump(prefix, t) + if not t then return end + for k, v in pairs(t) do + if type(v) ~= "table" then print(prefix .. k .. " " .. tostring(v)) end + end +end +local function last(t) return t and t[#t] or nil end +for _, der in ipairs({"pv", "battery", "meter"}) do + dump(der .. ".", last(host._emitted[der])) +end +for name, m in pairs(host._metrics) do + print("metric." .. name .. " " .. tostring(m.value)) +end +local calls = {} +for _, c in ipairs(host._calls) do + if c.func == "modbus_read" then + local key = tostring(c.args[3]) .. ":" .. tostring(c.args[1]) + print("read." .. key .. " " .. tostring(c.args[2])) + calls[key] = (calls[key] or 0) + 1 + end +end +for key, n in pairs(calls) do print("calls." .. key .. " " .. n) end +''' + + +def u16(value: int) -> int: + return value & 0xFFFF + + +def i32(value: int) -> tuple[int, int]: + raw = value & 0xFFFFFFFF + return raw >> 16, raw & 0xFFFF + + +def run(registers: dict[int, int], fail: tuple[int, ...] = (), + short: dict[int, int] | None = None, polls: int = 1) -> dict[str, str]: + lines = [f"host._modbus_registers.holding[{a}] = {v}" for a, v in registers.items()] + lines += [f'host._modbus_read_fail_addresses[{a}] = "Illegal Data Address"' for a in fail] + lines += [f"host._modbus_read_short_counts[{a}] = {n}" for a, n in (short or {}).items()] + script = ( + f'package.path = "{HARNESS}/?.lua;" .. package.path\n' + 'require("host_mock")\n' + "host.reset()\n" + + "\n".join(lines) + "\n" + f'dofile("{DRIVER}")\n' + "driver_init({})\n" + f"for _ = 1, {polls} do\n" + " local ok, err = pcall(driver_poll)\n" + ' if not ok then print("POLL_ERROR " .. tostring(err)) os.exit(1) end\n' + "end\n" + + REPORT) + 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 num(out: dict[str, str], key: str) -> float: + assert key in out, f"{key} missing from {sorted(out)}" + return float(out[key]) + + +def meter_regs(total_w: int) -> dict[int, int]: + hi, lo = i32(total_w) + return {0x0017: u16(52), 0x001A: 5001, 0x0021: hi, 0x0022: lo} + + +@pytest.mark.parametrize("raw_w", [2300, -1500]) +def test_meter_power_is_total_active_power_at_0x0021(raw_w): + # Positive is drawing from the grid, the site convention: passed through. + out = run(meter_regs(raw_w)) + assert num(out, "meter.W") == raw_w + assert num(out, "meter.L1_A") == pytest.approx(5.2) + assert num(out, "meter.Hz") == pytest.approx(50.01) + # The sources disagree on the phase voltage's scale, so it is left out. + assert "meter.L1_V" not in out + assert out["read.holding:23"] == "12" + + +def test_meter_left_out_when_its_read_fails(): + out = run(meter_regs(2300), fail=(0x0017,)) + assert not any(k.startswith("meter.") for k in out) + + +@pytest.mark.parametrize("raw, site_w", [(-2000, 2000), (1500, -1500)]) +def test_battery_power_at_0x0126_with_site_sign(raw, site_w): + # AlphaESS: "-: Charge, +: Discharge". Site: charging is positive. + out = run({0x0100: 5120, 0x0102: 875, 0x0126: u16(raw)}) + assert num(out, "battery.W") == site_w + assert num(out, "battery.V") == pytest.approx(512.0) + assert num(out, "battery.SoC_nom_fract") == pytest.approx(0.875) + assert out["read.holding:256"] == "39" + + +def test_battery_soc_out_of_range_is_left_out(): + out = run({0x0100: 5120, 0x0102: 1001, 0x0126: 0}) + assert "battery.SoC_nom_fract" not in out + assert num(out, "battery.W") == 0 + + +def test_battery_left_out_when_its_read_fails(): + out = run({0x0100: 5120, 0x0102: 875, 0x0126: u16(-2000)}, fail=(0x0100,)) + assert not any(k.startswith("battery.") for k in out) + + +def test_battery_short_reply_is_a_failed_read(): + out = run({0x0100: 5120, 0x0102: 875, 0x0126: u16(-2000)}, short={0x0100: 10}) + assert not any(k.startswith("battery.") for k in out) + + +def test_pv_power_is_the_sum_of_the_string_powers(): + regs = { + 0x041D: 3500, 0x041E: 82, # PV1 350.0 V, 8.2 A + 0x041F: 0, 0x0420: 2870, # PV1 2870 W + 0x0423: 0, 0x0424: 1200, # PV2 1200 W + 0x042B: 1, 0x042C: 0, # PV4 65536 W: the high word counts + } + out = run(regs) + assert num(out, "pv.W") == -(2870 + 1200 + 65536) + assert num(out, "pv.mppt1_v") == pytest.approx(350.0) + assert num(out, "pv.mppt1_a") == pytest.approx(8.2) + assert out["read.holding:1053"] == "24" + + +def test_pv_left_out_when_its_read_fails(): + out = run({0x041D: 3500, 0x041F: 0, 0x0420: 2870}, fail=(0x041D,)) + assert not any(k.startswith("pv.") for k in out) + + +def test_nothing_is_sent_when_every_read_fails(): + out = run({}, fail=(0x0017, 0x0100, 0x041D)) + assert not any(k.split(".")[0] in ("pv", "battery", "meter") for k in out) + + +def test_old_map_registers_are_not_read(): + out = run({}) + for addr in (16, 18, 32, 33, 34, 36, 38, 39, 40): + assert f"read.holding:{addr}" not in out, addr + + +def test_no_battery_sends_no_battery_stream(): + # Voltage and SoC both 0: no pack on the DC bus, not an empty one. + out = run({0x0100: 0, 0x0102: 0, 0x0126: 0, 0x010D: 0, 0x0110: 0}) + assert not any(k.startswith("battery.") for k in out) + assert "metric.battery_cell_temp_min_c" not in out + assert "metric.battery_cell_temp_max_c" not in out + assert "pv.W" in out + + +def test_empty_pack_with_a_voltage_is_still_sent(): + out = run({0x0100: 4600, 0x0102: 0, 0x0126: 0}) + assert num(out, "battery.SoC_nom_fract") == 0 + assert num(out, "battery.V") == pytest.approx(460.0) + + +def test_current_map_is_confirmed_once_by_the_protocol_version_register(): + out = run(meter_regs(2300), polls=3) + assert out["calls.holding:1870"] == "1" # 0x074E + assert out["calls.holding:23"] == "3" + assert num(out, "meter.W") == 2300 + + +def test_old_firmware_without_system_info_gets_no_meter_and_three_strings(): + regs = meter_regs(2300) + regs.update({0x041D: 3500, 0x041E: 82, 0x041F: 0, 0x0420: 2870, + 0x0429: 250, 0x042B: 1, 0x042C: 0, + 0x0100: 5120, 0x0102: 875, 0x0126: 0}) + out = run(regs, fail=(0x074E,), polls=5) + assert not any(k.startswith("meter.") for k in out) + assert "read.holding:23" not in out + # PV1-PV3 sit where they always have; 0x0429 was the inverter + # temperature in V1.17, so strings 4 to 6 are not read. + assert out["read.holding:1053"] == "12" + assert num(out, "pv.W") == -2870 + assert "battery.W" in out + # The probe is bounded: three tries, then left alone. + assert out["calls.holding:1870"] == "3" diff --git a/drivers/tests/test_kstar_battery_now.py b/drivers/tests/test_kstar_battery_now.py index e56cded..0b25fd5 100644 --- a/drivers/tests/test_kstar_battery_now.py +++ b/drivers/tests/test_kstar_battery_now.py @@ -2,9 +2,11 @@ KSTAR's hybrid Modbus map (V2.9.1, as read by ha-solarman) holds the inverter radiator temperature in input register 3055 and the battery temperature in -3067, both I16 x 0.1 °C. The driver reads 3055..3067 in one go. A failed read -or a value outside -40..100 °C is left out, never sent as zero. The map has -no live BMS limit, so no `available_*` field is sent. +3067, both I16 x 0.1 °C. The driver reads 3055..3067 in one go, together +with the battery's voltage, power and SoC. A failed read or a value outside +-40..100 °C is left out, never sent as zero. The map does not say that its +charge and discharge current limits come from the BMS, so no `available_*` +field is sent. """ from __future__ import annotations @@ -79,7 +81,7 @@ def num(out: dict[str, str], key: str) -> float: def test_kstar_reports_battery_and_inverter_temperature(): - out = run({3055: 455, 3067: i16(-50)}) + out = run({3055: 455, 3063: 5230, 3067: i16(-50)}) assert num(out, "battery.temperature_C") == pytest.approx(-5.0, abs=1e-3) assert num(out, "metric.battery_temp_c") == pytest.approx(-5.0, abs=1e-3) assert num(out, "metric.inverter_temp_c") == pytest.approx(45.5, abs=1e-3) @@ -93,12 +95,12 @@ def test_kstar_failed_temperature_read_is_left_out(): assert "battery.temp_c" not in out assert "metric.battery_temp_c" not in out assert "metric.inverter_temp_c" not in out - # The battery stream itself still goes out. - assert "battery.W" in out + # The whole block failed, so nothing from it goes out. + assert not any(k.startswith("battery.") for k in out) def test_kstar_out_of_range_temperature_is_dropped(): - out = run({3055: 455, 3067: 1500}) + out = run({3055: 455, 3063: 5230, 3067: 1500}) assert "battery.temperature_C" not in out assert "metric.battery_temp_c" not in out assert num(out, "metric.inverter_temp_c") == pytest.approx(45.5, abs=1e-3) diff --git a/drivers/tests/test_kstar_map.py b/drivers/tests/test_kstar_map.py new file mode 100644 index 0000000..8297d60 --- /dev/null +++ b/drivers/tests/test_kstar_map.py @@ -0,0 +1,208 @@ +"""KSTAR reads the input registers its own Modbus map gives, with the site's signs. + +KSTAR's Hybrid Inverter Modbus V2.9.1 (table 3.1, function 04H) puts: + +- PV1 voltage at 3000 (x 0.1 V), PV1 current at 3012 (S16 x 0.01 A), PV1 and + PV2 power at 3024 and 3025 (S16, 1 W); +- the inverter model at 3045 (table 3.1.5: 0x00-0x1F single phase, + 0x20-0x2F three phase); +- battery voltage at 3063 (x 0.01 V), power at 3065 (S16, 1 W) and SoC at + 3066 (x 0.1 %); +- per phase grid voltage, frequency, meter current and meter power at + 3097-3100 (R), 3101-3104 (S) and 3105-3108 (T), S and T "only for three + phase model". + +KSTAR gives no signs. ha-solarman negates the meter power to make import +positive, and counts battery power as power into the DC bus (positive while +discharging). The driver turns both into site signs. Earlier releases read +holding registers 10-54, which no source has. +""" + +from __future__ import annotations + +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" / "kstar.lua" + +pytestmark = pytest.mark.skipif( + not LUA.exists(), reason="run make check to build ./lua55") + +REPORT = ''' +local function dump(prefix, t) + if not t then return end + for k, v in pairs(t) do + if type(v) ~= "table" then print(prefix .. k .. " " .. tostring(v)) end + end +end +local function last(t) return t and t[#t] or nil end +for _, der in ipairs({"pv", "battery", "meter"}) do + dump(der .. ".", last(host._emitted[der])) +end +for name, m in pairs(host._metrics) do + print("metric." .. name .. " " .. tostring(m.value)) +end +local calls = {} +for _, c in ipairs(host._calls) do + if c.func == "modbus_read" then + local key = tostring(c.args[3]) .. ":" .. tostring(c.args[1]) + print("read." .. key .. " " .. tostring(c.args[2])) + calls[key] = (calls[key] or 0) + 1 + end +end +for key, n in pairs(calls) do print("calls." .. key .. " " .. n) end +''' + +SINGLE = 0x04 # KSE-5K-048S +THREE = 0x21 # E8KT + + +def u16(value: int) -> int: + return value & 0xFFFF + + +def run(registers: dict[int, int], fail: tuple[int, ...] = (), + polls: int = 1) -> dict[str, str]: + lines = [f"host._modbus_registers.input[{a}] = {v}" for a, v in registers.items()] + lines += [f'host._modbus_read_fail_addresses[{a}] = "Illegal Data Address"' for a in fail] + script = ( + f'package.path = "{HARNESS}/?.lua;" .. package.path\n' + 'require("host_mock")\n' + "host.reset()\n" + + "\n".join(lines) + "\n" + f'dofile("{DRIVER}")\n' + "driver_init({})\n" + f"for _ = 1, {polls} do\n" + " local ok, err = pcall(driver_poll)\n" + ' if not ok then print("POLL_ERROR " .. tostring(err)) os.exit(1) end\n' + "end\n" + + REPORT) + 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 num(out: dict[str, str], key: str) -> float: + assert key in out, f"{key} missing from {sorted(out)}" + return float(out[key]) + + +def meter_regs(model: int, r_w: int, s_w: int = 0, t_w: int = 0) -> dict[int, int]: + return { + 3045: model, + 3097: 2304, 3098: 4998, 3099: u16(-5230), 3100: u16(r_w), + 3104: u16(s_w), 3108: u16(t_w), + } + + +@pytest.mark.parametrize("raw, site_w", [(1800, -1800), (-950, 950)]) +def test_single_phase_meter_power_with_site_sign(raw, site_w): + # KSTAR's raw meter power is positive when exporting; site: import positive. + out = run(meter_regs(SINGLE, raw, s_w=999, t_w=999)) + assert num(out, "meter.W") == site_w + assert num(out, "meter.L1_V") == pytest.approx(230.4) + assert num(out, "meter.Hz") == pytest.approx(49.98) + assert num(out, "meter.L1_A") == pytest.approx(-5.23) + # Single phase: only R is read; S and T are not summed in. + assert out["read.input:3097"] == "4" + + +def test_three_phase_meter_sums_the_phases(): + out = run(meter_regs(THREE, -500, -600, -700)) + assert num(out, "meter.W") == 1800 + assert out["read.input:3097"] == "12" + + +@pytest.mark.parametrize("code", [10, 13, 16, 19, 31, 35, 47, 0x40]) +def test_unlisted_model_sends_no_meter(code): + # Table 3.1.5 marks 10, 13, 16, 19-31 and 35-47 "reserve". + out = run(meter_regs(code, 1800)) + assert not any(k.startswith("meter.") for k in out) + assert "read.input:3097" not in out + + +@pytest.mark.parametrize("code, count", [ + (0, "4"), (9, "4"), (11, "4"), (14, "4"), (18, "4"), + (32, "12"), (33, "12"), (34, "12"), +]) +def test_listed_models_read_their_phases(code, count): + out = run(meter_regs(code, 1800)) + assert out["read.input:3097"] == count + assert "meter.W" in out + + +def test_no_battery_sends_no_battery_stream(): + # Voltage and SoC both 0: no pack, not an empty one. + out = run({3055: 455, 3063: 0, 3065: 0, 3066: 0, 3067: 0}) + assert not any(k.startswith("battery.") for k in out) + assert "metric.battery_temp_c" not in out + # The inverter's own temperature is not the battery's. + assert num(out, "metric.inverter_temp_c") == pytest.approx(45.5) + + +def test_empty_pack_with_a_voltage_is_still_sent(): + out = run({3063: 4610, 3065: 0, 3066: 0}) + assert num(out, "battery.SoC_nom_fract") == 0 + assert num(out, "battery.V") == pytest.approx(46.10) + + +def test_model_read_failure_sends_no_meter_but_keeps_the_rest(): + regs = meter_regs(SINGLE, 1800) + regs.update({3063: 5230, 3065: 0, 3066: 500, 3024: 100, 3025: 0}) + out = run(regs, fail=(3045,)) + assert not any(k.startswith("meter.") for k in out) + assert "battery.W" in out + assert "pv.W" in out + + +def test_model_is_read_once(): + out = run(meter_regs(SINGLE, 1800), polls=3) + assert out["calls.input:3045"] == "1" + assert out["calls.input:3097"] == "3" + assert num(out, "meter.W") == -1800 + + +def test_meter_left_out_when_its_read_fails(): + out = run(meter_regs(SINGLE, 1800), fail=(3097,)) + assert not any(k.startswith("meter.") for k in out) + + +@pytest.mark.parametrize("raw, site_w", [(-2500, 2500), (1200, -1200)]) +def test_battery_power_at_3065_with_site_sign(raw, site_w): + # Raw positive is power into the DC bus (discharging); site: charging positive. + out = run({3063: 5230, 3065: u16(raw), 3066: 643, 3067: 215}) + assert num(out, "battery.W") == site_w + assert num(out, "battery.V") == pytest.approx(52.30) + assert num(out, "battery.SoC_nom_fract") == pytest.approx(0.643) + assert num(out, "battery.temperature_C") == pytest.approx(21.5) + assert out["read.input:3055"] == "13" + + +def test_battery_soc_out_of_range_is_left_out(): + out = run({3063: 5230, 3065: 0, 3066: 1001}) + assert "battery.SoC_nom_fract" not in out + + +def test_pv_reads_input_registers_from_3000(): + out = run({3000: 3805, 3012: 921, 3024: 2100, 3025: 1350}) + assert num(out, "pv.W") == -3450 + assert num(out, "pv.mppt1_v") == pytest.approx(380.5) + assert num(out, "pv.mppt1_a") == pytest.approx(9.21) + assert out["read.input:3000"] == "26" + + +def test_pv_left_out_when_its_read_fails(): + out = run({3000: 3805, 3024: 2100}, fail=(3000,)) + assert not any(k.startswith("pv.") for k in out) + + +def test_no_holding_registers_are_read(): + out = run(meter_regs(SINGLE, 1800)) + assert not any(k.startswith("read.holding:") for k in out) diff --git a/index.yaml b/index.yaml index 762fd25..6facac3 100644 --- a/index.yaml +++ b/index.yaml @@ -75,14 +75,14 @@ drivers: size_bytes: 23895 sha256: "e38da421ba6e5d3ceed2e9c6b7fdd620537b34e63ca48da14ac1e42080b133d0" - name: "alphaess" - version: "1.2.0" + version: "1.3.0" tier: community protocol: modbus connectivity: local ders: [pv, battery, meter] control: false - size_bytes: 6394 - sha256: "f1bf71b8f0a1e1e76c416f84e8f1bd7b08cb84f31fbb249a7c85921a7159a74d" + size_bytes: 9564 + sha256: "ad6de3206098e063f6e2344b35a4ff836c20d69549777aaf6a9b3c68eca4545d" - name: "ambibox" version: "1.2.0" tier: core @@ -403,14 +403,14 @@ drivers: size_bytes: 17688 sha256: "a5ea16a0f5a3315487384589589e14a3c0ef0407339997a0251dc5b50039008e" - name: "kstar" - version: "1.2.0" + version: "1.3.0" tier: community protocol: modbus connectivity: local ders: [pv, battery, meter] control: false - size_bytes: 5508 - sha256: "1855663dd2c8f2a1c08eda1cd6af06f9f3391df0900b29a654c89229216d635f" + size_bytes: 9159 + sha256: "b13d4a0e1d8a7a41e0f3fd175c60159b1324273c066167cf7903fc0a73c521d0" - name: "mennekes" version: "1.1.1" tier: community diff --git a/manifests/alphaess.yaml b/manifests/alphaess.yaml index 0d7ef07..acd24e9 100644 --- a/manifests/alphaess.yaml +++ b/manifests/alphaess.yaml @@ -1,5 +1,5 @@ name: "alphaess" -version: "1.2.0" +version: "1.3.0" tier: community author: "Sourceful Labs AB" protocol: modbus @@ -22,6 +22,22 @@ tested_devices: notes: "Community driver, untested" min_driver_version: "1.0.0" upstream_docs: + - url: "https://raw.githubusercontent.com/ramonvanraaij/ha-alphaess-modbus/main/docs/alphaess_modbus_register_parameter_list.pdf" + title: "AlphaESS Modbus parameter address table (household meter, battery and inverter blocks), copy in ramonvanraaij/ha-alphaess-modbus" + kind: register_map + url_stability: stable + - url: "https://raw.githubusercontent.com/ramonvanraaij/ha-alphaess-modbus/main/docs/alphaess_modbus_register_map_table.pdf" + title: "AlphaESS Household Register Map: register blocks per model, copy in ramonvanraaij/ha-alphaess-modbus" + kind: register_map + url_stability: stable + - url: "https://raw.githubusercontent.com/ramonvanraaij/ha-alphaess-modbus/main/docs/alphaess_modbus_protocol.pdf" + title: "AlphaESS Modbus_RTU/TCP Protocol, version history to V1.39, copy in ramonvanraaij/ha-alphaess-modbus" + kind: changelog + url_stability: stable + - url: "https://raw.githubusercontent.com/evcc-io/evcc/master/templates/definition/meter/alpha-ess-smile.yaml" + title: "evcc alpha-ess-smile template (grid, PV and battery registers and signs)" + kind: other + url_stability: stable - url: "https://raw.githubusercontent.com/dxoverdy/Alpha2MQTT/master/Alpha2MQTT/Definitions.h" title: "dxoverdy/Alpha2MQTT AlphaESS register definitions (battery block 0x0100-0x0130)" kind: register_map @@ -31,12 +47,12 @@ upstream_docs: kind: register_map url_stability: stable min_host_version: "2.0.0" -size_bytes: 6394 +size_bytes: 9564 dkb_id: "" -sha256: "f1bf71b8f0a1e1e76c416f84e8f1bd7b08cb84f31fbb249a7c85921a7159a74d" +sha256: "ad6de3206098e063f6e2344b35a4ff836c20d69549777aaf6a9b3c68eca4545d" signature: "" bytecode_sha256: "" bytecode_signature: "" bytecode_size: 0 -changelog: "" +changelog: "Read the grid meter, battery and PV from the registers in AlphaESS's current Modbus map, with site signs; send nothing for a read that failed." diff --git a/manifests/kstar.yaml b/manifests/kstar.yaml index 9290f56..6a0232a 100644 --- a/manifests/kstar.yaml +++ b/manifests/kstar.yaml @@ -1,5 +1,5 @@ name: "kstar" -version: "1.2.0" +version: "1.3.0" tier: community author: "Sourceful Labs AB" protocol: modbus @@ -22,17 +22,21 @@ tested_devices: notes: "Community driver, untested" min_driver_version: "1.0.0" upstream_docs: + - url: "https://github.com/user-attachments/files/17555061/KSTAR.Hybrid.Inverter.Modbus.V2.9.1_2024.07.21.pdf" + title: "KSTAR Hybrid Inverter MODBUS RS485 Communication Protocol V2.9.1 (2024-07-21), from KSTAR support, attached to davidrapan/ha-solarman" + kind: register_map + url_stability: unknown - url: "https://raw.githubusercontent.com/davidrapan/ha-solarman/main/custom_components/solarman/inverter_definitions/kstar_hybrid.yaml" title: "davidrapan/ha-solarman KSTAR hybrid inverter register definitions (from KSTAR Hybrid Inverter Modbus V2.9.1)" kind: register_map url_stability: stable min_host_version: "2.0.0" -size_bytes: 5508 +size_bytes: 9159 dkb_id: "" -sha256: "1855663dd2c8f2a1c08eda1cd6af06f9f3391df0900b29a654c89229216d635f" +sha256: "b13d4a0e1d8a7a41e0f3fd175c60159b1324273c066167cf7903fc0a73c521d0" signature: "" bytecode_sha256: "" bytecode_signature: "" bytecode_size: 0 -changelog: "" +changelog: "Read PV, battery and grid meter from KSTAR's input registers from 3000, with site signs; send nothing for a read that failed." diff --git a/support-status.json b/support-status.json index 4499dfb..a7d14b0 100644 --- a/support-status.json +++ b/support-status.json @@ -170,7 +170,7 @@ }, { "catalog_source": true, - "catalog_version": "1.2.0", + "catalog_version": "1.3.0", "driver_id": "alphaess", "targets": { "blixt-l1": { @@ -905,7 +905,7 @@ }, { "catalog_source": true, - "catalog_version": "1.2.0", + "catalog_version": "1.3.0", "driver_id": "kstar", "targets": { "blixt-l1": {