Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ev-keeps-order-at-quarter-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ftw": patch
---

A car the plan runs at a fixed step for a quarter no longer stops a few seconds early. On mains above 230 V the car draws a little more than its step, so it used up the quarter's energy early. The stop then reached the charger in the next quarter, and on a busy phase the start test kept the car off for over a minute. A car now runs for the plan's charging time in the quarter, or until the energy is used up if that comes later. Core no longer sends a pause shorter than 20 seconds at the end of a quarter.
6 changes: 4 additions & 2 deletions docs/energyplan-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ not establish the greatest reachable energy or the cheapest plan.
- `flex_max_power_w` is the allowed on-step; zero means off. Mean power must
lie between zero and that step.
- Stored EV energy increases by `mean_W * hours * charge_efficiency`.
- Core applies the on-step until the energy budget is spent, then pauses.
Validation must cover joint on/off pulse states, grid limits, surplus-only
- Core applies the on-step from the start of the executable part for its
on-time, the energy budget at the on-step, and on until the metered energy
is spent if that comes later, then pauses. An off-time under 20 s at the
slot's end is not sent. Validation must cover joint on/off pulse states, grid limits, surplus-only
charging, battery-to-EV rules and the corresponding costs.

For example, 1,000 Wh at efficiency 1 over a remaining 15 minutes requires
Expand Down
63 changes: 63 additions & 0 deletions go/internal/control/sitesim_duty_ceiling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package control

import (
"testing"
"time"

"github.com/srcfl/ftw/go/internal/loadpoint"
"github.com/srcfl/ftw/go/internal/sitesim"
)

// A duty quarter keeps the car on for the plan's on-time after its energy is
// spent. Core's import ceiling still cuts it: the on-time only shapes what
// the loadpoint asks for, and the EV hard limit's cap applies after it. Here
// the mains runs at 237 V, so the car draws 10665 W of the plan's 10350 W
// and spends the 02:45 quarter's energy at 02:59:33. At 02:59:40 the hour's
// ceiling leaves 5 kW for the rest of the hour, under the house and the car.
func TestSiteSimDutyOnTimeYieldsToTheImportCeiling(t *testing.T) {
start := time.Date(2026, 10, 26, 2, 30, 0, 0, time.Local)
p := simPlant(start)
p.Voltage, p.HouseW, p.EVSE.ReadbackLag = 237, houseA(1.5, 1.5, 1.5), 0
quarter := start.Add(15 * time.Minute)
s := &simSite{p: p, lp: simLoadpoint(), plan: func(now time.Time) (loadpoint.Directive, bool) {
slot := now.Truncate(15 * time.Minute)
if now.Before(start) || !now.Before(quarter.Add(15*time.Minute)) {
return loadpoint.Directive{}, false
}
return loadpoint.Directive{SlotStart: slot, SlotEnd: slot.Add(15 * time.Minute),
LoadpointEnergyWh: map[string]float64{"garage": 10350.0 / 4}, LoadpointMaxPowerW: map[string]float64{"garage": 10350}}, true
}}
s.setup = func(s *simSite) {
s.state.SiteFuseVoltage = 230
s.ctrl.SetSiteFuse(loadpoint.SiteFuse{MaxAmps: p.Fuse.RatingA, Voltage: 230, PhaseCnt: 3})
s.mgr.SetSchedule(s.lp.ID, loadpoint.Schedule{SoC: .8})
}
s.boot()
s.run(quarter.Sub(p.Now))
quarterWh := p.EVSE.SessionWh
s.run(quarter.Add(14*time.Minute + 39*time.Second).Sub(p.Now))
if s.failure != "" {
t.Fatal(s.failure)
}
if spentWh := p.EVSE.SessionWh - quarterWh; s.offerW != 10350 || spentWh < 10350.0/4 {
t.Fatalf("setup at 02:59:39: offer %.0f W, %.0f of %.0f Wh in the quarter; want the on-time running past the spent energy", s.offerW, spentWh, 10350.0/4)
}
s.state.PeakImportCeilingW = 5000
cutAt := p.Now
for s.offerW != 0 && p.Now.Sub(cutAt) < 5*time.Second {
s.step()
}
// The cut is the ceiling's; after it the spent energy keeps the car off.
if st, _ := s.mgr.State(s.lp.ID); s.offerW != 0 || st.CommandedReason != "import_limit" {
t.Fatalf("%v after the ceiling: offer %.0f W (%s), want 0 W on import_limit", p.Now.Sub(cutAt), s.offerW, st.CommandedReason)
}
s.run(quarter.Add(15*time.Minute - sitesim.Step).Sub(p.Now))
for _, c := range s.commands {
if c.device == "evse" && c.at.After(cutAt) && c.w > 0 {
t.Fatalf("ordered the car %.0f W at %s, after the ceiling cut it", c.w, c.at.Format("15:04:05"))
}
}
if p.EVSE.DrawW != 0 {
t.Fatalf("the car still draws %.0f W at the quarter's end", p.EVSE.DrawW)
}
}
50 changes: 50 additions & 0 deletions go/internal/control/sitesim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,49 @@ func planEV(w float64) loadpoint.PlanFunc {
}
}

// homeBoxNightPlan is the home box's plan for the car on 25 Oct 2026: its
// on-power and the energy each quarter gives it.
func homeBoxNightPlan(now time.Time) (loadpoint.Directive, bool) {
slot, evW, onW := now.Truncate(15*time.Minute), 10350.0, 10350.0
if slot.Minute() == 45 {
evW, onW = 8395, 8970
}
return loadpoint.Directive{SlotStart: slot, SlotEnd: slot.Add(15 * time.Minute),
LoadpointEnergyWh: map[string]float64{"garage": evW / 4}, LoadpointMaxPowerW: map[string]float64{"garage": onW}}, true
}

// homeBoxNight is the home box on 25 Oct 2026 from 02:29: a 3x20 A site
// whose mains runs at volts while Core reckons with 230 V, an Easee that
// answers in 8 s, a 9.6 kWh battery and the night's plan. The car must
// charge through 02:45 and run the plan's 13 A beside the battery's 2091 W.
func homeBoxNight(name string, volts float64) simScenario {
return simScenario{name: name, start: time.Date(2026, 10, 26, 2, 29, 0, 0, time.Local), run: 21 * time.Minute,
plant: func(p *sitesim.Plant) {
p.Fuse.RatingA, p.Voltage, p.HouseW = 20, volts, houseA(2.5, 1.3, 1.2)
p.EVSE.Delay, p.EVSE.ReadbackLag = 8*time.Second, 0
p.Battery = &sitesim.Battery{Phases: 3, MaxChargeW: 5000, MaxDischargeW: 5000, CapacityWh: 9600, SoC: .21, Latency: 8 * time.Second}
},
plan: homeBoxNightPlan,
setup: func(s *simSite) {
s.every, s.state.SiteFuseVoltage = 3*time.Second, 230
s.ctrl.SetSiteFuse(loadpoint.SiteFuse{MaxAmps: 20, Voltage: 230, PhaseCnt: 3})
s.state.Mode, s.state.UseEnergyDispatch = ModePlannerArbitrage, true
s.state.SlotDirective = func(now time.Time) (SlotDirective, bool) {
slot, w := now.Truncate(15*time.Minute), 711.0
if slot.Minute() == 45 {
w = 2091
}
return SlotDirective{SlotStart: slot, SlotEnd: slot.Add(15 * time.Minute), BatteryEnergyWh: w / 4, Strategy: "arbitrage"}, true
}
},
expect: []simExpect{{21*time.Minute - time.Second, func(s *simSite) error {
if s.p.EVSE.OfferA != 13 || s.p.EVSE.DrawW <= 0 || math.Abs(s.p.Battery.W-2091) > 100 {
return fmt.Errorf("at 02:50: car offered %.0f A and draws %.0f W, battery %.0f W; want the plan's 13 A and 2091 W", s.p.EVSE.OfferA, s.p.EVSE.DrawW, s.p.Battery.W)
}
return nil
}}}}
}

// planBatteryCharge has the plan charge the battery at w for the hour.
func planBatteryCharge(w float64) func(*simSite) {
return func(s *simSite) {
Expand Down Expand Up @@ -661,6 +704,13 @@ func simScenarios() []simScenario {
return nil
}},
}},
// The home box, 25 Oct 2026, v0.155.1-beta.1: the car drew 10386 W of
// the plan's 10350 W and spent the quarter's energy 3 s early. Core
// stopped it; at 02:45 the plan wanted 8970 W with the battery at
// 2091 W, but the start test read the car's own current on L1 and kept
// it off for 84 s. At 237 V the energy runs out 27 s early.
homeBoxNight("home box night, quarter's energy spent 3 s early", 230.8),
homeBoxNight("home box night at 237 V, energy spent 27 s early", 237),
// A one-phase car (a plug-in hybrid) on the field night's table: the
// plan's 4.8 kW three-phase order is 7 A a phase, which the car takes
// on L1 alone, 1.6 kW. Once the meter shows it, the order is the
Expand Down
20 changes: 18 additions & 2 deletions go/internal/loadpoint/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2517,14 +2517,30 @@ func (c *Controller) computeCommandForBudget(now time.Time, lpCfg Config, curren
}
maxW := lpCfg.MaxChargeW
if ceiling, ok := d.LoadpointMaxPowerW[lpCfg.ID]; ok {
// A duty plan commands its legal on-power for its on-time, the slot's
// energy at that power, and on until the energy is spent. A car that
// draws more than its step, as on a mains above 230 V, spends the
// energy early but keeps running for the on-time; a car stopped by
// then stays stopped. An off-time shorter than CommandSettle would
// reach the charger only in the next slot, so the car runs to the
// slot's end. The energy and the ceiling are both the plan's watts,
// before a one-phase car's order is scaled.
onUntil := now
if ceiling > 0 && budgetWh > 0 && !now.Before(d.SlotStart) && !d.LoadpointFallback[lpCfg.ID] {
onUntil = d.SlotStart.Add(time.Duration(budgetWh / ceiling * float64(time.Hour)))
if d.SlotEnd.Sub(onUntil) < CommandSettle {
onUntil = d.SlotEnd
}
}
st, _ := c.manager.State(lpCfg.ID)
running := st.CommandedKnown && st.CommandedW > 0
if onePhaseCar {
ceiling *= 3
}
maxW = min(maxW, ceiling)
if maxW <= 0 || remainingWh <= 1e-6 || remainingS <= 0 {
if maxW <= 0 || remainingS <= 0 || (remainingWh <= 1e-6 && (!running || !now.Before(onUntil))) {
return 0, true, false
}
// A duty plan commands its legal on-power until the Wh budget is spent.
wantW = maxW
}
// Joint fuse allocator (dispatch.go) caps EV demand when battery + EV
Expand Down
72 changes: 72 additions & 0 deletions go/internal/loadpoint/controller_energy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,78 @@ func TestDutyPlanCommandsOnPowerThenStopsAtEnergyBudget(t *testing.T) {
}
}

// A duty slot runs its on-power for the plan's on-time, its energy at that
// power. The home box on 25 Oct 2026: the plan gave the car 10350 W for the
// whole quarter, the mains ran at 230.8 V and the car drew 10386 W, so the
// energy ran out 3 s before the quarter ended. The stop reached the charger
// in the next quarter, which wanted the car too.
func TestDutySlotRunsItsOnTime(t *testing.T) {
for _, tc := range []struct {
name string
from int // seconds into the quarter the plan's interval starts, as after a replan
onS float64 // the plan's on-time at 10350 W from then
draw float64 // what the car draws per watt ordered
capFrom, capTo int // the fuse cap is capW in [capFrom, capTo)
capW float64
stopAt int // the tick the order goes to 0 W; -1: never
finalW float64 // the order at the quarter's last tick, when it never stops
}{
{name: "a full quarter at 230.8 V (the home box)", onS: 900, draw: 230.8 / 230, stopAt: -1, finalW: 10350},
{name: "a full quarter at 239 V", onS: 900, draw: 239.0 / 230, stopAt: -1, finalW: 10350},
{name: "replanned at 5 minutes, 239 V", from: 300, onS: 600, draw: 239.0 / 230, stopAt: -1, finalW: 10350},
{name: "nine tenths, the car draws 4 % more", onS: 810, draw: 1.04, stopAt: 810},
{name: "nine tenths, the car draws 5 % less", onS: 810, draw: .95, stopAt: 855},
{name: "an off-time of 15 s", onS: 885, draw: 1, stopAt: -1, finalW: 10350},
{name: "an off-time of 20 s", onS: 880, draw: 1, stopAt: 882},
{name: "an off-time of 25 s, the car draws 2 % more", onS: 875, draw: 1.02, stopAt: 876},
{name: "no energy", onS: 0, draw: 1, stopAt: 0},
{name: "a fuse cap of 5000 W in the last minute", onS: 900, draw: 230.8 / 230, capFrom: 840, capTo: 900, capW: 5000, stopAt: -1, finalW: 4140},
{name: "a fuse stop once the energy is spent", onS: 810, draw: 1.04, capFrom: 783, capTo: 795, stopAt: 783},
} {
t.Run(tc.name, func(t *testing.T) {
quarter := time.Date(2026, 10, 25, 1, 30, 0, 0, time.UTC)
cfg := Config{ID: "garage", DriverName: "easee", MinChargeW: 4140, MaxChargeW: 11040, AllowedStepsW: []float64{0, 4140, 8970, 10350}}
budgetWh := 10350 * tc.onS / 3600
dir := &Directive{SlotStart: quarter.Add(time.Duration(tc.from) * time.Second), SlotEnd: quarter.Add(15 * time.Minute),
LoadpointEnergyWh: map[string]float64{"garage": budgetWh}, LoadpointMaxPowerW: map[string]float64{"garage": 10350}}
samples := map[string]EVSample{"easee": {Connected: true, RequestActive: true, SessionWh: 1000}}
sender := &fakeSender{}
c := newTestController(t, []Config{cfg}, dir, samples, sender)
var s int
c.SetFuseEVMax(func() (float64, bool) {
if s >= tc.capFrom && s < tc.capTo {
return tc.capW, true
}
return 1e9, true
})
var delivered, drawW, cmdW float64
stoppedAt := -1
for s = tc.from; s < 900; s += 3 {
now := quarter.Add(time.Duration(s) * time.Second)
delivered += drawW * 3 / 3600
sample := samples["easee"]
sample.PowerW, sample.PowerAt, sample.SessionWh, sample.EnergyAt = drawW, now, 1000+delivered, now
samples["easee"] = sample
c.Tick(context.Background(), now)
cmd, ok := lastSetCurrent(sender.calls)
if !ok {
t.Fatal("missing current command")
}
if stoppedAt >= 0 && cmd.power > 0 {
t.Fatalf("started again at %ds after stopping at %ds", s, stoppedAt)
}
if stoppedAt < 0 && cmd.power == 0 {
stoppedAt = s
}
cmdW, drawW = cmd.power, cmd.power*tc.draw
}
if stoppedAt != tc.stopAt || tc.stopAt < 0 && cmdW != tc.finalW {
t.Fatalf("stopped at %ds (want %d), last order %.0f W (want %.0f); %.1f of %.1f Wh delivered", stoppedAt, tc.stopAt, cmdW, tc.finalW, delivered, budgetWh)
}
})
}
}

func TestDutyDispatchStopsForUnavailablePowerAndKeepsSpentBudget(t *testing.T) {
start := time.Now().Truncate(time.Minute)
cfg := Config{ID: "garage", DriverName: "easee", MinChargeW: 4140, MaxChargeW: 11000}
Expand Down
Loading