Skip to content

fix(mpc): price the clock hour in progress for planners that announce demand_elapsed_hours - #1966

Merged
frahlg merged 1 commit into
masterfrom
energyplan-demand-elapsed-hours
Nov 5, 2026
Merged

frahlg merged 1 commit into
masterfrom
energyplan-demand-elapsed-hours

Conversation

@frahlg

@frahlg frahlg commented Nov 5, 2026

Copy link
Copy Markdown
Member

Problem

With 15-minute prices Core replans at :15, :30 and :45 (and reactively, for
example when a car plugs in at 17:20). bindDemandCharges sends only the
tariff hours that start at or after slot 0, so the clock hour already running
is left out of the peak-power tariff the planner sees. The plan can then set a
new monthly peak in that hour. Today's completed peak also goes into
already_kw while today's planned hours form today's group, so today counts
twice in the top-N mean.

Change

  • bindDemandCharges sends the hour in progress even when it began before
    slot 0, with the import it has had up to execution start. That import comes
    from the site meter's live hourly counter (control.MeasuredHourImport, the
    counter the import ceiling uses, restored at boot by SeedHourImport) when
    it measures that hour, else from the import history. Time neither covers
    counts at the larger of the covered mean and slot 0's forecast import, never
    at zero.
  • A local day the plan reaches sends its completed peak as an hour of that
    day's group (its own start, end, kWh and weight) instead of a value in
    already_kw, so each local day is one sample. Hours go out in ascending
    start order.
  • Both shapes come from one history read. prepareEnergyplanRequest sends the
    new shape only to a planner whose handshake announces
    demand_elapsed_hours. Any other planner gets exactly the request it got
    before (no hour before slot 0, completed days in already_kw, a charge left
    empty dropped, months in their earlier order), and time_limit_s follows
    the charges it actually gets.
  • priceReserveCharging keeps its skip on p.DemandCharges: the tariff exists
    for either planner.

The bundled planner does not announce the feature yet, so Core's requests to
it are unchanged. The contract text for the feature lands with the pin bump.

Tests

  • TestBindDemandChargesSendsTheRunningHourBeforeTheFirstSlot replaces
    TestBindDemandChargesSkipsUnalignedCurrentHour: at 10:17 with slot 0 at
    10:15 the 10:00 hour goes out with its import, for the new shape only.
  • TestBindDemandChargesPutsTodaysPeakInTodaysGroup: on a weekday at 19:20
    today's group is {completed peak, hour in progress} and already_kw has no
    entry for today; the old shape is checked against its JSON.
  • TestRunningHourImportCountsUncoveredTime: counter, history and the fill.
  • TestLegacyDemandChargesKeepTheirOrderAtTheEndOfAMonth: at 23:20 on a
    month's last day the new shape starts with the month whose last hour is in
    progress; an old planner still gets the earlier JSON, the month with
    planned hours first.
  • TestEnergyplanRequestNegotiatesElapsedDemandHours: old and new planners
    get their JSON; a site whose only demand hour is the one in progress gives
    an old planner no demand charge and the non-demand budget.
  • TestReservePricingKeepsAwayFromADemandTariffWithOnlyTheRunningHour.
  • TestMeasuredHourImportCountsTheMeteredHour (control).
  • TestNativeEnergyplanTakesElapsedDemandHours: through
    NewEnergyplanOptimizer, with slot 0 at :15, the plan passes
    ValidatePlan. It skips while the planner's handshake lacks the feature,
    as TestNativeEnergyplanTakesHourlyImportCaps does; it asserts once the
    pin moves.

Overlap

This also changes go/cmd/ftw/main.go and go/internal/mpc/service.go, which open PR #1956 touches. The two branches merge without a conflict in those files.

go vet is clean, and go test -count=1 ./internal/mpc/... ./internal/control/... ./cmd/ftw/... passes on a8b696a.

🤖 Generated with Claude Code

… demand_elapsed_hours

With 15-minute prices Core replans at :15, :30 and :45, and
bindDemandCharges sent only the tariff hours that start at or after
slot 0. The hour already running was left out of the peak-power tariff
the planner sees, so a plan could set a new monthly peak in it, and
today's completed peak counted twice in the top-N mean.

Core now sends the hour in progress with the import measured so far
(the site meter's hourly counter, else the history; uncovered time
counts at no less than slot 0's forecast import) and puts today's
completed peak in today's group. Only a planner whose handshake
announces demand_elapsed_hours gets this shape; any other planner,
including the bundled one, gets exactly the request it got before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Fredrik Ahlgren <[email protected]>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2a675fe157

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +262 to +263
covered := max(0, h.lastAt.Sub(h.start)-h.unknown)
return mpc.HourImport{Start: h.start, ImportedWh: h.importedWh, Covered: covered}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep measured hour import independent of ceiling resets

When an owner enables or changes the import ceiling mid-hour, stepHourImportLimits resets h.importedWh and marks all preceding time unknown. This getter therefore returns only the import since that setting change, not the site's measured import for the hour. For a planner advertising demand_elapsed_hours, runningHourImportWh then prefers this truncated counter over available history and reconstructs the missing period using the subsequent mean or forecast. If import was higher before the change, the request understates the running hour's consumption and can schedule charging that creates an avoidable demand peak. Preserve a separate full-hour measured counter, or fall back to history when the ceiling counter has been reset.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-05T05:19:50.610607Z 2a675fe PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@frahlg
frahlg merged commit 7c8430c into master Nov 5, 2026
23 checks passed
@frahlg frahlg mentioned this pull request Nov 6, 2026
6 tasks done
frahlg added a commit that referenced this pull request Nov 6, 2026
Bundle the worker from srcfl/energyplan 847936a (0.7.0, tag v0.7.0-beta.1)
for linux-arm64 and linux-amd64. The macOS worker is no longer bundled;
#1977 lets verify.py check such a bundle and skip execution on a Mac. The
worker lists demand_elapsed_hours, so Core (#1966) sends it the demand
hour in progress and the day's completed peak in that day's group, and
the native demand test now requires the feature.

The contract describes the feature and the demand-charge fields, each
call's time limit since #1968, the batteries' total recovery rule and
optional reply diagnostics, and it stays identical to Energyplan's copy.
The worker version pins and the README follow. Licenses and forecast
schemas are unchanged; the notices list two more microlp modifications.
Protocol v1 is unchanged.

Signed-off-by: Fredrik Ahlgren <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
@frahlg
frahlg deleted the energyplan-demand-elapsed-hours branch November 11, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant