Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions SUPPORT_STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion devices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
Expand Down
75 changes: 44 additions & 31 deletions drivers/lua/ferroamp_modbus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
----------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
131 changes: 131 additions & 0 deletions drivers/tests/lua_harness/test_ferroamp_modbus_nan.lua
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions drivers/tests/test_ferroamp_modbus_nan.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading