Skip to content

Commit 28c771f

Browse files
frahlgclaude
andauthored
fix(zaptec_cloud): start one-phase cars, hand the charger back, give the cloud time (0.3.0) (#279)
* fix(zaptec_cloud): start one-phase cars, hand the charger back, give the cloud time (0.3.0) First field test (Zaptec Go, 20 A fuse, two one-phase cars) could not charge. - A one-phase order on an idle three-phase charger was divided over three phases, so up to 18 A came out under 6 A and was written as 0 A. The driver now offers the charger's minimum when Core asks for one phase and the charger draws on none, as a resume does. - Every stop left 0 A stored on the charger ("Max Current lower than Min Current"). driver_release puts back the owner's maximum, or 32 A, and resumes a charger FTW paused, when the owner removes the driver. - Zaptec Cloud is slow: ask for 20 s per call and retry once after a server error or a lost connection. - Read the charger's minimum (511) and never write a positive current below it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Fredrik Ahlgren <[email protected]> * fix(zaptec_cloud): keep FTW's pause and the charger minimum through odd replies Review findings on 0.3.0: - A pause whose first reply was lost and whose retry got 528 did not count as FTW's, so a removal left the charger paused. Any pause that held now counts. - A poll that still showed charging just after FTW's pause cleared it. Only a resume clears FTW's pause now. - A state document without 511 reset the minimum to 6 A, letting a write fall below a higher minimum. The last known value stays. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Fredrik Ahlgren <[email protected]> --------- Signed-off-by: Fredrik Ahlgren <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b234721 commit 28c771f

12 files changed

Lines changed: 363 additions & 30 deletions

‎CHANGELOG.md‎

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

33
## [Unreleased]
44

5+
### zaptec_cloud 0.3.0
6+
7+
First field test (Zaptec Go, 20 A main fuse, two one-phase cars) found
8+
three faults that kept the car from charging:
9+
10+
- A one-phase order on an idle three-phase charger was divided over three
11+
phases, so anything up to 18 A came out below 6 A and was written as 0 A.
12+
When Core asks for one phase, the charger draws on none and its state is
13+
fresh, the driver now offers the charger's minimum, as a resume does. Once
14+
the car draws on one phase it gets its full one-phase current. A car seen
15+
on three phases never gets this start.
16+
- The driver wrote 0 A on every stop and left it on the charger. The owner
17+
found "Max Current lower than Min Current" in the Zaptec portal, and
18+
neither the Zaptec app nor another load balancer could start the charger.
19+
The new `driver_release` puts back the maximum found before FTW's first
20+
write, or 32 A when that is unknown, and resumes a charger FTW paused.
21+
Core calls it when the owner removes or disables the driver
22+
(srcfl/ftw, paired PR); a restart still stands down at 0 A.
23+
- Zaptec Cloud calls took longer than the host's 5 s. The driver asks for
24+
20 s per call with `host.set_command_timeout_s` and retries a request once
25+
after a server error or a lost connection.
26+
27+
The driver also reads the charger's own minimum (observation 511) and never
28+
writes a positive current below it.
29+
530
### easee_cloud 1.5.0
631

732
Expose the actual pilot current and its source/change timestamp separately

‎SUPPORT_STATUS.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Catalog source is not proof that a target can install or run a driver.
182182
| wallbox | 1.1.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
183183
| zap | 3.2.0 | ftw-core | not_assessed | — | not_recorded | not_assessed |
184184
| zap | 3.2.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
185-
| zaptec_cloud | 0.2.2 | ftw-core | not_assessed | — | not_recorded | not_assessed |
186-
| zaptec_cloud | 0.2.2 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
185+
| zaptec_cloud | 0.3.0 | ftw-core | not_assessed | — | not_recorded | not_assessed |
186+
| zaptec_cloud | 0.3.0 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
187187
| zuidwijk_p1 | 1.1.2 | ftw-core | not_assessed | — | not_recorded | not_assessed |
188188
| zuidwijk_p1 | 1.1.2 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |

‎devices.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2114,7 +2114,7 @@ manufacturers:
21142114
protocols:
21152115
- protocol: http
21162116
driver: "zaptec_cloud"
2117-
version: "0.2.2"
2117+
version: "0.3.0"
21182118
ders: [ev]
21192119
control: true
21202120
firmware_versions: ""

‎docs/WRITING-A-DRIVER.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,22 @@ changelog, the API reference you decoded the device from — record those in the
3131
manifest so the driver can be checked against them again later. See
3232
[Record what you decoded it from](#record-what-you-decoded-it-from).
3333

34-
## The five entry points
34+
## The entry points
3535

3636
| Function | When |
3737
|---|---|
3838
| `driver_init(config)` | once, before the first poll |
3939
| `driver_poll()` | repeatedly; returns milliseconds until the next call |
4040
| `driver_command(action, power_w, cmd)` | when the host wants the device to act |
4141
| `driver_default_mode()` | hand the device back to itself |
42+
| `driver_release()` | optional: the owner removed the driver; give back their settings |
4243
| `driver_cleanup()` | once, when the driver stops |
4344

45+
A safe stop that stays on the device, such as a charger held at 0 A, must not
46+
outlive FTW. `driver_release()` puts the owner's setting back; `zaptec_cloud`
47+
is the worked example. A driver whose vendor cloud is slow asks for more time
48+
per call with `host.set_command_timeout_s(seconds)` in `driver_init`.
49+
4450
Only `driver_poll` is required. A read-only driver defines the first two and
4551
stops there — and most drivers should. A driver that cannot write cannot break
4652
a customer's site.

‎drivers/lua/zaptec_cloud.lua‎

Lines changed: 126 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,35 @@
2222
-- on exactly one. A single-phase car can then only draw less than Core
2323
-- budgeted, never more. With no cap in an order it keeps Core's last one.
2424
--
25+
-- One exception lets a single-phase car start. Below the charger's minimum
26+
-- (observation 511, usually 6 A) Zaptec allocates nothing, so a one-phase
27+
-- order of up to 18 A on an idle three-phase charger came out as 0 A and the
28+
-- car never started. When Core asks for one phase, the charger draws on no
29+
-- phase, its state is fresh and the order is at least the minimum on one
30+
-- phase, the driver offers that minimum, as a resume does. Once the car draws
31+
-- on one phase, the next order gets its full one-phase current.
32+
--
33+
-- The driver never writes a positive current below the charger's minimum.
34+
-- Zaptec documents that a maximum below the minimum allocates no current; an
35+
-- owner saw that as "Max Current lower than Min Current" in the portal.
36+
--
2537
-- Stand-alone mode (observation 712) makes the charger ignore current set
2638
-- through the API. The driver then refuses current orders and says why.
2739
--
2840
-- When FTW can no longer steer (driver_default_mode) it pauses and sets
2941
-- 0 A, as easee_cloud does, but only once FTW has written a current in this
30-
-- run. maxChargeCurrent is stored on the charger, so an owner who stops
31-
-- using FTW sets it again in the Zaptec app or portal.
42+
-- run. maxChargeCurrent is stored on the charger. Core runs driver_release
43+
-- instead when the owner removes or disables this driver: the driver puts
44+
-- back the maximum it found before FTW's first write in this run, or 32 A
45+
-- (no charger cap; the installation's own limits still apply) when that is
46+
-- unknown or below the minimum, and resumes a charger FTW paused. Without
47+
-- that, the charger stayed at 0 A and neither the Zaptec app nor another
48+
-- load balancer could start it.
49+
--
50+
-- Zaptec Cloud often takes a few seconds per call. The driver asks the host
51+
-- for 20 s per call (host.set_command_timeout_s), and retries a request once
52+
-- after a server error or a lost connection. Both writes are idempotent: a
53+
-- repeated command that changes nothing comes back 528.
3254
--
3355
-- Sign convention: positive W is charging (power into the vehicle).
3456
--
@@ -45,7 +67,7 @@ DRIVER = {
4567
id = "zaptec_cloud",
4668
name = "Zaptec Cloud",
4769
manufacturer = "Zaptec",
48-
version = "0.2.2",
70+
version = "0.3.0",
4971
protocols = { "http" },
5072
capabilities = { "ev" },
5173
description = "Zaptec Go / Go 2 / Pro via Zaptec Cloud. Email + password; optional charger serial. FTW sets the charging current and pauses or resumes.",
@@ -71,6 +93,7 @@ local OBS_CURRENT_L1 = 507
7193
local OBS_CURRENT_L2 = 508
7294
local OBS_CURRENT_L3 = 509
7395
local OBS_MAX_CURRENT = 510
96+
local OBS_MIN_CURRENT = 511
7497
local OBS_CHARGE_POWER = 513
7598
local OBS_SESSION_ENERGY = 553
7699
local OBS_OP_MODE = 710
@@ -91,10 +114,17 @@ local CMD_RESUME = 507
91114
-- command never reached the charger, so it is a failure.
92115
local CODE_REJECTED = 528
93116

94-
-- Charger limits. Below MIN_A a charger allocates no current, so the driver
95-
-- writes 0 rather than rounding a small order up.
117+
-- Charger limits. Below its minimum a charger allocates no current, so the
118+
-- driver writes 0 rather than rounding a small order up. MIN_A is Zaptec's
119+
-- default minimum; a charger that reports a higher one (511) uses that.
96120
local MIN_A = 6
97121
local MAX_A = 32
122+
-- What driver_release writes when the owner's own maximum is unknown: no cap
123+
-- on the charger, so the installation's limits govern as before FTW.
124+
local HANDBACK_A = 32
125+
-- Time per driver call asked of the host, and the wait before one retry.
126+
local COMMAND_TIMEOUT_S = 20
127+
local RETRY_WAIT_MS = 1000
98128
-- A live phase counts when it carries at least this much current.
99129
local LIVE_PHASE_A = 1.0
100130
-- Rewrite an unchanged current at most this often; small rises wait for it.
@@ -120,13 +150,20 @@ local stand_alone = false
120150
local op_mode = nil
121151
local OBS_FINAL_STOP = 718
122152
local phase_currents = { nil, nil, nil }
153+
local state_fresh = false -- the last state poll succeeded
154+
local charger_min_a = nil -- observation 511
155+
local charger_max_a = nil -- observation 510
156+
local final_stop = false -- observation 718
157+
local owner_max_a = nil -- 510 before FTW's first write in this run
158+
local warned_min = false
123159
local variant_logged = false
124160

125161
-- What the driver last wrote.
126162
local last_written_a = nil -- nil until the first write this run
127163
local last_write_ms = 0
128164
local last_cap_a = nil -- Core's last max_amps_per_phase
129165
local paused = false -- the charger is paused (506 or the poll says so)
166+
local ftw_paused = false -- FTW sent a 506 that held; only a resume clears it
130167
local resume_try_ms = nil
131168
local stood_down = false
132169
local stand_down_try_ms = nil
@@ -335,6 +372,11 @@ local function post(path, body, content_type)
335372
access_token = nil
336373
token_expiry = 0
337374
if ensure_auth() then err = once() end
375+
elseif err and (tostring(err):match("^HTTP 5%d%d") or not tostring(err):match("^HTTP %d")) then
376+
-- A server error or a lost connection: Zaptec Cloud has these now and
377+
-- then. Try once more; both writes are safe to repeat.
378+
host.sleep(RETRY_WAIT_MS)
379+
err = once()
338380
end
339381
return err
340382
end
@@ -384,22 +426,40 @@ local function live_phase_count()
384426
return n
385427
end
386428

429+
-- floor_a is the least current the charger allocates: its own minimum
430+
-- (511) when it reports one at or above Zaptec's 6 A default, else 6 A.
431+
local function floor_a()
432+
if charger_min_a and charger_min_a > MIN_A and charger_min_a <= MAX_A then
433+
return charger_min_a
434+
end
435+
return MIN_A
436+
end
437+
387438
-- amps_for turns Core's watts into amps per phase, never above Core's cap.
388439
-- It divides by three phases unless the installation has one phase and the
389440
-- charger is not drawing on more, or Core asks for one and the charger is
390441
-- drawing on exactly one. An idle charger
391-
-- proves nothing, so a three-phase car can never get a one-phase sum.
442+
-- proves nothing, so a three-phase car can never get a one-phase sum. It
443+
-- can still start a one-phase car at the minimum; see the header.
392444
local function amps_for(power_w, cmd, cap)
393445
local voltage = as_number(cmd.voltage)
394446
if not voltage or voltage <= 0 then voltage = 230 end
395447
local mode = type(cmd.phase_mode) == "string" and cmd.phase_mode or ""
396448
local phases = 3
397449
local live = live_phase_count()
398450
if (phases_cfg == 1 and live <= 1) or (mode == "1p" and live == 1) then phases = 1 end
399-
local amps = (as_number(power_w) or 0) / (voltage * phases)
451+
local w = as_number(power_w) or 0
452+
local amps = w / (voltage * phases)
400453
if cap and amps > cap then amps = cap end
401454
if amps > MAX_A then amps = MAX_A end
402-
if amps < MIN_A then return 0 end
455+
local floor = floor_a()
456+
if amps < floor then
457+
if phases == 3 and mode == "1p" and live == 0 and state_fresh
458+
and w / voltage >= floor and (cap == nil or cap >= floor) then
459+
return floor
460+
end
461+
return 0
462+
end
403463
return math.floor(amps * 10 + 1e-9) / 10
404464
end
405465

@@ -417,7 +477,7 @@ end
417477
local function send_resume()
418478
resume_try_ms = host.millis()
419479
local ok = send_command(CMD_RESUME)
420-
if ok then paused = false end
480+
if ok then paused = false; ftw_paused = false end
421481
return ok
422482
end
423483

@@ -445,7 +505,7 @@ local function set_current(power_w, cmd)
445505
end
446506
end
447507
if write then wrote = write_current(amps) end
448-
if amps < MIN_A or not wrote then return wrote end
508+
if amps < floor_a() or not wrote then return wrote end
449509
if paused and (resume_try_ms == nil or now - resume_try_ms >= RESUME_RETRY_MS) then
450510
return send_resume()
451511
end
@@ -456,6 +516,9 @@ local function pause()
456516
local wrote = write_current(0)
457517
local sent, applied = send_command(CMD_PAUSE)
458518
if applied then paused = true end
519+
-- A 528 can be the answer to a retry whose first try already paused the
520+
-- charger, so any pause that held counts as FTW's.
521+
if sent then ftw_paused = true end
459522
-- 0 A alone is enough: below 6 A the charger allocates nothing.
460523
return wrote or sent
461524
end
@@ -464,9 +527,10 @@ end
464527
-- than Core's last cap, and Core's next order sets the level.
465528
local function resume()
466529
if stand_alone then return refuse_stand_alone() end
467-
if (last_written_a == nil or last_written_a < MIN_A)
468-
and (last_cap_a == nil or last_cap_a >= MIN_A) then
469-
if not write_current(MIN_A) then return false end
530+
local floor = floor_a()
531+
if (last_written_a == nil or last_written_a < floor)
532+
and (last_cap_a == nil or last_cap_a >= floor) then
533+
if not write_current(floor) then return false end
470534
end
471535
-- Always send the command: the last poll may predate a pause.
472536
return send_resume()
@@ -486,6 +550,10 @@ function driver_init(config)
486550
end
487551
local p = as_number(config.phases)
488552
if p == 1 or p == 3 then phases_cfg = p end
553+
-- Older hosts have no set_command_timeout_s and keep their 5 s.
554+
if type(host.set_command_timeout_s) == "function" then
555+
host.set_command_timeout_s(COMMAND_TIMEOUT_S)
556+
end
489557

490558
if not email or not password then
491559
error("Zaptec: email/username and password required")
@@ -528,15 +596,33 @@ function driver_poll()
528596
auth_headers())
529597
if err then
530598
phase_currents = { nil, nil, nil }
599+
state_fresh = false
531600
host.log("warn", "Zaptec: state poll failed: " .. redact_http_err(err))
532601
return 10000
533602
end
534603
local obs, oerr = observation_map(resp)
535604
if oerr or not obs then
536605
phase_currents = { nil, nil, nil }
606+
state_fresh = false
537607
host.log("warn", "Zaptec: state decode failed")
538608
return 10000
539609
end
610+
state_fresh = true
611+
-- A state document that leaves one out keeps the last known value: losing
612+
-- the minimum would let a write fall below it.
613+
charger_max_a = as_number(obs[OBS_MAX_CURRENT]) or charger_max_a
614+
charger_min_a = as_number(obs[OBS_MIN_CURRENT]) or charger_min_a
615+
final_stop = as_flag(obs[OBS_FINAL_STOP]) == true
616+
if charger_min_a and charger_min_a > MIN_A and not warned_min then
617+
warned_min = true
618+
host.log("info", string.format(
619+
"Zaptec: the charger's minimum current is %.0f A; FTW starts the car there", charger_min_a))
620+
end
621+
-- The owner's own maximum, as found before FTW wrote in this run. A value
622+
-- under the minimum allocates nothing, so it is not worth handing back.
623+
if last_written_a == nil and charger_max_a and charger_max_a >= floor_a() then
624+
owner_max_a = charger_max_a
625+
end
540626

541627
log_variant(obs[OBS_CAPABILITIES])
542628
op_mode = as_number(obs[OBS_OP_MODE])
@@ -632,8 +718,9 @@ function driver_default_mode()
632718
stand_down_try_ms = now
633719
if not ensure_auth() then return false end
634720
local wrote = write_current(0)
635-
local _, applied = send_command(CMD_PAUSE)
721+
local sent, applied = send_command(CMD_PAUSE)
636722
if applied then paused = true end
723+
if sent then ftw_paused = true end
637724
-- Either holds the car: below 6 A Zaptec allocates nothing, and a paused
638725
-- charger draws nothing.
639726
if wrote or applied then
@@ -647,6 +734,31 @@ function driver_default_mode()
647734
return false
648735
end
649736

737+
function driver_release()
738+
-- Core calls this, not driver_default_mode, when the owner removes or
739+
-- disables the driver. Give the charger back: its own maximum and, if FTW
740+
-- paused it, a resume. A charger FTW never touched in this run is left
741+
-- alone unless its maximum sits below its minimum, which only an earlier
742+
-- FTW stand-down leaves behind and which stops every charger app.
743+
if not charger_id or not email or not password then return true end
744+
local below_floor = charger_max_a ~= nil and charger_max_a < floor_a()
745+
local held_by_ftw = ftw_paused or (below_floor and final_stop)
746+
if last_written_a == nil and not below_floor and not held_by_ftw then return true end
747+
if not ensure_auth() then return false end
748+
local amps = owner_max_a or HANDBACK_A
749+
local ok = write_current(amps)
750+
if not ok then return false end
751+
if held_by_ftw then
752+
-- 528 means there was nothing to resume, which is fine here.
753+
paused = false
754+
local resumed = send_command(CMD_RESUME)
755+
if not resumed then return false end
756+
ftw_paused = false
757+
end
758+
host.log("info", string.format("Zaptec: handed the charger back at %.0f A", amps))
759+
return true
760+
end
761+
650762
function driver_cleanup()
651763
access_token = nil
652764
refresh_token = nil

0 commit comments

Comments
 (0)