Skip to content

drivers/pixii: cache "unsupported" scale factors so meter_energy_sf absence stops flapping the poll-fail counter - #703

Closed
murwall wants to merge 1 commit into
srcfl:masterfrom
murwall:fix/drivers-pixii-sf-caching
Closed

murwall wants to merge 1 commit into
srcfl:masterfrom
murwall:fix/drivers-pixii-sf-caching

Conversation

@murwall

@murwall murwall commented Jul 28, 2026

Copy link
Copy Markdown

What

Change read_sf(addr) in drivers/pixii.lua to remember addresses
whose first probe fails. Subsequent polls skip the host.modbus_read
call for those addresses, using SF=0 (×1 scaling) directly — same
fallback semantics the current helper already provides, just without
the repeated failed read.

Why

On Pixii PowerShaper firmware, register 40288 (meter_energy_sf,
SunSpec Model 213 offset 32) returns Modbus exception 2 ("illegal data
address") — the SF register isn't populated by this firmware. Direct
raw Modbus TCP probe from the same host confirms:

40260/1  → OK 0x0000
40261/1  → OK 0x0000      (meter_va_sf)
40266/1  → OK 0x0000      (meter_var_sf)
40287/1  → EXCEPTION 2
40288/1  → EXCEPTION 2    ← the failing SF register
40289/1  → EXCEPTION 2
40290/1  → EXCEPTION 2

The current read_sf handles the failure gracefully:

local function read_sf(addr)
    local ok, regs = pcall(host.modbus_read, addr, 1, "holding")
    if ok and regs then return host.decode_i16(regs[1]) end
    return 0   -- SF=0 = 1× scaling, correct fallback
end

But the Go host counts every failed host.modbus_read toward the
driver-poll error tally regardless of Lua-side pcall handling.
So
on Pixii hardware the driver flaps driver_poll: 1 of 36 modbus reads failed every 5 s indefinitely, gets marked offline via the stale-
telemetry watchdog every minute, watchdog writes setpoint=0 to the
Pixii every 5 s, and SoC / battery / meter data never reach the
planner. This is a regression vs v0.120.9, where the same Pixii
polled cleanly on the same driver source.

Verified with all 26 unique (addr, count) combinations from
grep modbus_read drivers/pixii.lua: every one succeeds against the
device except the single 40288 read. Instrumented host.modbus_read
inside the driver to log every failing address — only 40288 ever
fired.

Behaviour change (or lack thereof)

The return 0 semantics of read_sf are unchanged. What changes:

  1. First poll: same behaviour as before (probe once, log if it fails).
  2. Subsequent polls: for an address confirmed unsupported, we skip the
    modbus_read call entirely and return 0 immediately. Downstream
    code still uses SF=0 (×1 scaling) — bit-for-bit identical result.
  3. Restart re-probes each SF once (firmware might have been updated).

On hardware where every SF read works, this patch is a no-op — the
cache fills with true values and every read runs as before.

Testing

Verified on Pixii PowerShaper firmware currently running an active
calibration cycle:

Before (v1.13.4 shipped driver):

driver_poll: 1 of 36 modbus reads failed   ← every 5 s indefinitely
Status=1, ConsecutiveErrors 48+
Pixii: watchdog → setpoint 0               ← every 5 s indefinitely

After (this patch):

  • First poll: probe attempts, 40288 fails, one INFO log line: "Pixii: scale factor at 40288 not exposed by this firmware — treating as SF=0 (×1 scaling) henceforth. Reprobe on next restart."
  • All subsequent polls: Status=0, ConsecutiveErrors=0, battery / meter / SoC data emit correctly.
  • Battery dispatch runs cleanly against the CVXPY planner.

Import/export energy calculations still work — meter_energy_sf=0
means ×1 scaling, which matches what v0.120.9 has been doing on this
firmware for months without incident.

Related

  • Same class of bug in solaredge_legacy.lua (MPPT block at 40123 on
    K-series firmware) — separate PR:
    [#PR-solaredge-legacy-skip-unsupported-mppt]
  • Both surface the same root cause: Go host counts failed
    modbus_read calls regardless of Lua's pcall. Localized "cache
    unsupported, skip subsequent reads" pattern fixes both.

Alternative

If a broader architectural approach is preferred, exposing a
host.modbus_read_optional(addr, n, fc) that doesn't count toward
the failure tally on nil returns would let drivers opt out declaratively.
Bigger surface change; the read_sf patch here contains the fix to
the one helper where "optional" SF registers enter the driver.

@murwall
murwall requested a review from frahlg as a code owner July 28, 2026 15:17

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read the diff. Same shape as #702 and just as clean: read_sf caches a per-address sf_supported, so a scale-factor register returning exception-2 (Pixii 40288 meter_energy_sf) is probed once, cached absent, and skipped after that, keeping the failed-read counter clean. Fallback stays SF=0 (x1), same as today.

  • One edge, a touch sharper here than in #702: an address locks to false on the first failed read, and a cached-absent SF returns 0, i.e. x1 scaling. If a supported SF register (real value non-zero, say SF=-2 for x0.01) missed on a transient first probe, that metric would mis-scale until restart rather than just going absent. Low odds (with the link up, an SF miss is almost certainly a real exception-2), but the failure mode is a wrong number, not a missing one, so it's worth being deliberate. 2 or 3 consecutive misses before locking removes it.
  • Security: nothing flagged. No secrets, no new deps, existing host APIs only.

No CI attached that I can see. Safe to merge from my read once the driver test run is confirmed green. Not in my allowlist, so a human merges.

@frahlg

frahlg commented Jul 28, 2026

Copy link
Copy Markdown
Member

Thanks @murwall — the diagnosis here is exactly right, and the register-by-register probe that pinned it to 40288 alone is what made the fix obvious. Carried over verbatim in srcfl/device-drivers#33, with you credited.

Moving it because drivers/ in this repository is a bundled snapshot, not the source. AGENTS.md in device-drivers puts it plainly: that repository is the only editable source for shared drivers and the main driver source for FTW. A change landed here is overwritten by the next publication from the signed channel, so the fix would come back out on its own.

That is not a hypothetical — it is what happened to this exact bug. It was fixed once in srcfl/device-drivers#16 for srcfl/device-drivers#15, then promoting FTW's driver in srcfl/device-drivers#27 overwrote the catalog copy and took the fix with it. The flap you hit is that fix being reverted. The regression test that should have caught it ran the package target, which kept its copy, so nothing went red.

srcfl/device-drivers#33 adds a test against the catalog driver, which is what the signed channel actually publishes, and verifies it against the unpatched baseline: three reads of 40288 over three polls before, one after.

Your live-hardware evidence is the part that PR does not have. If the Pixii unit is still reachable, a note there once the new driver reaches it would close that gap.

Closing in favour of srcfl/device-drivers#33.

tommylindgren pushed a commit to tommylindgren/device-drivers that referenced this pull request Aug 10, 2026
)

Brings the two driver fixes from srcfl/ftw#703 and srcfl/ftw#702 to the repository that owns the drivers. pixii 2.1.0 → 2.1.1, solaredge_legacy 0.3.0 → 0.3.1.

Closes srcfl/ftw#703, closes srcfl/ftw#702
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants