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
13 changes: 13 additions & 0 deletions .changeset/battery-makes-no-room-for-the-car.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"ftw": patch
---

A home battery that may not cover the car no longer makes room for it under
the fuse or the import limit. Before, a plan could discharge the battery to
cover the house while the car charged against the fuse; the room that freed
went to the car, so battery energy charged the car although the battery
should cover only the house. The car now gets the room the grid gives beside
the house with the battery idle, in Core's planner, its reserve plan, the
check of every worker plan and the charger's fuse limit. A battery boost, or
the old site-wide battery cover, still lets the battery help the car; a
boosted car may also start on the battery's room before it draws.
7 changes: 7 additions & 0 deletions docs/energyplan-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ not establish the greatest reachable energy or the cheapest plan.
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.

`no_storage_to_load` means storage may cover the house but never the car,
also not by making its room. While such a car is on, battery discharge may
not exceed `max(0, load_w + pv_w)`, and in a slot with `max_import_w` the
house and the on-steps of all charging cars must fit that limit with the
battery idle whenever the battery discharges. `surplus_only` keeps the same
rules.

For example, 1,000 Wh at efficiency 1 over a remaining 15 minutes requires
4,000 W mean. With steps `[0, 6000]`, the on-step is 6,000 W for ten minutes.
A 5,000 W available import margin cannot admit that pulse merely because the
Expand Down
2 changes: 2 additions & 0 deletions go/cmd/ftw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3233,6 +3233,7 @@ func main() {
// resume charging.
evCurtailHeadroomW := loadpoint.SurplusPotentialW(lpStatesSnapshot)
boostEVW, boostReserveSoC := activeBatteryBoostTotals(lpController, lpStatesSnapshot, tickNow)
boostActive := lpController != nil && lpController.HasActiveBatteryBoost(lpStatesSnapshot, tickNow)
var evFuseDemands []loadpoint.FuseDemand
if lpController != nil {
evFuseDemands = lpController.FuseDemands()
Expand All @@ -3248,6 +3249,7 @@ func main() {
ctrl.EVCurtailHeadroomW = evCurtailHeadroomW
ctrl.BatteryBoostEVChargingW = boostEVW
ctrl.BatteryBoostReserveSoC = boostReserveSoC
ctrl.BatteryBoostActive = boostActive
ctrl.SetEVCommanded(lpStatesSnapshot)
ctrl.SetEVFuseDemands(evFuseDemands)
fuseMaxW = ctrl.SiteFuseAmps * ctrl.SiteFuseVoltage * float64(ctrl.SiteFusePhases)
Expand Down
5 changes: 5 additions & 0 deletions go/internal/control/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ type State struct {
// main.go refreshes both from loadpoint.Controller every dispatch tick.
BatteryBoostEVChargingW float64
BatteryBoostReserveSoC float64
// BatteryBoostActive reports a valid lease on a plugged-in loadpoint
// that is not surplus-only, whether or not its car draws yet. The room
// a battery makes counts for the car from the start
// (evBatteryDischargeW): the lease's draw is zero until the car starts.
BatteryBoostActive bool
// EVCommandedW is the frozen sum of final charger offers from this tick.
// It reserves site capacity before optional battery charging, even before
// an EV ramps up and in modes without an accepted optimizer plan.
Expand Down
73 changes: 73 additions & 0 deletions go/internal/control/ev_battery_cover_room_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package control

import (
"fmt"
"testing"
"time"
)

// A field night, 15 Nov 2026: the car charged against the fuse while the
// plan discharged the battery to cover the house. The meter showed the
// room the discharge made, and the car took it, so about two thirds of the
// battery's energy went into the car although the battery could cover only
// the house. A battery that may not cover the car leaves the car the room
// the grid gives with the battery idle. A battery that may cover it still
// lends it the room.
func TestPlannedDischargeIsRoomOnlyForACarTheBatteryMayCover(t *testing.T) {
const houseW, lineW = 10000.0, 16560.0 // 24 A on three phases
for _, covers := range []bool{false, true} {
t.Run(fmt.Sprintf("covers=%v", covers), func(t *testing.T) {
s := newEVBatterySite(t, houseW)
s.state.BatteryCoversEV = covers
start := s.now
discharge := SlotDirective{SlotStart: start, SlotEnd: start.Add(time.Hour), BatteryEnergyWh: -4000, Strategy: "arbitrage"}
s.state.SlotDirective = func(time.Time) (SlotDirective, bool) { return discharge, true }
s.run(10 * time.Minute)
t.Logf("offer %.0f W, battery %.0f W, grid %.0f W, highest grid %.0f W", s.offer(), s.batteryW, s.gridW(), s.maxGridW)
if s.batteryW > -3000 {
t.Fatalf("setup: the plan's discharge did not run: battery %.0f W", s.batteryW)
}
if s.maxGridW > lineW+1e-6 {
t.Fatalf("grid %.0f W over the %v W line", s.maxGridW, lineW)
}
gridRoomW := lineW - houseW
if !covers && s.offer() > gridRoomW {
t.Fatalf("the car took %.0f W, past the grid's %.0f W of room: the battery's discharge went into the car", s.offer(), gridRoomW)
}
if covers && s.offer() <= gridRoomW {
t.Fatalf("a battery that may cover the car left it at %.0f W, inside the grid's %.0f W of room", s.offer(), gridRoomW)
}
})
}
}

// A boost lets the battery help the car, so the room its discharge makes is
// the car's from the start. The lease's live draw is zero until the car
// starts, so the lease itself counts: with the house near the line and the
// battery covering it, a boosted car that waited for its own draw would
// never start. Without a boost the car waits for the grid's own room.
func TestABoostedCarStartsOnTheBatterysRoom(t *testing.T) {
const houseW, lineW, minW = 13000.0, 16560.0, 4140.0 // grid room 3,560 W
for _, boosted := range []bool{false, true} {
t.Run(fmt.Sprintf("boosted=%v", boosted), func(t *testing.T) {
s := newEVBatterySite(t, houseW)
s.state.BatteryBoostActive = boosted
discharge := SlotDirective{SlotStart: s.now, SlotEnd: s.now.Add(time.Hour), BatteryEnergyWh: -4000, Strategy: "arbitrage"}
s.state.SlotDirective = func(time.Time) (SlotDirective, bool) { return discharge, true }
s.run(10 * time.Minute)
t.Logf("offer %.0f W, battery %.0f W, grid %.0f W, highest grid %.0f W", s.offer(), s.batteryW, s.gridW(), s.maxGridW)
if s.batteryW > -3000 {
t.Fatalf("setup: the plan's discharge did not run: battery %.0f W", s.batteryW)
}
if s.maxGridW > lineW+1e-6 {
t.Fatalf("grid %.0f W over the %v W line", s.maxGridW, lineW)
}
if boosted && s.offer() < minW {
t.Fatalf("a boosted car stayed at %.0f W beside %.0f W of battery discharge", s.offer(), -s.batteryW)
}
if !boosted && s.offer() > 0 {
t.Fatalf("the car started at %.0f W on the battery's room, with %.0f W of grid room", s.offer(), lineW-houseW)
}
})
}
}
3 changes: 2 additions & 1 deletion go/internal/control/ev_battery_sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ func TestReturningBatteryReliefCountsWholeAgain(t *testing.T) {
}

// Relief is only the discharge the fuse guard added: a cancelled charge or
// the planner's own discharge stays room for the car.
// the planner's own discharge stays out of it. Whether the planner's
// discharge is room for the car is evBatteryDischargeW's call.
func TestFuseReliefIsOnlyAddedDischarge(t *testing.T) {
s := &State{}
s.recordFuseRelief(
Expand Down
46 changes: 43 additions & 3 deletions go/internal/control/ev_fuse_budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,8 +996,9 @@ func (s *State) stepEVMeterLimit(store *telemetry.Store, meter *telemetry.DerRea
// show yet (in r). Between that level and the import limit the cap holds.
// Battery discharge the fuse guard added ends once the site fits without
// it, so it is no room: a car that took it would keep the battery
// feeding the car at the line.
if reliefW := s.evFuseReliefW(store); reliefW > 0 {
// feeding the car at the line. Where the battery may not cover the car,
// no discharge is room (evBatteryDischargeW).
if reliefW := s.evBatteryDischargeW(store); reliefW > 0 {
r = s.evMeterRoom(meter, gridW, pool.phases, fuseMaxW, s.withBatteryOrders(s.pendingLoad(at), pending+reliefW))
}
fuseRoom, importRoom := 0.0, 0.0
Expand Down Expand Up @@ -1045,11 +1046,50 @@ func (s *State) stepEVMeterLimit(store *telemetry.Store, meter *telemetry.DerRea
// build on it. expireOfflineFuseRelief ends it evSettle after it went offline.
func (s *State) evFuseReliefW(store *telemetry.Store) float64 {
var w float64
for _, relief := range s.evFuseReliefByDriver(store) {
w += relief
}
return w
}

func (s *State) evFuseReliefByDriver(store *telemetry.Store) map[string]float64 {
out := make(map[string]float64, len(s.batteryFuseRelief))
for driver, relief := range s.batteryFuseRelief {
if raw, _, ok := store.OnlineRawW(driver, telemetry.DerBattery); ok {
relief = math.Min(relief, math.Max(0, -raw))
}
w += relief
out[driver] = relief
}
return out
}

// evBatteryDischargeW is the battery discharge a car's room must not
// include. The fuse guard's relief always counts (evFuseReliefW). Where the
// battery may not cover the car, so does every discharge Core ordered, as
// far as a live reading shows it: the room a battery makes by covering the
// house would let the car charge faster on battery energy whenever the fuse
// or the import limit binds. A discharge Core did not order, such as one a
// battery holds across a restart, is the device's own and stays room, as
// before, until Core's next order replaces it. An active boost lease lets
// the battery cover the car before its car draws, so a boosted car can
// start on that room.
func (s *State) evBatteryDischargeW(store *telemetry.Store) float64 {
if s.batteryMayCoverEV() || s.BatteryBoostActive {
return s.evFuseReliefW(store)
}
byDriver := s.evFuseReliefByDriver(store)
now := s.now()
for driver, log := range s.batteryCommands {
_, _, latest, sent := log.span(now)
raw, _, live := store.OnlineRawW(driver, telemetry.DerBattery)
if !sent || !live || latest >= 0 {
continue
}
byDriver[driver] = math.Max(byDriver[driver], math.Min(-latest, math.Max(0, -raw)))
}
var w float64
for _, discharge := range byDriver {
w += discharge
}
return w
}
Expand Down
21 changes: 19 additions & 2 deletions go/internal/loadpoint/battery_boost.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ func (c *Controller) evaluateBatteryBoost(id string, now time.Time, connected, d
// loadpoint snapshot from the current tick.
func (c *Controller) ActiveBatteryBoostTotals(states []State, now time.Time) (powerW, reserveSoC float64) {
for _, st := range states {
lease, status := c.BatteryBoost(st.ID, now)
if !status.Active || !st.PluggedIn || st.SurplusOnly {
lease, ok := c.servedBatteryBoost(st, now)
if !ok {
continue
}
if st.CurrentPowerW > 0 {
Expand All @@ -390,3 +390,20 @@ func (c *Controller) ActiveBatteryBoostTotals(states []State, now time.Time) (po
}
return powerW, reserveSoC
}

// HasActiveBatteryBoost reports whether a loadpoint in the snapshot has a
// lease ActiveBatteryBoostTotals would serve, whether or not its car draws
// yet: the lease's live watts stay zero until the car starts.
func (c *Controller) HasActiveBatteryBoost(states []State, now time.Time) bool {
for _, st := range states {
if _, ok := c.servedBatteryBoost(st, now); ok {
return true
}
}
return false
}

func (c *Controller) servedBatteryBoost(st State, now time.Time) (BatteryBoostLease, bool) {
lease, status := c.BatteryBoost(st.ID, now)
return lease, status.Active && st.PluggedIn && !st.SurplusOnly
}
34 changes: 34 additions & 0 deletions go/internal/loadpoint/battery_boost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,37 @@ func TestBatteryBoostLeaseJSONRoundTripIsFraction(t *testing.T) {
t.Fatalf("1.02 overflow = %v, want 1 (not 0.0102)", overflow.MinBatterySoC)
}
}

// A lease counts before its car draws: the totals' live watts stay zero
// until the car starts, and Core must still let the battery make it room.
func TestHasActiveBatteryBoostBeforeTheCarDraws(t *testing.T) {
mgr := NewManager()
mgr.Load([]Config{{ID: "garage", DriverName: "garage"}, {ID: "street", DriverName: "street"}})
mgr.Observe("garage", true, 0, 0, true)
mgr.Observe("street", true, 0, 0, true)
ctrl := NewController(mgr, nil, nil, nil)
now := time.Now()
if ctrl.HasActiveBatteryBoost(mgr.States(), now) {
t.Fatal("active without a lease")
}
if _, err := ctrl.EnableBatteryBoost("street", validBatteryBoost(now), now); err != nil {
t.Fatal(err)
}
mgr.Observe("street", false, 0, 0, true)
if ctrl.HasActiveBatteryBoost(mgr.States(), now) {
t.Fatal("active for a lease on a loadpoint with no car plugged in")
}
if _, err := ctrl.EnableBatteryBoost("garage", validBatteryBoost(now), now); err != nil {
t.Fatal(err)
}
if !ctrl.HasActiveBatteryBoost(mgr.States(), now) {
t.Fatal("not active for a lease on a plugged-in car that does not draw yet")
}
if power, _ := ctrl.ActiveBatteryBoostTotals(mgr.States(), now); power != 0 {
t.Fatalf("live boost watts = %.0f, want 0 before the car draws", power)
}
var none *Controller
if none.HasActiveBatteryBoost(mgr.States(), now) {
t.Fatal("a nil controller reports an active boost")
}
}
15 changes: 15 additions & 0 deletions go/internal/loadpoint/site_power.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ func BatteryDischargeFeedsEV(batteryW, evW, loadW, pvW float64) bool {
return -batteryW > HouseResidualW(loadW, pvW)+SitePowerEpsW
}

// BatteryDischargeFeedsEVUnderImport adds the site's import limit to
// BatteryDischargeFeedsEV. When the house and the car would not fit under
// maxImportW with the battery idle, the discharge is what lets the car
// charge that fast, so it puts battery energy into the car even while the
// house uses all of it. A maxImportW of 0 or less means no limit.
func BatteryDischargeFeedsEVUnderImport(batteryW, evW, loadW, pvW, maxImportW float64) bool {
if BatteryDischargeFeedsEV(batteryW, evW, loadW, pvW) {
return true
}
if evW <= 0 || batteryW >= 0 || maxImportW <= 0 {
return false
}
return GridW(loadW, pvW, 0, evW) > maxImportW+SitePowerEpsW
}

// PlannedSurplusForEVW is the near-term 3Φ-gate quantity: leftover PV
// after house load, minus planned PV-soak battery charge. gridW is the
// grid flow attributed to house+battery (planned GridW minus EV).
Expand Down
20 changes: 20 additions & 0 deletions go/internal/loadpoint/site_power_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ func TestBatteryDischargeFeedsEV(t *testing.T) {
}
}

func TestBatteryDischargeFeedsEVUnderImport(t *testing.T) {
for _, tc := range []struct {
name string
battery, ev, load, limit float64
want bool
}{
{"past the house", -4000, 4000, 500, 0, true},
{"house cover, no limit", -3000, 8280, 3800, 0, false},
{"house cover makes the car's room", -3000, 8280, 3800, 9250, true},
{"car fits the grid alone", -3000, 5450, 3800, 9250, false},
{"within the meter's noise", -3000, 5500, 3800, 9250, false},
{"battery charging", 500, 8280, 3800, 9250, false},
{"car idle", -3000, 0, 3800, 9250, false},
} {
if got := BatteryDischargeFeedsEVUnderImport(tc.battery, tc.ev, tc.load, 0, tc.limit); got != tc.want {
t.Errorf("%s: got %v, want %v", tc.name, got, tc.want)
}
}
}

func TestEffectivePVWActiveZeroIsTrueCap(t *testing.T) {
if got := EffectivePVW(-5000, 0, false); got != -5000 {
t.Errorf("inactive = %.0f, want -5000", got)
Expand Down
Loading
Loading