Skip to content

Commit e463c65

Browse files
davmozclaude
andauthored
fix(zap): read PV and battery under data model v2 (3.1.3) (#209)
Zap firmware 7.x calls the PV DER solar, sends power and energy on one side, _ac or _dc, and moves the ratings to /api/devices. read_pv and read_battery therefore read nothing on 7.x. The driver now reads the 1.x and the v2 names, and a value v2 sends as null stays unknown. An AC-side voltage or current is not reported as the battery's own. The inverter DER is not counted as another resource. Also removes draft comments and a pinned version from the meter schema test. Signed-off-by: David Mozart <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d662c3e commit e463c65

11 files changed

Lines changed: 325 additions & 59 deletions

‎CHANGELOG.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
## [Unreleased]
44

5+
### zap 3.1.3
6+
7+
`read_pv` and `read_battery` read again on Zap firmware 7.x. Data model v2 calls
8+
the PV DER `solar`, puts power and energy on one side (`_ac` or `_dc`), and moves
9+
the ratings to `/api/devices`, so 3.1.2 found no PV and no battery power there.
10+
11+
- PV and battery read on both the 1.x and the v2 names. A value v2 sends as
12+
`null` stays unknown.
13+
- An AC-side voltage or current is not reported as the battery's own.
14+
- The `inverter` DER is not counted as another resource, and `ev_charger_port`
15+
counts as a charger.
16+
17+
Checked through FTW's Lua host on firmware 7.0.3, against a real Zap-P1 with a
18+
simulated Solis. Not checked against a real inverter. Without `read_pv` and
19+
`read_battery` the driver still reads the meter and nothing else.
20+
21+
FTW's bundled copy does not move with this commit. Bump the pin and run
22+
`scripts/sync-bundled-drivers.sh`.
23+
524
### zap 3.1.2
625

726
The P1/HAN site meter reads again on Zap firmware serving the data model v2

‎SUPPORT_STATUS.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ Catalog source is not proof that a target can install or run a driver.
180180
| victron_mqtt | 1.0.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
181181
| wallbox | 1.0.3 | ftw-core | not_assessed | — | not_recorded | not_assessed |
182182
| wallbox | 1.0.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
183-
| zap | 3.1.2 | ftw-core | not_assessed | — | not_recorded | not_assessed |
184-
| zap | 3.1.2 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
183+
| zap | 3.1.3 | ftw-core | not_assessed | — | not_recorded | not_assessed |
184+
| zap | 3.1.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
185185
| zaptec | 1.0.1 | ftw-core | not_assessed | — | not_recorded | not_assessed |
186186
| zaptec | 1.0.1 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
187187
| zaptec_cloud | 0.1.1 | ftw-core | not_assessed | — | not_recorded | not_assessed |

‎devices.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,7 @@ manufacturers:
17491749
protocols:
17501750
- protocol: http
17511751
driver: "zap"
1752-
version: "3.1.2"
1752+
version: "3.1.3"
17531753
ders: [meter, pv, battery]
17541754
control: false
17551755
firmware_versions: ""

‎drivers/lua/zap.lua‎

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,25 @@
3232
-- pv: -W generation
3333
-- battery:+W charge, -W discharge
3434
--
35-
-- Meter schema compatibility: some Zap firmware serves the local data model
36-
-- v2 (srcful-data-models 2.3.0, "version":"v2" in /data/json), which renames
37-
-- every meter field with an "_ac" suffix and lowercases the L1/L2/L3 phase
38-
-- prefix (e.g. "L1_V" -> "l1_V_ac", "W" -> "W_ac"; "Hz" is unchanged either
39-
-- way). This driver reads both v1 and v2 field names for the meter DER; see
40-
-- https://github.com/srcfl/device-drivers/issues/170 for the full v2 scope
41-
-- (pv -> solar, battery, DC/AC split), which is not yet covered here.
35+
-- Data model v2: Zap firmware 7.x serves the local API in data model v2
36+
-- ("version":"v2" in /data/json). Older firmware uses the 1.x names. This
37+
-- driver reads both, and a value v2 sends as null stays unknown. v2 moved:
38+
-- meter: W -> W_ac, L1_V -> l1_V_ac, total_import_Wh -> total_import_Wh_ac
39+
-- pv: the DER is "solar"; power and energy are on one side, _ac or _dc;
40+
-- mppt1_V -> mppt[1].V_dc; heatsink_C -> inverter.temperature_C
41+
-- battery: power and energy likewise; V -> V_dc, A -> A_dc;
42+
-- SoC_nom_fract -> soc_nom_fract; heatsink_C -> temperature_C
43+
-- ratings: from /api/devices: rated_power_W_ac on the "inverter" DER,
44+
-- installed_power_W_dc on "solar", rated_power_W_dc and
45+
-- capacity_Wh_dc on "battery"
4246

4347
DRIVER = {
4448
host_api_min = 1,
4549
host_api_max = 1,
4650
id = "zap",
4751
name = "Sourceful Zap (P1/HAN meter)",
4852
manufacturer = "Sourceful",
49-
version = "3.1.2",
53+
version = "3.1.3",
5054
protocols = { "http" },
5155
capabilities = { "meter", "pv", "battery" },
5256
read_only = true,
@@ -57,7 +61,7 @@ DRIVER = {
5761
verification_status = "production",
5862
verified_by = { "erikarenhill@fortytwo:3d" },
5963
verified_at = "2026-07-17",
60-
verification_notes = "P1/HAN live-hardware verified against the official Zap Local API (srcful-zap-x-firmware 96e0258). PV aggregation was live-hardware verified on the same API before the 3.0 meter-only cut; it returns as an operator opt-in. Battery payloads are contract-tested against the firmware serializers. Inverters, batteries and chargers found on Zap are not ingested unless read_pv / read_battery is set.",
64+
verification_notes = "P1/HAN live-hardware verified against the official Zap Local API (srcful-zap-x-firmware 96e0258). PV aggregation was live-hardware verified on the same API before the 3.0 meter-only cut; it returns as an operator opt-in. Battery payloads are contract-tested against the firmware serializers. Inverters, batteries and chargers found on Zap are not ingested unless read_pv / read_battery is set. Data model v2 checked 2026-11-04 on firmware 7.0.3: the meter on a Zap-HAN with a real meter; PV and battery on a Zap-P1 with a simulated Solis, not on a real inverter.",
6165
tested_models = { "Sourceful Zap (ESP32-C3 controller firmware)" },
6266
connection_defaults = {
6367
host = "zap.local",
@@ -98,6 +102,24 @@ local function number(v)
98102
return n
99103
end
100104

105+
local function positive(v)
106+
local n = number(v)
107+
if n ~= nil and n > 0 then return n end
108+
return nil
109+
end
110+
111+
-- Data model v2 sends a power or energy value on one side, _ac or _dc, and the
112+
-- other side null. A 1.x payload has the plain name.
113+
local function sided(raw, name)
114+
return number(raw[name]) or number(raw[name .. "_ac"]) or number(raw[name .. "_dc"])
115+
end
116+
117+
-- A battery's or a string's own voltage and current. An AC-side value is the
118+
-- grid's, not theirs.
119+
local function dc_side(raw, name)
120+
return number(raw[name]) or number(raw[name .. "_dc"])
121+
end
122+
101123
local function cfg_true(v)
102124
return v == true or v == "true" or v == 1 or v == "1"
103125
end
@@ -252,22 +274,29 @@ local function discover_devices()
252274
if der.type == "meter" then
253275
rec.meter = true
254276
if not first_meter then first_meter = sn end
255-
elseif der.type == "pv" then
277+
elseif der.type == "pv" or der.type == "solar" then
256278
if read_pv then
257279
rec.pv = true
258-
rec.pv_rated_w = number(der.rated_power) or number(der.installed_power) or rec.pv_rated_w
280+
rec.pv_rated_w = number(der.rated_power) or number(der.installed_power)
281+
or number(der.installed_power_W_dc) or rec.pv_rated_w
259282
else
260283
add_other(extras, seen, "PV inverter")
261284
end
262285
elseif der.type == "battery" then
263286
if read_battery then
264287
rec.battery = true
265-
rec.battery_rated_w = number(der.rated_power) or rec.battery_rated_w
266-
rec.battery_capacity_wh = number(der.capacity) or rec.battery_capacity_wh
288+
rec.battery_rated_w = number(der.rated_power) or number(der.rated_power_W_dc)
289+
or rec.battery_rated_w
290+
rec.battery_capacity_wh = number(der.capacity) or number(der.capacity_Wh_dc)
291+
or rec.battery_capacity_wh
267292
else
268293
add_other(extras, seen, "battery")
269294
end
270-
elseif der.type == "v2x_charger" or der.type == "ev" then
295+
elseif der.type == "inverter" then
296+
-- The AC stage and rating of a device already
297+
-- listed, not a resource of its own.
298+
rec.inverter_rated_w = positive(der.rated_power_W_ac) or rec.inverter_rated_w
299+
elseif der.type == "v2x_charger" or der.type == "ev" or der.type == "ev_charger_port" then
271300
add_other(extras, seen, "charger")
272301
elseif der.type ~= nil and der.type ~= "" and der.type ~= "meter" then
273302
add_other(extras, seen, tostring(der.type))
@@ -454,6 +483,11 @@ local function snapshot_map()
454483
return out
455484
end
456485

486+
local function mppt_input(pv, index)
487+
local inputs = type(pv.mppt) == "table" and pv.mppt or {}
488+
return type(inputs[index]) == "table" and inputs[index] or {}
489+
end
490+
457491
local function emit_pv(snapshots)
458492
local source_count = count_kind("pv")
459493
if source_count == 0 then return end
@@ -467,14 +501,14 @@ local function emit_pv(snapshots)
467501

468502
for _, rec in ipairs(tracked) do
469503
local data = snapshots[rec.sn]
470-
local pv = data and data.pv
504+
local pv = data and (data.pv or data.solar)
471505
if rec.pv and type(pv) == "table" then
472-
local rated = number(pv.rated_power_W) or rec.pv_rated_w
506+
local rated = number(pv.rated_power_W) or rec.inverter_rated_w or rec.pv_rated_w
473507
if rated and rated > 0 then
474508
total_rated_w = total_rated_w + rated
475509
any_rating = true
476510
end
477-
local w = sane_power(pv.W, rated)
511+
local w = sane_power(sided(pv, "W"), rated)
478512
if w ~= nil and w <= 0 then
479513
total_w = total_w + w
480514
any_power = true
@@ -485,18 +519,20 @@ local function emit_pv(snapshots)
485519
host.log("warn", "Zap: rejected positive PV power for " .. rec.sn .. ": " .. w .. "W")
486520
end
487521

488-
local generation = bounded(pv.total_generation_Wh, 0, nil)
522+
local generation = bounded(sided(pv, "total_generation_Wh"), 0, nil)
489523
if generation ~= nil then
490524
total_generation_wh = total_generation_wh + generation
491525
any_generation = true
492526
end
493527

494528
local tag = source_count > 1 and ("_" .. metric_tag(rec.sn)) or ""
495-
emit_optional_metric("pv_heatsink_c" .. tag, number(pv.heatsink_C), "°C")
496-
emit_optional_metric("pv_mppt1_v" .. tag, number(pv.mppt1_V), "V")
497-
emit_optional_metric("pv_mppt1_a" .. tag, number(pv.mppt1_A), "A")
498-
emit_optional_metric("pv_mppt2_v" .. tag, number(pv.mppt2_V), "V")
499-
emit_optional_metric("pv_mppt2_a" .. tag, number(pv.mppt2_A), "A")
529+
local inverter = type(data.inverter) == "table" and data.inverter or {}
530+
local mppt1, mppt2 = mppt_input(pv, 1), mppt_input(pv, 2)
531+
emit_optional_metric("pv_heatsink_c" .. tag, number(pv.heatsink_C) or number(inverter.temperature_C), "°C")
532+
emit_optional_metric("pv_mppt1_v" .. tag, number(pv.mppt1_V) or number(mppt1.V_dc), "V")
533+
emit_optional_metric("pv_mppt1_a" .. tag, number(pv.mppt1_A) or number(mppt1.A_dc), "A")
534+
emit_optional_metric("pv_mppt2_v" .. tag, number(pv.mppt2_V) or number(mppt2.V_dc), "V")
535+
emit_optional_metric("pv_mppt2_a" .. tag, number(pv.mppt2_A) or number(mppt2.A_dc), "A")
500536
end
501537
end
502538

@@ -544,15 +580,15 @@ local function emit_battery(snapshots)
544580
total_rated_w = total_rated_w + rated
545581
any_rating = true
546582
end
547-
local w = sane_power(battery.W, rated)
583+
local w = sane_power(sided(battery, "W"), rated)
548584
if w ~= nil then
549585
total_w = total_w + w
550586
any_power = true
551587
end
552588

553589
-- Firmware SoC_nom_fract is 0-1. Identifier keeps the
554590
-- catalog check that soc is already a fraction, not percent.
555-
local soc_already_fract = bounded(battery.SoC_nom_fract, 0, 1)
591+
local soc_already_fract = bounded(number(battery.SoC_nom_fract) or number(battery.soc_nom_fract), 0, 1)
556592
local soc = soc_already_fract
557593
if soc ~= nil then
558594
soc_sum = soc_sum + soc
@@ -565,17 +601,17 @@ local function emit_battery(snapshots)
565601
end
566602
end
567603

568-
local charge = bounded(battery.total_charge_Wh, 0, nil)
604+
local charge = bounded(sided(battery, "total_charge_Wh"), 0, nil)
569605
if charge ~= nil then total_charge_wh = total_charge_wh + charge; any_charge_energy = true end
570-
local discharge = bounded(battery.total_discharge_Wh, 0, nil)
606+
local discharge = bounded(sided(battery, "total_discharge_Wh"), 0, nil)
571607
if discharge ~= nil then total_discharge_wh = total_discharge_wh + discharge; any_discharge_energy = true end
572608

573609
local tag = source_count > 1 and ("_" .. metric_tag(rec.sn)) or ""
574610
if source_count > 1 and w ~= nil then host.emit_metric("battery_w" .. tag, w, "W") end
575611
if source_count > 1 and soc ~= nil then host.emit_metric("battery_soc" .. tag, soc, "fraction") end
576-
emit_optional_metric("battery_dc_v" .. tag, number(battery.V), "V")
577-
emit_optional_metric("battery_dc_a" .. tag, number(battery.A), "A")
578-
emit_optional_metric("battery_temp_c" .. tag, number(battery.heatsink_C), "°C")
612+
emit_optional_metric("battery_dc_v" .. tag, dc_side(battery, "V"), "V")
613+
emit_optional_metric("battery_dc_a" .. tag, dc_side(battery, "A"), "A")
614+
emit_optional_metric("battery_temp_c" .. tag, number(battery.heatsink_C) or number(battery.temperature_C), "°C")
579615
end
580616
end
581617

@@ -600,9 +636,9 @@ local function emit_battery(snapshots)
600636
if any_rating then battery_reading.rated_w = total_rated_w end
601637

602638
if source_count == 1 and single then
603-
battery_reading.v = number(single.V)
604-
battery_reading.a = number(single.A)
605-
battery_reading.temp_c = number(single.heatsink_C)
639+
battery_reading.v = dc_side(single, "V")
640+
battery_reading.a = dc_side(single, "A")
641+
battery_reading.temp_c = number(single.heatsink_C) or number(single.temperature_C)
606642
end
607643

608644
host.emit("battery", battery_reading)

‎drivers/tests/lua_harness/test_zap_meter_schema.lua‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
-- Exercise zap.lua's meter field reading against both the v1 and v2 Zap
22
-- local API field names. Run with the scenario as arg[1]: "v1" or "v2".
3-
--
4-
-- This file is a draft for drivers/tests/lua_harness/test_zap_meter_schema.lua
5-
-- in srcfl/device-drivers — see pr-drafts/README.md for how this fits that
6-
-- repo's `make test-driver ID=zap` harness. Not run against the real harness
7-
-- yet (no local checkout with lua55/pytest); matches the structure of the
8-
-- existing drivers/tests/lua_harness/test_esphome_dsmr_aliases.lua, which
9-
-- solves the same kind of field-name-aliasing problem.
103

114
local scenario = arg[1]
125
if scenario ~= "v1" and scenario ~= "v2" then
@@ -50,8 +43,8 @@ end
5043

5144
dofile("drivers/lua/zap.lua")
5245

53-
if DRIVER.id ~= "zap" or DRIVER.version ~= "3.1.2" then
54-
error("Zap identity metadata is wrong or version was not bumped")
46+
if DRIVER.id ~= "zap" then
47+
error("Zap identity metadata is wrong")
5548
end
5649
if DRIVER.host_api_min ~= 1 or DRIVER.host_api_max ~= 1 or DRIVER.read_only ~= true then
5750
error("Zap host API or read-only metadata is wrong")

0 commit comments

Comments
 (0)