Skip to content

drivers: add Ferroamp Modbus driver - #31

Merged
frahlg merged 1 commit into
masterfrom
worktree-agent-a2cbc63e
Apr 15, 2026
Merged

frahlg merged 1 commit into
masterfrom
worktree-agent-a2cbc63e

Conversation

@frahlg

@frahlg frahlg commented Apr 15, 2026

Copy link
Copy Markdown
Member

Summary

  • Ports the hugin Ferroamp Modbus TCP driver to 42W's v2.1 Lua host as drivers/ferroamp_modbus.lua.
  • Gives us an alternative transport to drivers/ferroamp.lua (MQTT) for sites where the Ferroamp external MQTT API isn't exposed — same meter + pv + battery capabilities, with control.
  • DRIVER.id = "ferroamp-modbus" so both drivers can coexist in the catalog.

Port details

  • API drift (hugin → 42W v2.1): host.log(msg) → host.log("info", msg); host.modbus_write_multiple(…) → host.modbus_write_multi(…); no host.decode_f32 in 42W so decode_f32_be + word-swap decoder + encode_f32_ws are inlined in Lua (same pattern as sdm630.lua / fronius.lua).
  • Site convention: Ferroamp reports battery power as positive = discharging. The driver negates at the boundary so everything above it uses the unified positive = charging convention — matches drivers/ferroamp.lua.
  • Diagnostics: every per-phase meter channel and grid frequency is also routed through host.emit_metric into the long-format TSDB.
  • Watchdog safety: driver_default_mode wraps the Modbus write in pcall — the watchdog often fires precisely because Modbus is unreachable, and an unhandled write error would crash the VM.
  • Control: battery, curtail, curtail_disable, deinit all preserved from hugin.

Test plan

  • go test ./internal/drivers/ — 14.3 s, all green including the new smoke test
  • go test ./... — full suite green including go/test/e2e
  • New TestFerroampModbusLoads exercises init → poll → all four command actions → default_mode with no Modbus capability attached; the driver's pcall guards swallow the capability errors gracefully.
  • New TestFerroampModbusCatalogEntry confirms the DRIVER = {…} block parses cleanly and advertises the expected id / manufacturer / protocols / capabilities.
  • Live test against a real Ferroamp EnergyHub XL over Modbus TCP (not yet — owner of device to verify on hardware).

🤖 Generated with Claude Code

Port of sourceful-hugin/device-support/drivers/lua/ferroamp_modbus.lua
to the 42W v2.1 Lua host. Enables Ferroamp EnergyHub control on sites
where MQTT isn't exposed.

- DRIVER.id = "ferroamp-modbus" (distinct from the MQTT "ferroamp")
- protocols=["modbus"], caps=meter+pv+battery, with control
- Inlines decode_f32_be + encode_f32_ws (word-swapped) since the host
  has no f32 helpers
- API-drift fixes vs hugin: host.log("info", …), modbus_write_multi
- Site convention: flips Ferroamp's positive-discharging battery sign
  to positive-charging at the driver boundary (matches ferroamp.lua)
- driver_default_mode wraps modbus_write in pcall so the watchdog
  can't crash the VM when the device is unreachable
- Smoke test + catalog-parse assertion in ferroamp_modbus_test.go

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 21a4265591

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +268 to +270
local ref_kw = -power_w / 1000
host.modbus_write_multi(6064, encode_f32_ws(ref_kw))
host.modbus_write(6000, 1) -- power mode

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Treat zero battery setpoint as auto mode

The control loop sends {"action":"battery","power_w":...} on every cycle (go/cmd/forty-two-watts/main.go), and power_w is frequently 0 when a battery should idle. This branch currently converts zero to ref_kw = 0 and forces register 6000 to mode 1 (power mode), so the inverter is never returned to self-consumption for zero commands; that contradicts the documented command contract (docs/writing-a-driver.md, power_w = 0 means revert to self-consumption) and can leave the device pinned in forced mode.

Useful? React with 👍 / 👎.

Comment on lines +239 to +242
host.emit("battery", {
w = bat_w,
soc = bat_soc,
charge_wh = bat_charge_wh,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Omit SoC on read failure instead of emitting 0

If the SoC Modbus read fails, bat_soc stays at 0 and this emit still publishes soc = 0, which turns transient comms failures into false 0% battery events. The telemetry store only preserves last-known SoC when the field is missing (soc == nil), so always emitting zero here can corrupt control decisions and historical data; emit soc only when the register read succeeds.

Useful? React with 👍 / 👎.

@frahlg
frahlg merged commit 03b802c into master Apr 15, 2026
github-actions Bot pushed a commit to erikarenhill/forty-two-watts that referenced this pull request Apr 15, 2026
## 1.0.0 (2026-04-15)

### Bug Fixes

* **ci:** switch semantic-release to conventionalcommits preset ([15290c7](15290c7))
* Lua driver Command() reading wrong field — Sungrow ignored targets ([9237156](9237156))

### Drivers

* add Eastron SDM630 Lua driver ([srcfl#18](https://github.com/erikarenhill/forty-two-watts/issues/18)) ([d5ad806](d5ad806))
* add Ferroamp Modbus driver (alt transport to ferroamp.lua) ([srcfl#31](https://github.com/erikarenhill/forty-two-watts/issues/31)) ([03b802c](03b802c))
* port Deye SUN-SG hybrid inverter to 42W v2.1 Lua host ([srcfl#29](https://github.com/erikarenhill/forty-two-watts/issues/29)) ([df8fbc0](df8fbc0))
* port Fronius GEN24 (SunSpec) to Lua ([srcfl#19](https://github.com/erikarenhill/forty-two-watts/issues/19)) ([c1fc875](c1fc875))
* port Fronius Smart Meter (SunSpec Modbus, read-only) ([srcfl#24](https://github.com/erikarenhill/forty-two-watts/issues/24)) ([575895c](575895c))
* port GoodWe hybrid inverter (ET-Plus / EH) to Lua v2.1 ([srcfl#28](https://github.com/erikarenhill/forty-two-watts/issues/28)) ([e43d2d9](e43d2d9))
* port Growatt hybrid inverter driver (read-only) ([srcfl#20](https://github.com/erikarenhill/forty-two-watts/issues/20)) ([92524ac](92524ac))
* port Huawei SUN2000 hybrid inverter ([srcfl#15](https://github.com/erikarenhill/forty-two-watts/issues/15)) ([09a8855](09a8855))
* port Kostal Plenticore / Piko IQ (Lua, read-only) ([srcfl#21](https://github.com/erikarenhill/forty-two-watts/issues/21)) ([bdeca96](bdeca96))
* port Pixii PowerShaper battery driver to v2.1 Lua host ([srcfl#22](https://github.com/erikarenhill/forty-two-watts/issues/22)) ([70a96d1](70a96d1))
* port SMA hybrid inverter Lua driver ([srcfl#23](https://github.com/erikarenhill/forty-two-watts/issues/23)) ([dd34555](dd34555))
* port Sofar HYD-ES/HYD-EP from hugin to Lua v2.1 ([srcfl#26](https://github.com/erikarenhill/forty-two-watts/issues/26)) ([14f6131](14f6131))
* port SolarEdge SunSpec inverter + meter to Lua (read-only) ([srcfl#30](https://github.com/erikarenhill/forty-two-watts/issues/30)) ([1007e63](1007e63))
* port Solis hybrid inverter ([srcfl#27](https://github.com/erikarenhill/forty-two-watts/issues/27)) ([98b2a50](98b2a50))
* port Victron Energy GX Modbus driver ([srcfl#25](https://github.com/erikarenhill/forty-two-watts/issues/25)) ([ad71db2](ad71db2))

### UI

* inline target on hover + driver card + collapsible model cards ([de88f43](de88f43))
* legend wrap + nice-tick y-axis + cleaner chart labels ([srcfl#33](https://github.com/erikarenhill/forty-two-watts/issues/33)) ([aeb1d1c](aeb1d1c))
* live version from API + no-cache headers on static assets ([7b8779b](7b8779b))

### Control loop

* fold live DerEV readings into the EV clamp ([srcfl#36](https://github.com/erikarenhill/forty-two-watts/issues/36)) ([5d57d68](5d57d68))

### MPC planner

* fall back to forecast when learned PV twin collapses ([srcfl#39](https://github.com/erikarenhill/forty-two-watts/issues/39)) ([f3062ac](f3062ac))

### Telemetry

* add DerEV type for EV charger readings ([srcfl#34](https://github.com/erikarenhill/forty-two-watts/issues/34)) ([65c9e2c](65c9e2c))

### TSDB

* long-format SQLite (14d) + Parquet rolloff for older ([c53c964](c53c964))

### Safety

* watchdog marks stale drivers offline + reverts to autonomous ([519196c](519196c))
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 2.0.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

github-actions Bot pushed a commit that referenced this pull request Apr 16, 2026
## [0.4.0](v0.3.0...v0.4.0) (2026-04-16)

### Features

* config/UI improvements — kWh display, secure EV password, planner tab ([#65](#65)) ([35ab03d](35ab03d))
* Easee Cloud driver + host.http_get/post for Lua drivers ([#56](#56)) ([4cdc942](4cdc942))
* EV charger config + credential masking in API responses ([#58](#58)) ([c22cb80](c22cb80))

### Bug Fixes

* 5 Go-side P1 bugs from Codex review ([#46](#46)) ([0cd2885](0cd2885))
* 5 Go-side P1 bugs from Codex review ([#47](#47)) ([4f2eaf6](4f2eaf6))
* address P2 review comments across control, MPC, drivers, and UI ([#64](#64)) ([fcafa88](fcafa88))
* **ci:** disable @semantic-release/github PR annotation features ([4020d46](4020d46)), closes [#32](#32) [#33](#33) [#34](#34) [#35](#35) [#36](#36) [#39](#39)
* **ci:** switch semantic-release to conventionalcommits preset ([7e0bb89](7e0bb89))
* **ci:** upgrade GitHub Actions to Node.js 24 (drop deprecated Node 20) ([4005bd8](4005bd8))
* Lua driver Command() reading wrong field — Sungrow ignored targets ([9237156](9237156))
* populate EV Charger tab from driver config when ev_charger is empty ([5e6b116](5e6b116))
* remove dead evSlider event listeners that crash app.js ([8ae76c7](8ae76c7))
* replace wonky Catmull-Rom spline with simple linear forecast ([abea431](abea431))
* show '...' instead of stale v0.1.0 while JS loads version ([dc65065](dc65065))
* **solaredge_pv:** read SunSpec scale factors every poll, not cached ([#38](#38)) ([26f8793](26f8793))

### Drivers

* add Eastron SDM630 Lua driver ([#18](#18)) ([d5ad806](d5ad806))
* add Ferroamp Modbus driver (alt transport to ferroamp.lua) ([#31](#31)) ([03b802c](03b802c))
* fix 9 P1 bugs flagged by Codex review ([#44](#44)) ([b20e485](b20e485))
* port Deye SUN-SG hybrid inverter to 42W v2.1 Lua host ([#29](#29)) ([df8fbc0](df8fbc0))
* port Fronius GEN24 (SunSpec) to Lua ([#19](#19)) ([c1fc875](c1fc875))
* port Fronius Smart Meter (SunSpec Modbus, read-only) ([#24](#24)) ([575895c](575895c))
* port GoodWe hybrid inverter (ET-Plus / EH) to Lua v2.1 ([#28](#28)) ([e43d2d9](e43d2d9))
* port Growatt hybrid inverter driver (read-only) ([#20](#20)) ([92524ac](92524ac))
* port Huawei SUN2000 hybrid inverter ([#15](#15)) ([09a8855](09a8855))
* port Kostal Plenticore / Piko IQ (Lua, read-only) ([#21](#21)) ([bdeca96](bdeca96))
* port Pixii PowerShaper battery driver to v2.1 Lua host ([#22](#22)) ([70a96d1](70a96d1))
* port SMA hybrid inverter Lua driver ([#23](#23)) ([dd34555](dd34555))
* port Sofar HYD-ES/HYD-EP from hugin to Lua v2.1 ([#26](#26)) ([14f6131](14f6131))
* port SolarEdge SunSpec inverter + meter to Lua (read-only) ([#30](#30)) ([1007e63](1007e63))
* port Solis hybrid inverter ([#27](#27)) ([98b2a50](98b2a50))
* port Victron Energy GX Modbus driver ([#25](#25)) ([ad71db2](ad71db2))

### UI

* add status bar with driver health indicators ([b048d60](b048d60))
* EV status card + Easee control commands ([#59](#59)) ([b03749a](b03749a))
* fix summary cards grid for 7 cards + raise side-by-side breakpoint ([6e19973](6e19973))
* inline target on hover + driver card + collapsible model cards ([de88f43](de88f43))
* legend wrap + nice-tick y-axis + cleaner chart labels ([#33](#33)) ([aeb1d1c](aeb1d1c))
* remove manual EV charging slider ([063174c](063174c))
* show mode band in plan chart + grid target on status card ([877e0bd](877e0bd))
* smooth Catmull-Rom spline for forecast + 15min forecast zone ([dba51a5](dba51a5))

### Control loop

* fold live DerEV readings into the EV clamp ([#36](#36)) ([5d57d68](5d57d68))
* slew-rate anchors on actual battery power, not stale command ([#41](#41)) ([4f73f19](4f73f19))

### MPC planner

* fall back to forecast when learned PV twin collapses ([#39](#39)) ([f3062ac](f3062ac))
* log optimize params + ems_mode per action for plan chart ([9e8c14b](9e8c14b))
* value SoC at import−export spread in self-consumption modes ([#40](#40)) ([a90d525](a90d525))

### Telemetry

* add DerEV type for EV charger readings ([#34](#34)) ([65c9e2c](65c9e2c))

### TSDB

* long-format SQLite (14d) + Parquet rolloff for older ([c53c964](c53c964))

### Safety

* watchdog marks stale drivers offline + reverts to autonomous ([519196c](519196c))
@frahlg
frahlg deleted the worktree-agent-a2cbc63e branch August 7, 2026 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant