Skip to content
Closed
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
9 changes: 9 additions & 0 deletions .changeset/heat-comfort-day-price.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"ftw": patch
---

A `thermal` section in the configuration for heat comfort that follows the
day's price: your own cheap, normal and dear day levels, how far each zone
may move, and the least daily saving for planned heat to be acted on. It is
parsed and checked, and has no defaults. Nothing acts on it yet, so there
is no behaviour change.
7 changes: 7 additions & 0 deletions .changeset/heat-planner-package.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"ftw": patch
---

Internal: a heat planner package (`heatplan`) that proposes control values
for one zone from prices, weather and the site's energy plan. Nothing calls
it yet. No behaviour change.
17 changes: 17 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,23 @@ fleet_ping:
# soc_min: 0.10
# soc_max: 0.95

# Heat comfort that follows the day's price (docs/adr/0012). Optional, and
# nothing here has a default: every number is yours. Prices are the day's
# mean total price in öre/kWh. Between the levels the change is gradual.
# Parsed and checked today; heat control itself is not released yet.
# thermal:
# cheap_day_ore_kwh: 150 # at or below: all of cheap_day_raise_k
# normal_day_ore_kwh: 260 # your own target, unchanged
# dear_day_ore_kwh: 400 # at or above: all of dear_day_lower_k
# min_saving_ore_per_day: 500 # move heat in time only if all zones together
# # save this much in a day; 0 = no limit
# zones:
# - comfort_sensor: { driver: wiser, metric: bathroom_floor_c }
# cheap_day_raise_k: 2.5 # a warmer floor on a cheap day
# - comfort_sensor: { driver: wiser, metric: bathroom_air_c }
# dear_day_lower_k: 1.0 # cooler air on a dear day, never warmer
# step_k: 0.5 # the thermostat's step

# Peak-power tariff (weekday 06–20 local time). Same minor units as
# grid_tariff_ore_kwh, per kW, excluding VAT. 0 disables it.
# price:
Expand Down
48 changes: 48 additions & 0 deletions docs/adr/0012-heat-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,47 @@ thermal:
- Without a comfort sensor, a zone cannot be planned. The roadmap does not
let Core assume indoor temperature.

#### Comfort follows the day's price

Proposed; see [Open](#open). What a degree is worth to the owner depends on
what the day costs. A warm bathroom floor is cheap comfort on a cheap day,
and a slightly cooler room is an easy saving on a dear one. That is a choice
about comfort, not about timing, and it needs no model of the house:

```yaml
thermal:
cheap_day_ore_kwh: 150 # at or below: all of cheap_day_raise_k
normal_day_ore_kwh: 260 # the owner's own target
dear_day_ore_kwh: 400 # at or above: all of dear_day_lower_k
min_saving_ore_per_day: 500
zones:
- comfort_sensor: { driver: wiser, metric: bathroom_floor_c }
cheap_day_raise_k: 2.5
- comfort_sensor: { driver: wiser, metric: bathroom_air_c }
dear_day_lower_k: 1.0
step_k: 0.5
```

- The price is the mean total consumer price of the day the comfort is for,
once at least 22 hours of it are known. Between the levels the target
moves linearly, so a day one öre over a level is not a different day.
- Each direction is per zone and off at zero. A floor may follow cheap days
up while the room air only follows dear days down.
- The moved target stays inside the zone's hard band.
- **Nothing has a default.** These numbers depend on the contract, the
price area and the household. A site that sets none is unchanged.
- `min_saving_ore_per_day` is the least the plans of all zones must save
together in a day before any planned value is sent. Below it, every zone
is left on its target for the day. Moving heat for a few öre costs device
writes and the owner's trust in what the house is doing, and the saving is
inside the twin's error. An open gate closes again at 80 % of the limit,
so a saving near the limit does not start and stop the planning.
- **The two are layered.** The day's comfort moves the target first. The
plan and the baseline it is measured against both stand on that target,
so the plan is never credited with what the comfort rule saved. The
comfort rule holds without a trusted twin; the gate weighs plans and so
applies only where a twin is trusted.

### 2. Declaration by the driver

Two optional fields on an existing control, an optional `sensors` list and
Expand Down Expand Up @@ -362,6 +403,13 @@ Fredrik answered on the hot-water floor on 12 November 2026:

## Open

- **Comfort that follows the day's price, and the least daily saving.**
Proposed in section 1 and not yet answered. One household has run both
since November 2026 on three floor-heating zones: with a 5 kr limit, the
planned savings of 0.1–4.9 kr per day never opened the gate, so in
practice only the day's comfort level acted. Whether the day's mean is
the right measure of a cheap or dear day, against a longer reference such
as the month, is open.
- **Holding off a reheat needs a control.** Many heat pumps start a
hot-water charge by themselves at their own start temperature. The planner
can only delay a reheat on a device whose driver declares a control that
Expand Down
35 changes: 35 additions & 0 deletions go/cmd/ftw/thermal_comfort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"github.com/srcfl/ftw/go/internal/config"
"github.com/srcfl/ftw/go/internal/heatplan"
)

// thermalGateKeepShare keeps an open savings gate open down to this share
// of the owner's limit, so a saving near the limit does not start and stop
// the planning every quarter. It is not a setting: it changes no outcome
// the owner would choose between.
const thermalGateKeepShare = 0.8

// thermalComfort is the owner's comfort rule for one zone, by its sensor
// key. A zone the owner did not list, or a site with no thermal section,
// gets the zero value, which moves nothing.
func thermalComfort(cfg *config.Thermal, sensor string) heatplan.PriceComfort {
z, ok := cfg.Zone(sensor)
if !ok {
return heatplan.PriceComfort{}
}
return heatplan.PriceComfort{
CheapPrice: cfg.CheapDayOreKWh, NormalPrice: cfg.NormalDayOreKWh, DearPrice: cfg.DearDayOreKWh,
RaiseK: z.CheapDayRaiseK, LowerK: z.DearDayLowerK, StepK: z.StepK,
}
}

// thermalSavingsGate is the owner's least daily saving, in öre. No limit
// set is no gate.
func thermalSavingsGate(cfg *config.Thermal) heatplan.SavingsGate {
if cfg == nil || cfg.MinSavingOrePerDay <= 0 {
return heatplan.SavingsGate{}
}
return heatplan.SavingsGate{MinPerDay: cfg.MinSavingOrePerDay, KeepShare: thermalGateKeepShare}
}
55 changes: 55 additions & 0 deletions go/cmd/ftw/thermal_comfort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"testing"

"github.com/srcfl/ftw/go/internal/config"
"github.com/srcfl/ftw/go/internal/heatplan"
)

func TestThermalConfigBecomesThePlannersRules(t *testing.T) {
cfg := &config.Thermal{
CheapDayOreKWh: 150, NormalDayOreKWh: 260, DearDayOreKWh: 400, MinSavingOrePerDay: 500,
Zones: []config.ThermalZone{
{ComfortSensor: config.ThermalSensor{Driver: "wiser", Metric: "floor_c"}, CheapDayRaiseK: 2.5},
{ComfortSensor: config.ThermalSensor{Driver: "wiser", Metric: "air_c"}, DearDayLowerK: 1, StepK: 0.5},
},
}
if err := cfg.Validate(); err != nil {
t.Fatalf("validate: %v", err)
}
floor := thermalComfort(cfg, "wiser/floor_c")
if want := (heatplan.PriceComfort{CheapPrice: 150, NormalPrice: 260, DearPrice: 400, RaiseK: 2.5}); floor != want {
t.Errorf("floor = %+v, want %+v", floor, want)
}
// What the configuration accepts, the planner's own check accepts.
for _, key := range []string{"wiser/floor_c", "wiser/air_c"} {
if err := thermalComfort(cfg, key).Validate(); err != nil {
t.Errorf("%s: %v", key, err)
}
}
if got := floor.ShiftK(205); got != 1.25 {
t.Errorf("floor on a half-cheap day = %v K, want 1.25", got)
}
if got := thermalComfort(cfg, "wiser/air_c").ShiftK(500); got != -1 {
t.Errorf("air on a dear day = %v K, want -1", got)
}
if thermalComfort(cfg, "wiser/hall_c").Enabled() {
t.Error("a zone with no rule got one")
}
if gate := thermalSavingsGate(cfg); gate.MinPerDay != 500 || gate.KeepShare != thermalGateKeepShare {
t.Errorf("gate = %+v", gate)
}
}

func TestNoThermalConfigIsNoRuleAndNoGate(t *testing.T) {
if thermalComfort(nil, "a/b").Enabled() || thermalSavingsGate(nil) != (heatplan.SavingsGate{}) {
t.Error("a missing thermal section must change nothing")
}
if thermalSavingsGate(&config.Thermal{}) != (heatplan.SavingsGate{}) {
t.Error("no limit must be no gate")
}
if open, _ := thermalSavingsGate(nil).Open(nil, 24, false); !open {
t.Error("no limit closed the gate")
}
}
7 changes: 0 additions & 7 deletions go/internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,6 @@ type Server struct {
// nil in production; SendWithGeneration still binds every real dispatch to
// the selected running generation.
beforeDriverControlSend func()
// beforeDriverControlStateLock is a package-test seam for the narrower
// lookup-to-lock lifecycle race. It runs after the state map lookup while
// the map lock is still held, before the per-driver state lock is taken.
beforeDriverControlStateLock func()
// beforeDriverDefaultStateLock is a package-test seam for the default path's
// lookup-to-lock lifecycle race. It is nil in production.
beforeDriverDefaultStateLock func()

versionUpdateMu sync.Mutex
driverUpdateMu sync.Mutex
Expand Down
4 changes: 2 additions & 2 deletions go/internal/api/api_driver_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (s *Server) handleDriverControl(w http.ResponseWriter, r *http.Request) {
// command. The response is written inside done, still under that lock, so
// the status read that explains an error is the one that caused it.
s.controlHolds.Send(r.Context(), name, control.ID, applied, body,
time.Duration(seconds)*time.Second, s.beforeDriverControlStateLock,
time.Duration(seconds)*time.Second,
func(hold drivercontrol.Hold, err error) {
if err != nil {
s.writeControlSendError(w, name, err)
Expand Down Expand Up @@ -254,5 +254,5 @@ func (s *Server) controlRegistry() drivercontrol.Registry {
// it clears any hold under the per-driver lock and sends the driver's own
// default. See drivercontrol.Holds.SendDefault.
func (s *Server) SendDriverDefault(ctx context.Context, name string) error {
return s.controlHolds.SendDefault(ctx, name, s.beforeDriverDefaultStateLock)
return s.controlHolds.SendDefault(ctx, name)
}
Loading
Loading