diff --git a/.changeset/surplus-without-step-table.md b/.changeset/surplus-without-step-table.md new file mode 100644 index 000000000..739597ea8 --- /dev/null +++ b/.changeset/surplus-without-step-table.md @@ -0,0 +1,5 @@ +--- +"ftw": patch +--- + +Solar charging works on a charger with no step table, as the settings page saves a new one. Surplus snapped every order to the table's only entry, 0 W, so the car never charged from solar. With no table the charger now gets the surplus between its minimum and maximum, and still pauses below the minimum. diff --git a/go/internal/control/testdata/sitesim_field_known.txt b/go/internal/control/testdata/sitesim_field_known.txt index e4221bb2d..f304490ad 100644 --- a/go/internal/control/testdata/sitesim_field_known.txt +++ b/go/internal/control/testdata/sitesim_field_known.txt @@ -7,8 +7,6 @@ # phases; the planner gets steps a three-phase order can deliver. # #1795 srcfl/ftw#1795: a charger drawing against its order reads not # following. -# nostep no fix yet: with phase_mode unset and no step table, surplus -# never orders a step, though export now leaves room for one. # readback no fix yet: surplus raises again before the charger applied the # last raise, then pauses when both land (surplus_reserve.go:128). probe/25A/auto/mixed/easee40s/car3p/balanced/surplus+goal+export2500W idle readback @@ -18,17 +16,15 @@ timing/16A/3p/3p/easee40s/car3p/L1/surplus idle readback timing/16A/auto/mixed/cloud60s/car3p/L1/surplus idle readback timing/16A/auto/mixed/easee120s/car3p/L1/surplus idle readback timing/16A/auto/mixed/easee40s/car3p/L1/surplus idle readback -timing/16A/empty/none/cloud60s/car3p/L1/surplus idle nostep -timing/16A/empty/none/easee120s/car3p/L1/surplus idle nostep -timing/16A/empty/none/easee40s/car3p/L1/surplus idle nostep -timing/16A/empty/none/fast2s/car3p/L1/surplus idle nostep +timing/16A/empty/none/cloud60s/car3p/L1/surplus idle readback +timing/16A/empty/none/easee120s/car3p/L1/surplus idle readback +timing/16A/empty/none/easee40s/car3p/L1/surplus idle readback timing/25A/3p/3p/cloud60s/car3p/L1/surplus idle readback timing/25A/3p/3p/easee120s/car3p/L1/surplus idle readback timing/25A/3p/3p/easee40s/car3p/L1/surplus idle readback timing/25A/auto/mixed/cloud60s/car3p/L1/surplus idle readback timing/25A/auto/mixed/easee120s/car3p/L1/surplus idle readback timing/25A/auto/mixed/easee40s/car3p/L1/surplus idle readback -timing/25A/empty/none/cloud60s/car3p/L1/surplus idle nostep -timing/25A/empty/none/easee120s/car3p/L1/surplus idle nostep -timing/25A/empty/none/easee40s/car3p/L1/surplus idle nostep -timing/25A/empty/none/fast2s/car3p/L1/surplus idle nostep +timing/25A/empty/none/cloud60s/car3p/L1/surplus idle readback +timing/25A/empty/none/easee120s/car3p/L1/surplus idle readback +timing/25A/empty/none/easee40s/car3p/L1/surplus idle readback diff --git a/go/internal/loadpoint/controller.go b/go/internal/loadpoint/controller.go index 6cb11aee1..9e61d60e8 100644 --- a/go/internal/loadpoint/controller.go +++ b/go/internal/loadpoint/controller.go @@ -1568,7 +1568,7 @@ func (c *Controller) tickOne(ctx context.Context, now time.Time, lpCfg Config, s // After the surplus clamp, a wake-kick still offers the minimum // step. A 0 A wallbox cannot finish the session the wake started. if c.wakeKickActive(lpCfg.ID, now) { - minKick := smallestNonZero(c.pickSurplusSteps(now, lpCfg)) + minKick := surplusMinStepW(lpCfg, c.pickSurplusSteps(now, lpCfg)) if minKick > 0 && cmdW < minKick { slog.Info("loadpoint wake-kick", "lp", lpCfg.ID, "prev_cmd_w", cmdW, "kick_w", minKick) @@ -1968,7 +1968,7 @@ func (c *Controller) computeSurplusCmd(now time.Time, lpCfg Config, wantW float6 if steps == nil { steps = c.pickSurplusSteps(now, lpCfg) // A hold ended after chooseTick. } - minStep := smallestNonZero(steps) + minStep := surplusMinStepW(lpCfg, steps) // Compatibility variables for the rest of the function — the // pause/resume hysteresis and wake-kick reuse these names. @@ -2050,8 +2050,20 @@ func (c *Controller) pickSurplusSteps(now time.Time, lpCfg Config) []float64 { if c == nil { return surplus3PhaseSteps(lpCfg) } + if len(lpCfg.AllowedStepsW) == 0 { + // No step table, no one-phase ladder to fall back to: a one-phase + // order would cap the charger under its minimum. The charger holds + // three phases, through a pause too, and a one-phase lock from + // before the table went is cleared. A pin still wins. + c.phaseLockMu.Lock() + delete(c.phaseLocked1P, lpCfg.ID) + delete(c.phaseLockedAt, lpCfg.ID) + c.selectPhaseLocked3P(lpCfg.ID, now) + c.phaseLockMu.Unlock() + return nil + } steps3 := surplus3PhaseSteps(lpCfg) - minStep3 := smallestNonZero(steps3) + minStep3 := surplusMinStepW(lpCfg, steps3) // 3Φ-only charger: an operator who pinned "3p" is telling us the // wallbox can't fall back to single-phase (e.g. CTEK Chargestorm — @@ -2289,6 +2301,18 @@ func (c *Controller) surplusLockedTo1P(id string) bool { return c.phaseLocked1P[id] } +// selectPhaseLocked3P records three phases as the session's phase choice. +// Caller holds phaseLockMu. +func (c *Controller) selectPhaseLocked3P(id string, now time.Time) { + if c.phaseSelected3P == nil { + c.phaseSelected3P = map[string]bool{} + c.phaseSelectedAt = map[string]time.Time{} + } + if !c.phaseSelected3P[id] { + c.phaseSelected3P[id], c.phaseSelectedAt[id] = true, now + } +} + // dwellSelectedPhaseMode returns the phase_mode string ("1p"/"3p") // implied by the near-term dwell selection for this loadpoint, or the // empty string when no dwell decision is on file (first-tick, fresh @@ -2333,7 +2357,9 @@ func sameLocalDay(a, b time.Time) bool { // locked the install to 1Φ, so a 3Φ-only set would just wedge the // loadpoint at 0. func surplus3PhaseSteps(lpCfg Config) []float64 { - if lpCfg.PhaseMode == "1p" { + if lpCfg.PhaseMode == "1p" || len(lpCfg.AllowedStepsW) == 0 { + // Without a step table the charger takes any power from its + // minimum to its maximum, as SnapChargeW reads no steps. return lpCfg.AllowedStepsW } split := lpCfg.PhaseSplitW @@ -2352,6 +2378,15 @@ func surplus3PhaseSteps(lpCfg Config) []float64 { return out } +// surplusMinStepW is the least surplus may order: the smallest step, or +// the charger's minimum when it has no step table. +func surplusMinStepW(lpCfg Config, steps []float64) float64 { + if len(steps) == 0 { + return lpCfg.MinChargeW + } + return smallestNonZero(steps) +} + func smallestNonZero(steps []float64) float64 { var min float64 for _, s := range steps { diff --git a/go/internal/loadpoint/surplus_no_step_table_test.go b/go/internal/loadpoint/surplus_no_step_table_test.go new file mode 100644 index 000000000..14a17d52c --- /dev/null +++ b/go/internal/loadpoint/surplus_no_step_table_test.go @@ -0,0 +1,119 @@ +package loadpoint + +import ( + "context" + "testing" + "time" +) + +// A charger with no step table, as the settings page writes a new one, +// takes any power from its minimum to its maximum. Surplus used to snap +// every order to its only step, 0 W, so the car never charged from +// surplus. It orders the surplus and still pauses below the minimum. +func TestSurplusWithoutAStepTableOrdersTheSurplus(t *testing.T) { + now := time.Date(2026, 10, 25, 12, 0, 0, 0, time.UTC) + cfg := Config{ID: "garage", DriverName: "easee", MinChargeW: 4140, MaxChargeW: 11000, PhaseMode: "3p", SurplusOnly: true} + for _, tc := range []struct { + surplusW, want float64 + }{{6000, 6000}, {2000, 0}} { + c := NewController(NewManager(), nil, nil, nil) + c.SetSiteSurplusForEV(func() (float64, bool) { return tc.surplusW, true }) + if got := c.computeSurplusCmd(now, cfg, cfg.MaxChargeW, nil); got != tc.want { + t.Errorf("%.0f W of surplus: order %.0f W, want %.0f W", tc.surplusW, got, tc.want) + } + } +} + +// With phase_mode unset or auto and no step table, as on the home box, +// surplus has no one-phase ladder to lock to. A day forecast under the +// three-phase minimum must not lock the charger to one phase, where an +// order is capped at 3,680 W, under its 4,140 W minimum. +func TestSurplusWithoutAStepTableNeverLocksToOnePhase(t *testing.T) { + for _, mode := range []string{"", "auto"} { + t.Run("phase_mode "+mode, func(t *testing.T) { + now := time.Date(2026, 10, 21, 12, 0, 0, 0, time.Local) + cfg := Config{ID: "garage", DriverName: "evse", MinChargeW: 4140, MaxChargeW: 11000, PhaseMode: mode, SurplusOnly: true} + samples := map[string]EVSample{cfg.DriverName: {Connected: true, RequestActive: true}} + sender := &fakeSender{} + c := newTestController(t, []Config{cfg}, nil, samples, sender) + c.SetSiteFuse(SiteFuse{MaxAmps: 25, Voltage: 230, PhaseCnt: 3}) + c.SetSiteSurplusForEV(func() (float64, bool) { return 9310, true }) + c.SetNearTermPeakSurplusW(func(time.Duration) (float64, bool) { return 0, true }) + c.SetPeakRemainingSurplusW(func() (float64, bool) { return 0, true }) + for tick := 0; tick < 10; tick++ { + c.Tick(context.Background(), now) + cmd, _ := lastSetCurrent(sender.calls) + samples[cfg.DriverName] = EVSample{Connected: true, RequestActive: true, PowerW: cmd.power} + now = now.Add(5 * time.Second) + } + for _, cmd := range sender.calls { + if cmd.action == "ev_set_current" && (cmd.phaseMode == "1p" || cmd.power > 0 && cmd.power < cfg.MinChargeW) { + t.Fatalf("offer %+v: want no one-phase order and none under 4140 W", cmd) + } + } + if last, _ := lastSetCurrent(sender.calls); last.power < 9000 { + t.Fatalf("last offer %.0f W of 9310 W surplus", last.power) + } + }) + } +} + +// An owner who removes the step table on a day the charger was locked to +// one phase, or held on one, leaves no lock behind: the charger holds +// three phases from the next tick, without a restart. +func TestRemovingTheStepTableClearsTheOnePhaseLock(t *testing.T) { + now := time.Date(2026, 10, 21, 12, 0, 0, 0, time.Local) + cfg := Config{ID: "garage", DriverName: "evse", MinChargeW: 4140, MaxChargeW: 11000, PhaseMode: "auto", SurplusOnly: true} + c := NewController(NewManager(), nil, nil, nil) + c.phaseLocked1P = map[string]bool{cfg.ID: true} + c.phaseLockedAt = map[string]time.Time{cfg.ID: now.Add(-time.Hour)} + c.phaseSelected3P = map[string]bool{cfg.ID: false} + c.phaseSelectedAt = map[string]time.Time{cfg.ID: now.Add(-time.Hour)} + if steps := c.pickSurplusSteps(now, cfg); steps != nil { + t.Fatalf("steps %v, want none without a table", steps) + } + if c.surplusLockedTo1P(cfg.ID) || c.dwellSelectedPhaseMode(cfg.ID) != "3p" { + t.Fatalf("lock %v, phase held %q after the table went; want no lock and 3p", c.surplusLockedTo1P(cfg.ID), c.dwellSelectedPhaseMode(cfg.ID)) + } +} + +// Through a pause and back, a no-table charger on auto keeps three phases: +// an "auto" order at 0 W lets the charger flip to one phase and cycle its +// contactor at every cloud. +func TestSurplusWithoutAStepTableHoldsThreePhasesThroughAPause(t *testing.T) { + now := time.Date(2026, 10, 21, 12, 0, 0, 0, time.Local) + cfg := Config{ID: "garage", DriverName: "evse", MinChargeW: 4140, MaxChargeW: 11000, PhaseMode: "auto", SurplusOnly: true} + samples := map[string]EVSample{cfg.DriverName: {Connected: true, RequestActive: true}} + sender := &fakeSender{} + c := newTestController(t, []Config{cfg}, nil, samples, sender) + c.SetSiteFuse(SiteFuse{MaxAmps: 25, Voltage: 230, PhaseCnt: 3}) + surplusW := 9310.0 + c.SetSiteSurplusForEV(func() (float64, bool) { return surplusW, true }) + c.SetNearTermPeakSurplusW(func(time.Duration) (float64, bool) { return 0, true }) + c.SetPeakRemainingSurplusW(func() (float64, bool) { return 0, true }) + for tick := 0; tick < 60; tick++ { + switch tick { + case 10: + surplusW = 1000 + case 30: + surplusW = 9310 + } + c.Tick(context.Background(), now) + cmd, _ := lastSetCurrent(sender.calls) + samples[cfg.DriverName] = EVSample{Connected: true, RequestActive: true, PowerW: cmd.power} + now = now.Add(5 * time.Second) + } + paused := false + for _, cmd := range sender.calls { + if cmd.action != "ev_set_current" { + continue + } + paused = paused || cmd.power == 0 + if cmd.phaseMode != "3p" { + t.Fatalf("offer %+v: want three phases through the pause", cmd) + } + } + if !paused { + t.Fatal("surplus never paused at 1000 W") + } +} diff --git a/go/test/site/README.md b/go/test/site/README.md index 1290d9f48..125f87119 100644 --- a/go/test/site/README.md +++ b/go/test/site/README.md @@ -83,7 +83,7 @@ tick as 2 s more. | `s2-16` Charge now during export | 16 A, 1 A house, 7 kW PV, full battery: 9.1 A export per phase | car starts within 60 s (**known**: export counted as import); fuse holds | | | `s3-16`, `s3-25` Load step | car and a 3 kW battery hold charging, then 13 A (16 A) or 21 A (25 A) per phase for 180 s | fuse holds; over the limit at most 7 s after the step | yes | | `s4-25`, `s4b-25` Saved unlock | `surplus_unlock_bat_soc` 0.9 saved, not armed; Charge now 3p, and bare (s4b) | no stops; no offer drops to 0 W; full power over the last minute | | -| `s5-25`, `s5s-25` Surplus only | 25 A, 9.3 kW surplus, `auto`; s5 without steps, s5s with Easee's | no offer above 16 A; fuse holds; only 6–16 A; ≥ 4 140 W 90 % of the time (**known** in both) | | +| `s5-25`, `s5s-25` Surplus only | 25 A, 9.3 kW surplus, `auto`; s5 without steps, s5s with Easee's | no offer above 16 A; fuse holds; only 6–16 A; ≥ 4 140 W 90 % of the time (**known** in s5s) | | | `s5t-25` Surplus with a goal | as s5s with a goal, 3.8 kW of export, a charger that switches phases | no offer above 16 A; fuse holds; ≥ 6 A on one phase 90 % of the time | | | `s6-25` Inverter offline | 1.5 kW PV, the inverter silent for 300 s | Home use shown and marked estimated throughout; measured before and after; never estimated while every driver reads | | | `s7-25` Restart | Charge now, 15 min, Core down 20 s, 420 s after | the day's Home use is kept; Home use back within 30 s; Charge now never below 4 140 W; full power within 420 s | | diff --git a/go/test/site/scenarios_test.go b/go/test/site/scenarios_test.go index 60967ea80..952d779f1 100644 --- a/go/test/site/scenarios_test.go +++ b/go/test/site/scenarios_test.go @@ -49,8 +49,7 @@ var scenarios = []scenario{ {name: "s4b-25", dir: "s4b-unlock-nophase-25A", minutes: 3.6, run: func(c *runCtx) (map[string]any, error) { return s4(c, 25, "") }, judge: judgeS4}, {name: "s5-25", dir: "s5-surplus-auto-25A", minutes: 5.2, - run: func(c *runCtx) (map[string]any, error) { return s5(c, 25, nil) }, judge: judgeS5, - known: map[string]string{"charges at least the minimum 90 % of the time": "surplus without allowed_steps_w never starts, loadpoint/controller.go surplus3PhaseSteps; next beta"}}, + run: func(c *runCtx) (map[string]any, error) { return s5(c, 25, nil) }, judge: judgeS5}, {name: "s5s-25", dir: "s5s-surplus-auto-steps-25A", minutes: 5.2, run: func(c *runCtx) (map[string]any, error) { return s5(c, 25, easeeSteps) }, judge: judgeS5, known: map[string]string{"charges at least the minimum 90 % of the time": "phase mode auto stays on 1p under 9 kW surplus (R1); next beta"}},