From d90fd259b00653c8c806e08e0923d40c4f7b6b8b Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Sun, 22 Nov 2026 13:54:08 +0100 Subject: [PATCH] fix(ferroamp_modbus): leave out a NaN or infinite float (2.2.2) decode_f32_ws_at turned a NaN or infinite reply, the EnergyHub's "not present", into 0, so a NaN grid power went out as a 0 W meter and a NaN SoC as an empty pack. It now returns nil, as it does for a reply missing a word, and every caller goes through f32_at, which also drops a value that becomes infinite after scaling. Harness tests for NaN and infinity on the meter, PV and battery fail on main. Not checked on hardware. Refs srcfl/device-drivers#258 Signed-off-by: Fredrik Ahlgren Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 13 ++ SUPPORT_STATUS.md | 4 +- devices.yaml | 2 +- drivers/lua/ferroamp_modbus.lua | 75 +++++----- .../lua_harness/test_ferroamp_modbus_nan.lua | 131 ++++++++++++++++++ drivers/tests/test_ferroamp_modbus_nan.py | 38 +++++ index.yaml | 6 +- manifests/ferroamp_modbus.yaml | 6 +- support-status.json | 2 +- 9 files changed, 236 insertions(+), 41 deletions(-) create mode 100644 drivers/tests/lua_harness/test_ferroamp_modbus_nan.lua create mode 100644 drivers/tests/test_ferroamp_modbus_nan.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 5766b57..e7844ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,19 @@ ## [Unreleased] +### ferroamp_modbus 2.2.2 + +A float that answers NaN or infinity is now left out instead of sent as 0 +(docs/WRITING-A-DRIVER.md, "Never fabricate" and "Numbers"). The EnergyHub +uses them for "not present", and the decoder turned them into 0, so a NaN +grid power went out as a 0 W meter and a NaN SoC as an empty pack. The +same rule now covers a reply that is missing a word, also on the battery +reads, and a value that only becomes infinite after scaling, as float32's +largest value times 1000 does where Lua numbers are 32-bit floats. The +meter and PV readings still go out only with their power, and each +per-phase watt figure only with that phase's voltage and current. A real +0.0 is still a reading. Not yet checked on hardware. + ### growatt 2.2.2 A failed read no longer becomes 0 (docs/WRITING-A-DRIVER.md, "Never diff --git a/SUPPORT_STATUS.md b/SUPPORT_STATUS.md index 66cbb06..035e94b 100644 --- a/SUPPORT_STATUS.md +++ b/SUPPORT_STATUS.md @@ -54,8 +54,8 @@ Catalog source is not proof that a target can install or run a driver. | ferroamp | 2.1.2 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | ferroamp_dc2_v2x | 2.1.1 | ftw-core | not_assessed | — | not_recorded | not_assessed | | ferroamp_dc2_v2x | 2.1.1 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | -| ferroamp_modbus | 2.2.1 | ftw-core | not_assessed | — | not_recorded | not_assessed | -| ferroamp_modbus | 2.2.1 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | +| ferroamp_modbus | 2.2.2 | ftw-core | not_assessed | — | not_recorded | not_assessed | +| ferroamp_modbus | 2.2.2 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | foxess_h3_smart | 0.10.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | | foxess_h3_smart | 0.10.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed | | fronius | 2.2.0 | ftw-core | not_assessed | — | not_recorded | not_assessed | diff --git a/devices.yaml b/devices.yaml index 36fe113..346a0c4 100644 --- a/devices.yaml +++ b/devices.yaml @@ -382,7 +382,7 @@ manufacturers: control: true - protocol: modbus driver: "ferroamp_modbus" - version: "2.2.1" + version: "2.2.2" ders: [battery, meter, pv] control: false firmware_versions: "" diff --git a/drivers/lua/ferroamp_modbus.lua b/drivers/lua/ferroamp_modbus.lua index 87cff1d..89a7453 100644 --- a/drivers/lua/ferroamp_modbus.lua +++ b/drivers/lua/ferroamp_modbus.lua @@ -23,7 +23,7 @@ DRIVER = { id = "ferroamp_modbus", name = "Ferroamp EnergyHub (Modbus)", manufacturer = "Ferroamp", - version = "2.2.1", + version = "2.2.2", protocols = { "modbus" }, capabilities = { "meter", "pv", "battery" }, read_only = true, @@ -47,7 +47,9 @@ PROTOCOL = "modbus" -- Decode IEEE 754 single-precision float from two u16 registers, -- big-endian word order: hi = first register, lo = second. --- Returns 0 for NaN / ±Inf (treated as "not implemented" sentinels). +-- Returns nil for NaN / ±Inf, which the EnergyHub uses for "not present": +-- the field is left out, never sent as 0 (docs/WRITING-A-DRIVER.md, +-- "Never fabricate"). Up to 2.2.1 they decoded as 0. -- -- Works on the 16-bit halves (docs/WRITING-A-DRIVER.md, "Numbers"). The -- earlier version built the 32-bit word and compared it with 0x80000000, @@ -64,7 +66,7 @@ local function decode_f32_be(hi, lo) local exp = math.floor(hi / 0x80) local frac = (hi % 0x80) * 0x10000 + lo if exp == 0 and frac == 0 then return 0 end - if exp == 0xFF then return 0 end -- NaN / Inf → not present + if exp == 0xFF then return nil end -- NaN / Inf → not present if exp == 0 then return sign * frac / 0x800000 * (2 ^ -126) end @@ -76,13 +78,25 @@ end -- (hi, lo), so we swap here. `regs` is the 1-indexed table returned by -- host.modbus_read, `idx` is the position of the LOW word. local function decode_f32_ws_at(regs, idx) - if not regs then return 0 end + if not regs then return nil end local lo = regs[idx] local hi = regs[idx + 1] - if lo == nil or hi == nil then return 0 end + if lo == nil or hi == nil then return nil end return decode_f32_be(hi, lo) end +-- The float at idx times scale, or nil when a word is missing or the value +-- is NaN or infinite. Checked again after scaling: float32's largest value +-- times 1000 is infinite where Lua numbers are 32-bit floats +-- (docs/WRITING-A-DRIVER.md, "Numbers"). +local function f32_at(regs, idx, scale) + local v = decode_f32_ws_at(regs, idx) + if v == nil then return nil end + v = v * scale + if v ~= v or v == math.huge or v == -math.huge then return nil end + return v +end + ---------------------------------------------------------------------------- -- Bounded reads ---------------------------------------------------------------------------- @@ -117,8 +131,7 @@ local function probe_read(addr, count, kind) return nil end --- A reply shorter than the read did not deliver every register asked for, --- and decode_f32_ws_at would turn the missing word into 0. +-- A reply shorter than the read did not deliver every register asked for. local function complete(regs, count) return regs ~= nil and regs[count] ~= nil end @@ -149,44 +162,43 @@ function driver_poll() -- Grid frequency: input 2016, float32, Hz (2 regs, word-swapped) local hz_regs = probe_read(2016, 2, "input") if complete(hz_regs, 2) then - meter.hz = decode_f32_ws_at(hz_regs, 1) - host.emit_metric("grid_hz", meter.hz) + meter.hz = f32_at(hz_regs, 1, 1) + if meter.hz ~= nil then host.emit_metric("grid_hz", meter.hz) end end -- Grid voltage L1/L2/L3: input 2032/2036/2040, float32, Vrms. -- Each value occupies 4 regs (2 f32 + 2 unused), so read 10 regs for 3 values. local v_regs = probe_read(2032, 10, "input") if complete(v_regs, 10) then - meter.l1_v = decode_f32_ws_at(v_regs, 1) -- 2032-2033 - meter.l2_v = decode_f32_ws_at(v_regs, 5) -- 2036-2037 - meter.l3_v = decode_f32_ws_at(v_regs, 9) -- 2040-2041 + meter.l1_v = f32_at(v_regs, 1, 1) -- 2032-2033 + meter.l2_v = f32_at(v_regs, 5, 1) -- 2036-2037 + meter.l3_v = f32_at(v_regs, 9, 1) -- 2040-2041 end -- Grid active power (total): input 3100, float32, kW local gw_regs = probe_read(3100, 2, "input") - if complete(gw_regs, 2) then meter.w = decode_f32_ws_at(gw_regs, 1) * 1000 end + if complete(gw_regs, 2) then meter.w = f32_at(gw_regs, 1, 1000) end -- Grid active current L1/L2/L3: input 3112/3116/3120, float32, Arms local ga_regs = probe_read(3112, 10, "input") if complete(ga_regs, 10) then - meter.l1_a = decode_f32_ws_at(ga_regs, 1) -- 3112-3113 - meter.l2_a = decode_f32_ws_at(ga_regs, 5) -- 3116-3117 - meter.l3_a = decode_f32_ws_at(ga_regs, 9) -- 3120-3121 + meter.l1_a = f32_at(ga_regs, 1, 1) -- 3112-3113 + meter.l2_a = f32_at(ga_regs, 5, 1) -- 3116-3117 + meter.l3_a = f32_at(ga_regs, 9, 1) -- 3120-3121 end -- Per-phase power: V * I_active (no per-phase power registers available), -- only when both answered. - if meter.l1_v and meter.l1_a then - meter.l1_w = meter.l1_v * meter.l1_a - meter.l2_w = meter.l2_v * meter.l2_a - meter.l3_w = meter.l3_v * meter.l3_a + for _, phase in ipairs({ "l1", "l2", "l3" }) do + local v, a = meter[phase .. "_v"], meter[phase .. "_a"] + if v ~= nil and a ~= nil then meter[phase .. "_w"] = v * a end end -- Grid energy: export at 3064, import at 3068, float32, kWh (8 regs, two values) local ge_regs = probe_read(3064, 8, "input") if complete(ge_regs, 8) then - meter.export_wh = decode_f32_ws_at(ge_regs, 1) * 1000 -- 3064-3065 - meter.import_wh = decode_f32_ws_at(ge_regs, 5) * 1000 -- 3068-3069 + meter.export_wh = f32_at(ge_regs, 1, 1000) -- 3064-3065 + meter.import_wh = f32_at(ge_regs, 5, 1000) -- 3068-3069 end if meter.w ~= nil then host.emit("meter", meter) end @@ -206,13 +218,14 @@ function driver_poll() -- Solar power: input 5100, float32, kW (always positive from Ferroamp). -- Without it there is no PV reading. local pv_regs = probe_read(5100, 2, "input") - if complete(pv_regs, 2) then + local pv_w = complete(pv_regs, 2) and f32_at(pv_regs, 1, 1000) or nil + if pv_w ~= nil then local pv = {} - pv.w = -decode_f32_ws_at(pv_regs, 1) * 1000 -- negative = generation + pv.w = -pv_w -- negative = generation -- Solar energy produced: input 5064, float32, kWh local pe_regs = probe_read(5064, 2, "input") - if complete(pe_regs, 2) then pv.lifetime_wh = decode_f32_ws_at(pe_regs, 1) * 1000 end + if complete(pe_regs, 2) then pv.lifetime_wh = f32_at(pe_regs, 1, 1000) end host.emit("pv", pv) end @@ -236,20 +249,20 @@ function driver_poll() -- Ferroamp: positive = discharging. Site convention: positive = charging. -- Negate and convert kW → W (matches drivers/ferroamp.lua's sign handling). local bw_regs = probe_read(6100, 2, "input") - if bw_regs then bat_data.w = -decode_f32_ws_at(bw_regs, 1) * 1000 end + local bat_w = f32_at(bw_regs, 1, 1000) + if bat_w ~= nil then bat_data.w = -bat_w end -- Battery SoC: input 6016, float32, percent → 0-1 fraction local soc_regs = probe_read(6016, 2, "input") - if soc_regs then bat_data.soc = decode_f32_ws_at(soc_regs, 1) / 100 end + local soc_pct = f32_at(soc_regs, 1, 1) + if soc_pct ~= nil then bat_data.soc = soc_pct / 100 end -- Battery energy: discharge at 6064, charge at 6068, float32, kWh (8 regs). -- Read as input registers; driver_command writes holding 6064 for the power -- setpoint, which is a different function code and a separate path. local be_regs = probe_read(6064, 8, "input") - if be_regs then - bat_data.discharge_wh = decode_f32_ws_at(be_regs, 1) * 1000 -- 6064-6065 - bat_data.charge_wh = decode_f32_ws_at(be_regs, 5) * 1000 -- 6068-6069 - end + bat_data.discharge_wh = f32_at(be_regs, 1, 1000) -- 6064-6065 + bat_data.charge_wh = f32_at(be_regs, 5, 1000) -- 6068-6069 -- Send whatever answered (docs/WRITING-A-DRIVER.md, "A hybrid inverter -- may have no battery"): a failed power read must not also drop a good diff --git a/drivers/tests/lua_harness/test_ferroamp_modbus_nan.lua b/drivers/tests/lua_harness/test_ferroamp_modbus_nan.lua new file mode 100644 index 0000000..c219f58 --- /dev/null +++ b/drivers/tests/lua_harness/test_ferroamp_modbus_nan.lua @@ -0,0 +1,131 @@ +-- ferroamp_modbus: a float that answers NaN or infinity is left out, never +-- sent as 0. Run with the scenario as arg[1]. +-- +-- The EnergyHub's floats are IEEE 754 with the low word at the lower +-- address. NaN is 0x7FC00000, +infinity 0x7F800000, -infinity 0xFF800000. +-- The values are synthetic, not a capture. + +local scenario = arg[1] + +dofile("drivers/tests/lua_harness/host_mock.lua") +host.reset() + +local function f32(value) + local hi, lo = string.unpack(">I2I2", string.pack(">f", value)) + return { lo, hi } +end +local NAN = { 0x0000, 0x7FC0 } +local INF = { 0x0000, 0x7F80 } +local NEG_INF = { 0x0000, 0xFF80 } +local MAX = { 0xFFFF, 0x7F7F } -- float32's largest finite value + +-- Floats at slot offsets 0, 4, 8 of a block: { lo, hi, 0, 0, lo, hi, ... }. +local function slots(...) + local block = {} + for _, words in ipairs({ ... }) do + block[#block + 1] = words[1] + block[#block + 1] = words[2] + block[#block + 1] = 0 + block[#block + 1] = 0 + end + return block +end + +local input = host._modbus_registers.input +input[2016] = f32(50.02) +input[2032] = slots(f32(231), f32(229), f32(230)) +input[3100] = f32(1.25) +input[3112] = slots(f32(2), f32(1.5), f32(2)) +input[3064] = slots(f32(800), f32(4100)) +input[5100] = f32(3.4) +input[5064] = f32(9100) +input[6100] = f32(-0.8) -- charging 800 W +input[6016] = f32(64) +input[6064] = slots(f32(10), f32(12)) + +local function poll() + dofile("drivers/lua/ferroamp_modbus.lua") + driver_init({}) + local ok, err = pcall(driver_poll) + if not ok then error(scenario .. ": driver_poll failed: " .. tostring(err)) end + return host._emitted.meter and host._emitted.meter[1], + host._emitted.pv and host._emitted.pv[1], + host._emitted.battery and host._emitted.battery[1] +end + +local function fail(text) error(scenario .. ": " .. text) end + +local function want(name, actual, expected) + if type(actual) ~= "number" or math.abs(actual - expected) > 0.01 then + fail(name .. " = " .. tostring(actual) .. ", want " .. tostring(expected)) + end +end + +local function absent(name, actual) + if actual ~= nil then fail(name .. " = " .. tostring(actual) .. ", want it left out") end +end + +local function metric(name) + local entry = host._metrics[name] + return entry and entry.value +end + +if scenario == "nan_grid_power" then + -- A NaN grid power is no reading: no meter, not a 0 W balanced site. + input[3100] = NAN + local meter, pv = poll() + if meter then fail("emitted a meter reading with w = " .. tostring(meter.w)) end + want("pv.w", pv.w, -3400) + +elseif scenario == "inf_pv_power" then + input[5100] = INF + local meter, pv = poll() + if pv then fail("emitted a PV reading with w = " .. tostring(pv.w)) end + want("meter.w", meter.w, 1250) + +elseif scenario == "nan_one_phase" then + -- L2 voltage NaN: L2 voltage and watts are left out, L1 and L3 stay. + input[2032] = slots(f32(231), NAN, f32(230)) + local meter = poll() + want("meter.l1_v", meter.l1_v, 231) + want("meter.l3_w", meter.l3_w, 460) + absent("meter.l2_v", meter.l2_v) + absent("meter.l2_w", meter.l2_w) + absent("meter_l2_v", metric("meter_l2_v")) + absent("meter_l2_w", metric("meter_l2_w")) + want("meter.l2_a", meter.l2_a, 1.5) + +elseif scenario == "nan_frequency_and_counter" then + input[2016] = NAN + input[3064] = slots(NEG_INF, f32(4100)) + local meter = poll() + absent("meter.hz", meter.hz) + absent("grid_hz", metric("grid_hz")) + absent("meter.export_wh", meter.export_wh) + want("meter.import_wh", meter.import_wh, 4100000) + +elseif scenario == "nan_battery" then + -- NaN SoC and a NaN power: both left out, the energy counters stay. + input[6016] = NAN + input[6100] = NAN + local _, _, battery = poll() + absent("battery.soc", battery.soc) + absent("battery.w", battery.w) + want("battery.charge_wh", battery.charge_wh, 12000) + +elseif scenario == "largest_float_scaled" then + -- float32's largest value times 1000 is infinite where Lua numbers are + -- 32-bit floats, as in this harness (docs/WRITING-A-DRIVER.md, "Numbers"). + input[3100] = MAX + local meter = poll() + if meter then fail("emitted a meter reading with w = " .. tostring(meter.w)) end + +elseif scenario == "real_zero_kept" then + -- 0.0 is a reading, not a sentinel. + input[3100] = f32(0) + local meter = poll() + want("meter.w", meter.w, 0) + +else + error("unknown scenario: " .. tostring(scenario)) +end diff --git a/drivers/tests/test_ferroamp_modbus_nan.py b/drivers/tests/test_ferroamp_modbus_nan.py new file mode 100644 index 0000000..348fe16 --- /dev/null +++ b/drivers/tests/test_ferroamp_modbus_nan.py @@ -0,0 +1,38 @@ +"""ferroamp_modbus leaves out a float that answers NaN or infinity. + +docs/WRITING-A-DRIVER.md, "Never fabricate" and "Numbers": the EnergyHub +uses NaN and infinity for "not present". Up to 2.2.1 the decoder turned them +into 0, so a 0 W meter or a 0 % SoC went out as a reading. +""" + +from pathlib import Path +import subprocess + +import pytest + + +ROOT = Path(__file__).resolve().parents[2] +LUA = ROOT / "lua55" + +pytestmark = pytest.mark.skipif( + not LUA.exists(), reason="run make check to build ./lua55") + + +@pytest.mark.parametrize("scenario", [ + "nan_grid_power", + "inf_pv_power", + "nan_one_phase", + "nan_frequency_and_counter", + "nan_battery", + "largest_float_scaled", + "real_zero_kept", +]) +def test_ferroamp_modbus_nan(scenario): + result = subprocess.run( + [str(LUA), "drivers/tests/lua_harness/test_ferroamp_modbus_nan.lua", scenario], + cwd=ROOT, + text=True, + capture_output=True, + check=False, + ) + assert result.returncode == 0, result.stdout + result.stderr diff --git a/index.yaml b/index.yaml index 11369da..54fef3c 100644 --- a/index.yaml +++ b/index.yaml @@ -234,14 +234,14 @@ drivers: size_bytes: 13358 sha256: "997fc712c5ef0336620fd5d628175c4df831e26b7826b9fdc36f56b5ca3f69d7" - name: "ferroamp_modbus" - version: "2.2.1" + version: "2.2.2" tier: core protocol: modbus connectivity: local ders: [battery, meter, pv] control: false - size_bytes: 12099 - sha256: "b37f6356e8010a80f7c9477e70a582b7f16eba359858bb9fd43f6c3530a0f306" + size_bytes: 12624 + sha256: "f4f4a8c9809d56a8f27021b4159574b6727a2a4936375f12b3344f2136f5fbd7" - name: "foxess_h3_smart" version: "0.10.0" tier: community diff --git a/manifests/ferroamp_modbus.yaml b/manifests/ferroamp_modbus.yaml index ab2830a..c6b213d 100644 --- a/manifests/ferroamp_modbus.yaml +++ b/manifests/ferroamp_modbus.yaml @@ -1,5 +1,5 @@ name: "ferroamp_modbus" -version: "2.2.1" +version: "2.2.2" tier: core author: "Sourceful Labs AB" protocol: modbus @@ -15,9 +15,9 @@ tested_devices: notes: "Modbus TCP must be enabled by Ferroamp support. Port 502, Unit ID 1. Word-swapped float32." min_driver_version: "1.0.0" min_host_version: "2.0.0" -size_bytes: 12099 +size_bytes: 12624 dkb_id: "ferroamp_modbus" -sha256: "b37f6356e8010a80f7c9477e70a582b7f16eba359858bb9fd43f6c3530a0f306" +sha256: "f4f4a8c9809d56a8f27021b4159574b6727a2a4936375f12b3344f2136f5fbd7" signature: "" bytecode_sha256: "" diff --git a/support-status.json b/support-status.json index 4cb6a48..dbeae8b 100644 --- a/support-status.json +++ b/support-status.json @@ -527,7 +527,7 @@ }, { "catalog_source": true, - "catalog_version": "2.2.1", + "catalog_version": "2.2.2", "driver_id": "ferroamp_modbus", "targets": { "blixt-l1": {