From 09dfa52f5aee54696c2f3131c2fbc9090d7eba29 Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Sun, 25 Oct 2026 09:54:24 +0100 Subject: [PATCH] fix(control): an idle car starts on the hour's slack under the import ceiling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hourly import ceiling caps each clock hour's mean import, but the idle start held the car to the ceiling itself: evMeterRoom set the start level to min(rise level, ceiling), and stepEVIdleStart needs that level less the dwell's mean import to hold the car's minimum. When house load plus the minimum sat above the ceiling, the car never started, however much of the hour was left. A help report showed a 4.5 kW ceiling, 0.85–1.3 kW of house load and a 4,140 W minimum: 0 W to the car for a whole hour with 3.3–3.9 kWh of room. The start level is now the rise level, which already follows the hour's rules: it starts each hour at the ceiling, rises in whole steps with one in hand, waits 5 minutes after a cut and does not rise in the last 15. The rate cut still guards the hour's energy. In the hour's last quarter the start level stays at most the ceiling, as before, so Core never orders a start on slack with less than 15 minutes of the hour left. Tests: TestImportCeilingNoStartOnSlackAlone becomes TestImportCeilingStartsOnHoursSlack, with the help report (850 and 1,300 W house, 4.5 kW) and the old 5 kW / 7.5 kW case. Over three hours the car starts in every hour, stops at most once in each and never within 15 minutes of a start, every hour's mean holds the ceiling, and every tick says import_limit. TestImportCeilingNightNeverSaysFuse now lets the car run on the 22:00 hour's slack and checks the label from 23:01 to midnight. TestImportCeilingStartTestSaysImportLimit pins the last-quarter rule. Signed-off-by: Fredrik Ahlgren Co-Authored-By: Claude Opus 5.5 --- .changeset/import-ceiling-idle-start.md | 5 + go/internal/control/ev_fuse_budget.go | 16 +-- go/internal/control/import_hour_test.go | 126 +++++++++++++++++++----- 3 files changed, 115 insertions(+), 32 deletions(-) create mode 100644 .changeset/import-ceiling-idle-start.md diff --git a/.changeset/import-ceiling-idle-start.md b/.changeset/import-ceiling-idle-start.md new file mode 100644 index 000000000..d4afc8108 --- /dev/null +++ b/.changeset/import-ceiling-idle-start.md @@ -0,0 +1,5 @@ +--- +"ftw": patch +--- + +An idle car now starts under the hourly import limit when the hour has room for it. The limit is an hourly average, but FTW let an idle car start only if the house and the car's lowest charging power fit under the limit at that moment. With a 4.5 kW limit, about 1 kW of house load and a car that needs 4.1 kW, the car stayed at 0 W all hour although the hour had over 3 kWh to spare. When the house and the car together need more than the limit, the car now starts once the hour has saved enough room, charges to the end of the hour and stops when the next hour begins, so each hour still averages under the limit. On such a site the charger starts and stops about once an hour. In the last 15 minutes of an hour FTW starts a car only if it fits under the limit itself, so it does not start a car only to stop it minutes later. diff --git a/go/internal/control/ev_fuse_budget.go b/go/internal/control/ev_fuse_budget.go index c9f194f2b..ed63acfa1 100644 --- a/go/internal/control/ev_fuse_budget.go +++ b/go/internal/control/ev_fuse_budget.go @@ -500,8 +500,8 @@ func (s *State) stepEVIdleStart(r evMeterRoom, gridW float64, pool evPool, first if !r.hourly { perReadingW = math.Min(perReadingW, r.riseW) } else { - // The start test below holds an idle car to the ceiling itself, - // whatever slack the hour has: name it by this reading. + // The start test below holds an idle car to the start level: + // name it by this reading. lim.byImport = r.startLevelW-gridW-pending < r.fuseW } room := 0.0 @@ -557,7 +557,7 @@ type evMeterRoom struct { overLimit bool // a phase, or the total without phases, reads over the fuse's working limit limitW float64 // below the hour's import limit; negative is an overage riseW float64 // below the level an increase may reach, less what we sent - startLevelW float64 // import an idle start may reach: the rise level, at most the ceiling + startLevelW float64 // import an idle start may reach: the rise level, in the hour's last quarter at most the ceiling hourly bool // the hour's import is measured, so a short overage may wait measured bool // false when the meter reports phase currents that cannot be read worstA float64 @@ -570,9 +570,13 @@ func (s *State) evMeterRoom(meter *telemetry.DerReading, gridW, phases, fuseMaxW r := evMeterRoom{fuseW: totalW - sent.w, fuseOverW: math.Max(0, -totalW), overLimit: totalW < 0, limitW: inf, riseW: inf, startLevelW: inf} if limitW, riseW, ok := s.importHourLimitsW(); ok { r.limitW, r.riseW = limitW-gridW, riseW-gridW-sent.w - // A car that could run only on one hour's slack would stop when - // the next hour starts at the ceiling. - r.startLevelW = math.Min(riseW, s.PeakImportCeilingW) + // The ceiling caps the hour's mean, so an idle car may start on the + // hour's slack, as a rise may. In the hour's last quarter it may + // not: the next hour starts again at the ceiling and would stop it. + r.startLevelW = riseW + if now := s.now(); s.importHourStart(now).Add(time.Hour).Sub(now) < importHourRiseMinLeft { + r.startLevelW = math.Min(riseW, s.PeakImportCeilingW) + } r.hourly = s.importHourMeasured() } amps, present, valid := phaseCurrentsA(meter, s) diff --git a/go/internal/control/import_hour_test.go b/go/internal/control/import_hour_test.go index 06741868c..9b2be261b 100644 --- a/go/internal/control/import_hour_test.go +++ b/go/internal/control/import_hour_test.go @@ -414,24 +414,78 @@ func TestImportCeilingSetMidHourCountsFromThen(t *testing.T) { } } -// A car that could run only on the hour's slack does not start: with 5 kW -// of house load under a 7.5 kW ceiling, its 4.14 kW minimum fits only late -// in each hour and would stop when the next one starts. -func TestImportCeilingNoStartOnSlackAlone(t *testing.T) { - h := newHourSite(t, 7500, 5000, clock(16, 21, 0)) - h.run(3 * time.Hour) - h.logChanges() - for _, o := range h.offers { - if o.w > 0 { - t.Fatalf("the car started at %s on slack alone", o.at.In(hourTestZone).Format("15:04:05")) +// Help report, 25 Oct 2026: a 4.5 kW hourly ceiling, 0.85–1.3 kW of house +// load and a car whose minimum is 4,140 W. House and minimum sit above the +// ceiling, so the car can run only on the hour's slack. The ceiling held it +// at 0 W for a whole hour with over 3 kWh of room left. It now starts in +// every hour, stops when the next hour starts again at the ceiling, and each +// hour's mean holds the ceiling. A 5 kW house under a 7.5 kW ceiling is the +// same case. The phases stay low, and every tick says import_limit. +func TestImportCeilingStartsOnHoursSlack(t *testing.T) { + for _, tc := range []struct { + name string + ceilingW, houseW float64 + }{ + {"help report", 4500, 850}, + {"help report, 1.3 kW house", 4500, 1300}, + {"5 kW house, 7.5 kW ceiling", 7500, 5000}, + } { + t.Run(tc.name, func(t *testing.T) { + from := clock(25, 9, 0) + h := newHourSite(t, tc.ceilingW, tc.houseW, from) + for h.now.Before(from.Add(3 * time.Hour)) { + h.step() + if st, _ := h.mgr.State("garage"); st.CommandedReason != "import_limit" { + h.logChanges() + t.Fatalf("%s says %q under the hour's ceiling, want import_limit", h.now.In(hourTestZone).Format("15:04:05"), st.CommandedReason) + } + } + h.logChanges() + h.startsEveryHour(from, 3) + }) + } +} + +// startsEveryHour checks whole clock hours from `from`: the car starts in +// each, stops at most once in each and never within 15 minutes of a start, +// and each hour's mean import holds the ceiling. +func (h *hourSite) startsEveryHour(from time.Time, hours int) { + h.t.Helper() + starts, stops := map[time.Time]int{}, map[time.Time]int{} + var startedAt time.Time + last := 0.0 + for _, o := range h.offersSince(from) { + hour := importHourStart(o.at, hourTestZone) + switch { + case o.w > 0 && last == 0: + starts[hour]++ + startedAt = o.at + case o.w == 0 && last > 0: + stops[hour]++ + if o.at.Sub(startedAt) < 15*time.Minute { + h.t.Errorf("the car stopped at %s, %v after it started", o.at.In(hourTestZone).Format("15:04:05"), o.at.Sub(startedAt)) + } + } + last = o.w + } + for i := range hours { + hour := from.Add(time.Duration(i) * time.Hour) + if starts[hour] == 0 || stops[hour] > 1 { + h.t.Errorf("hour %s: %d starts and %d stops, want a start and at most one stop", hour.Format("15:04"), starts[hour], stops[hour]) + } + if mean := h.meanW(hour); mean > h.ceilingW { + h.t.Errorf("hour %s mean import %.0f W over the %.0f W ceiling", hour.Format("15:04"), mean, h.ceilingW) } } } // The field case from the review of #1783: a 7.5 kW hourly ceiling, a 25 A -// fuse, a light first half hour, and a car plugged in at 22:40 beside 6 kW -// (and 8 kW) of house load. The ceiling keeps it idle to the end of the -// hour, and not one tick of that says fuse_limit. +// fuse, a light first 40 minutes, and a car plugged in at 22:40 beside 6 kW +// (and 8 kW) of house load. The hour's slack lets the car run to 23:00, as +// near the fuse as its margin allows, and the hour's mean holds the ceiling. +// The next hour starts at the ceiling and stops it. Once the fuse has cooled +// from that run, the phases sit low, and not one tick to midnight says +// fuse_limit. func TestImportCeilingNightNeverSaysFuse(t *testing.T) { for _, houseW := range []float64{6000, 8000} { h := newHourSite(t, 7500, 500, clock(16, 22, 0)) @@ -439,34 +493,54 @@ func TestImportCeilingNightNeverSaysFuse(t *testing.T) { h.run(40 * time.Minute) h.setHouse(houseW) h.plugIn() + started := false for h.now.Before(clock(16, 23, 0)) { + h.step() + started = started || h.offer() > 0 + } + if !started { + t.Fatalf("house %.0f W: the car never started on the hour's slack", houseW) + } + if h.offer() != 0 { + t.Fatalf("house %.0f W: offer %.0f W at 23:00, want the new hour to stop the car", houseW, h.offer()) + } + if mean := h.meanW(clock(16, 22, 0)); mean > 7500 { + t.Fatalf("house %.0f W: hour 22:00 mean import %.0f W over the 7,500 W ceiling", houseW, mean) + } + h.run(time.Minute) // the fuse's heat from the run + for h.now.Before(clock(17, 0, 0)) { h.step() if st, _ := h.mgr.State("garage"); st.CommandedReason == "fuse_limit" { t.Fatalf("house %.0f W: %s says fuse_limit with the phases low under a 7.5 kW hourly ceiling", houseW, h.now.In(hourTestZone).Format("15:04:05")) } } - if h.offer() != 0 { - t.Fatalf("house %.0f W: the car started at %.0f W under the ceiling", houseW, h.offer()) - } + h.logChanges() } } -// A quiet first 40 minutes leave the hour so much slack that its limits sit -// above what the 25 A fuse allows. An 8 kW house still keeps an idle car from -// starting, by the ceiling's own start test, and the car says import_limit. +// A quiet first 46 minutes leave the hour so much slack that its limits sit +// above what the 25 A fuse allows. In the hour's last quarter an idle car +// may not start on that slack: the next hour starts again at the ceiling +// and would stop it. An 8 kW house keeps the car idle, by the ceiling's own +// start test, and every tick says import_limit. func TestImportCeilingStartTestSaysImportLimit(t *testing.T) { h := newHourSite(t, 7500, 0, clock(16, 21, 0)) h.unplugged = true - h.run(40 * time.Minute) + h.run(46 * time.Minute) + if _, rise, _ := h.state.importHourLimitsW(); rise <= 17250 { + t.Fatalf("setup: rise level %.0f W, want it above the fuse", rise) + } h.setHouse(8000) h.plugIn() - h.run(5 * time.Minute) - if h.offer() != 0 { - t.Fatalf("the car started at %.0f W beside 8 kW under a 7.5 kW ceiling", h.offer()) - } - if st, _ := h.mgr.State("garage"); st.CommandedReason != "import_limit" { - t.Fatalf("idle under the ceiling says %q, want import_limit", st.CommandedReason) + for h.now.Before(clock(16, 22, 0)) { + h.step() + if h.offer() != 0 { + t.Fatalf("the car started at %.0f W at %s, in the hour's last quarter", h.offer(), h.now.In(hourTestZone).Format("15:04:05")) + } + if st, _ := h.mgr.State("garage"); st.CommandedReason != "import_limit" { + t.Fatalf("idle under the ceiling says %q at %s, want import_limit", st.CommandedReason, h.now.In(hourTestZone).Format("15:04:05")) + } } }