Skip to content

feat(loadpoint,easee): 1Φ/3Φ auto-switching with per-phase fuse clamp - #203

Merged
erikarenhill merged 2 commits into
masterfrom
easee-phase-switching
Apr 26, 2026
Merged

erikarenhill merged 2 commits into
masterfrom
easee-phase-switching

Conversation

@erikarenhill

@erikarenhill erikarenhill commented Apr 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds 1Φ ↔ 3Φ auto-switching for an Easee charger, carved off the larger PR #184 review surface. None of the bundled features that were entangled with the planner DP / loadpoint state surface (schedule persistence, policy gates only_surplus/allow_grid/allow_battery_support, EV-first cost bias, live-load clamp) come along — only the phase mechanics + the Easee driver support.

How it works in reality

Phase decisions live in the driver. The Go controller does not pick phases. Each EV charger has its own physics — the minimum amperage it accepts (Easee = 6 A), the maximum (32 A), the contactor + cloud-API round-trip latency for switching, the manufacturer-specific wire format for the phaseMode setting. Pushing that logic into the cross-driver controller would force every future Go driver to inherit Easee's assumptions. So the controller passes through what's universal — operator preferences and site fuse parameters — and lets each driver decide.

ev_set_current command shape

{
  "action":             "ev_set_current",
  "power_w":            4000.0,         // MPC slot budget for this loadpoint
  "phase_mode":         "auto",         // "1p" | "3p" | "auto" (operator pref)
  "phase_split_w":      0,              // 0 = derive from voltage × max_amps_per_phase
  "min_phase_hold_s":   60,             // hysteresis dwell, "auto" only
  "voltage":            230.0,          // site voltage from cfg.Fuse.Voltage
  "max_amps_per_phase": 16.0,           // site fuse trip current per phase
  "site_phases":        3               // 1 or 3
}

Empty phase_mode is omitted from the cmd entirely — legacy fleets that don't read these fields see exactly the pre-switching shape and continue running locked-3Φ.

The driver's decision (in easee_cloud.lua)

  1. Pick phases. pick_phases(mode, want_w, voltage, max_amps_per_phase, split_w, hold_s, now_ms):

    • Locked 1p / 3p → return that, no hysteresis applied.
    • Empty / unknown mode → return 3 (legacy default).
    • auto → effective split = phase_split_w if set, otherwise voltage × max_amps_per_phase (so a 240 V × 16 A install splits at 3840 W, not the canonical 3680 W). Pick 1Φ when wantW < split, else 3Φ.
    • Hysteresis: auto reads host.millis() to compare against the timestamp of the last commit. If less than min_phase_hold_s × 1000 ms have passed since the last flip, hold the previous phase regardless of what the new wantW would prefer. Easee's contactor + cloud-API round-trip is ~5–10 s observed; without the hold a wantW oscillating around the split would burn the contactor and never deliver useful power.
  2. Compute amperage. per_phase_amps(power_w, voltage, phases, max_amps_per_phase):

    • amps = round(power_w / voltage / phases) — round, not floor, so 5.8 A clears the 6 A minimum.
    • Clamp to [0, EASEE_MAX_A=32] AND to the per-phase fuse ceiling AND 0 < amps < 6 → 0 (Easee silently ignores writes below its minimum, which would leave the charger in a "I requested but nothing flows" zombie state — pause cleanly instead).
  3. POST phaseMode first when requested_phases != last_sent_phases. Easee's settings endpoint accepts phaseMode = 1 (locked-1p) or 3 (locked-3p); we never use 2 (auto) because hysteresis is our concern, not the charger's.

    • Safety-critical fail-fast: if the phaseMode POST fails, the driver returns false immediately WITHOUT writing dynamicChargerCurrent. The alternative — computing amps against the unchanged phase count after a failed transition (e.g. 7400 W intended for 3Φ, written as 32 A on a single phase) would trip a 16 A breaker. Controller retries on the next tick; the operator sees a phaseMode write failed warning in the logs.
  4. POST dynamicChargerCurrent with the clamped amperage.

What the controller still owns

  • Energy allocation: translates MPC's per-slot Wh budget for this loadpoint to a continuous wantW via remaining_wh × 3600 / remaining_s, accounting for what's already been delivered mid-slot.
  • Loadpoint-level snap: SnapChargeW(wantW, MinChargeW, MaxChargeW, AllowedStepsW) — operator's discrete step list is a planning hint that maps the smooth MPC budget to a charger-friendly target. The driver may further round to its own minimum amperage.
  • Plan-not-ready 0 W standdown: explicit pause when MPC has no allocation, instead of leaving the charger riding the previous slot's setpoint.

Reference helpers (not called by the controller)

PhaseFor and FilterStepsByPhase live in loadpoint/snap.go as exported pure-Go helpers. The Easee Lua driver implements the same rule locally; these helpers are reference implementations for future Go-side drivers (and the test contract). The controller does NOT call them — phase decisions are entirely a driver-level concern.

What does NOT land

Everything else from PR #184. The pieces below stay parked on teslable-ev-surplus because each requires either MPC DP changes or Manager state expansion to be functionally complete:

  • loadpoint_schedules SQLite table + cross-restart RestoreTarget
  • Policy gates (only_surplus, allow_grid, allow_battery_support) — DP-only enforcement
  • EV-first cost bias on the home battery
  • Live-load fuse clamp (the gridW − evW headroom math)
  • Tesla BLE driver + DerVehicle telemetry type
  • Auto-charge on plug-in + vehicle auto-discovery
  • CodeMirror YAML editor

Test plan

  • go test ./... (all green incl. test/e2e)
  • 7 controller hand-off tests (controller_phase_handoff_test.go): cmd JSON carries phase_mode / phase_split_w / min_phase_hold_s / voltage / max_amps_per_phase / site_phases correctly; empty phase_mode is omitted; non-230 V site (240 V) propagates verbatim.
  • 9 reference-helper tests (phase_helpers_test.go): PhaseFor + FilterStepsByPhase covering locked modes, empty default → 3Φ, auto split direction, non-standard voltage split, custom split override, empty input.
  • Driver-level integration smoke on real hardware: site without phase_mode set continues to send 3Φ commands (no behaviour change); site with phase_mode: auto flips to 1Φ when MPC budget < per-phase ceiling.

Followups (not blockers for this PR)

  • Lua test harness so pick_phases/per_phase_amps can have unit tests inside drivers/.
  • Other EV drivers (Zaptec, OCPP) implementing the same ev_set_current contract.

🤖 Generated with Claude Code

erikarenhill and others added 2 commits April 26, 2026 18:51
Adds phase-mode awareness to the loadpoint controller and the Easee
cloud driver so a 16 A site can deliver low-W charge sessions on a
single phase (≥ 6 A min), then flip up to three phases when the MPC
budget pushes past the per-phase ceiling.

Config (per-loadpoint):

  loadpoints:
    - id: garage
      driver_name: easee
      phase_mode: auto      # "1p" | "3p" | "auto" — empty == "3p"
      phase_split_w: 0      # 0 = derive from site fuse (V × A per phase)
      min_phase_hold_s: 60  # hysteresis dwell, "auto" only

The split is derived from SiteFuse.MaxAmps × SiteFuse.Voltage so a
240 V or 220 V install is handled correctly without hard-coding 230 V.
The Easee Lua driver now also accepts a `voltage` field on the
ev_set_current command so its W→A conversion uses the same site
voltage instead of a baked 230.

Safety:

  * Per-phase fuse clamp applied BEFORE step filtering AND a
    second-line check after snapping so a misconfigured AllowedStepsW
    can't punch through the breaker.
  * On 1Φ↔3Φ transitions, the Easee driver POSTs phaseMode FIRST.
    If that POST fails, the W→A write is skipped entirely — the
    failure mode would otherwise compute amps against the old phase
    count (e.g. 7400 W ÷ 1Φ = 32 A), trip a 16 A breaker. Controller
    retries next tick.
  * MinPhaseHoldS hysteresis (default 60 s) prevents contactor flap
    when the MPC budget oscillates around the split. Locked modes
    (1p, 3p) bypass the hold.
  * Empty PhaseMode ("") behaves identically to legacy locked-3Φ —
    no behaviour change for existing fleets on upgrade.

Tests: 12 (controller_phase_test.go). Covers auto split direction,
locked modes, fuse clamping at multiple breaker sizes, non-230V
voltage propagation, hysteresis, payload shape, and the zero-fuse
no-clamp guard.

Carved off the larger PR #184 review surface — extracted only the
phase-switching pieces; schedule persistence, MPC policy gates,
EV-first cost bias, and the live-load fuse clamp stay on the branch
for separate evaluation.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Reframes the phase-switching design after review feedback: deciding
1Φ vs 3Φ is a charger-physics question (knows its min/max amps,
contactor switching latency, manufacturer-specific phaseMode wire
format) — it does NOT belong in the cross-driver Go controller.

Wire-format change to the ev_set_current command:

  Before:  power_w + phases + voltage   (controller decided phases)
  After:   power_w + phase_mode + phase_split_w + min_phase_hold_s
           + voltage + max_amps_per_phase + site_phases
           (driver decides phases from operator preferences + site
           fuse params)

The Go controller now does only what's universal: convert MPC's Wh
budget for the slot to a continuous wantW, snap to the loadpoint's
configured AllowedStepsW (a planning hint), and pass the operator's
phase_* preferences and the site fuse parameters through verbatim.
SetSiteFuse stays as a setter so main.go can inject the fuse values
once at startup.

Easee Lua now owns:

  * pick_phases() — locked / auto with hysteresis using host.millis()
    for the dwell timer (no Go-side state map per loadpoint anymore).
  * per_phase_amps() — W → A conversion using the controller-supplied
    voltage; clamps to the per-phase fuse ceiling AND to Easee's
    physical 0/6/32 A band (round-not-floor so 5.8 A → 6 A clears the
    minimum).
  * The phaseMode-fail-safe early-return: if the phaseMode POST fails
    on a transition, we abort the dynamicChargerCurrent write rather
    than command 32 A on a now-stale 1Φ phase count.

Tests:

  * controller_phase_handoff_test.go — 7 tests on the controller side
    asserting the cmd JSON carries the operator preferences and site
    fuse params correctly. Empty PhaseMode is omitted (legacy shape
    preserved). The Voltage 240 V case proves no 230 V hardcode.
  * phase_helpers_test.go — 9 tests on PhaseFor + FilterStepsByPhase
    (now exported as advisory reference implementations). The Easee
    Lua driver implements the same rule locally; these tests pin the
    contract for any future Go-side driver.

Driver-side phase logic is verified against real hardware in
integration testing — there's no Lua test harness in the project
yet (followup).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@erikarenhill
erikarenhill force-pushed the easee-phase-switching branch from ece83e0 to 5c3d1df Compare April 26, 2026 16:53
@erikarenhill
erikarenhill merged commit 9c3fecf into master Apr 26, 2026
1 check passed
@frahlg
frahlg deleted the easee-phase-switching branch August 7, 2026 08:16
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.

1 participant