Repository navigation
feat(plan): metric_battery_grid_charge_cost - extra cost per kWh of grid energy into the battery - #6209
feat(plan): metric_battery_grid_charge_cost - extra cost per kWh of grid energy into the battery#6209brdsutte wants to merge 3 commits into
Conversation
springfall2008
left a comment
There was a problem hiding this comment.
Automated comment from the triage bot.
Reviewed head commit e1d97ef1 (metric_battery_grid_charge_cost). The new simulation term in prediction.py is arithmetically right on every path I traced — it matches get_diff's AC conversion for a charging step, fires only when net DC flow is charging, and handles the gone-car allocation, PV-covered charging and discharge/export steps correctly — and the C++ kernel mirrors it exactly with ABI/parity bumped. 3 findings, all inline: one correctness issue in battery_value_rate (the cap drops the new cost, so the setting can be a no-op on flat tariffs) and two maintainer-rule findings (a getattr with a false justification, and no test for any of the three output.py changes).
|
Thanks for the review - pushed a follow-up commit:
Quick suite (with |
springfall2008
left a comment
There was a problem hiding this comment.
Automated comment from the triage bot.
Reviewed head f2d87b21; this supersedes the review of e1d97ef1. 2 findings inline, 0 in the body.
I re-read the whole diff, not only the follow-up commit. The earlier findings are settled: the getattr in Prediction.__init__ is now a direct copy, and run_grid_charge_output_tests does fail when the three output.py sites are reverted. On the third, I accept your reasoning for leaving the battery_value_rate cap alone - the cost is paid on the way into the battery while the cap is what a stored kWh can realise by avoiding an import, so on a flat tariff the setting correctly has no effect there. My earlier framing of that as a defect was wrong, and run_grid_charge_battery_value_cap_tests now pins the intended behaviour.
Verified this pass:
- the simulation term
min(diff - car_gone_kwh, -(battery_draw + pv_dc) * inverter_loss_recp)charges exactly the AC import that covers the battery's charge demand, for both hybrid (DC PV offsets the charge) and AC-coupled (surplus PV lowersdifffirst), and is zero when PV covers the charge, when the gone car's share is netted off, and on discharge - therecpmatches the branchget_difftakes for a negative balance; - the C++ mirror is line-for-line, the new
PkContextfield sits afteriboost_rate_threshold_exporton both sides, and ABI 12 / parity 21 are bumped inprediction_kernel.cppandprediction_kernel.pytogether; - the four adjusted-charge-rate sites (
battery_value_rate, the charge sort, the plan_debug string andimport_rate_adjusted) all use the same(rate + cost) / battery_loss / inverter_lossconvention, so the planner, the simulation and the display agree; - the new module is registered in
TEST_REGISTRY, its kernel pass is skipped only viakernel_available(), and it restores the state it mutates (reset_inverter,fetch_config_options,plan_debug,log).
| # Account for losses in average rate as it makes import higher | ||
| average = window["average"] / self.inverter_loss / self.battery_loss + self.metric_battery_cycle | ||
| # Account for losses in average rate as it makes import higher, and for any grid charge cost on top of it | ||
| average = (window["average"] + self.metric_battery_grid_charge_cost) / self.inverter_loss / self.battery_loss + self.metric_battery_cycle |
There was a problem hiding this comment.
Automated comment from the triage bot.
Adding the same metric_battery_grid_charge_cost to every window's average and dividing by the same losses cannot reorder the windows - only the 0.2p banding from dp1(average / 2) * 2 changes which windows tie and fall back to the time/id tie-break. The resulting level is read only for price_set ordering; charge_window_best[...]["set"] is written here and nowhere else. In the optimiser test's own numbers the two windows get adjusted averages of 6.67 and 11.11 without the cost and 11.11 and 15.56 with it, so they sort the same way either way, and no test asserts a level value - reverting this line would not fail the suite. If the intent is the reported charge level, a one-line assertion on price_set (or window_links[...]["average"]) would pin it; if the intent is the shortlist ordering, the PR description overstates it. Not a blocker either way, but the description's "so the window shortlist agrees with the simulation" reads as if the ordering depended on it.
There was a problem hiding this comment.
Agreed for the order among charge windows - corrected the description. It does move charge entries relative to the freeze-charge entries in the same sort (they carry no losses, cycle cost or grid charge cost), now pinned by run_grid_charge_window_sort_tests in fd54007. Details in the comment below.
|
Thanks - pushed a follow-up:
Quick suite (with |
…rid energy into the battery Some installations pay more for grid charging than the import rate - for example where a generation subsidy (green energy certificates in Flanders) is paid on a generation meter between the inverter and the house, which counts grid energy going into the inverter as negative generation, so every kWh charged from the grid loses the subsidy on the round-trip losses. Add an expert-mode setting, metric_battery_grid_charge_cost (p/kWh, default 0), that the simulation adds to the metric for the grid import covering the battery's AC charge demand only: solar charging, discharging and import that serves the house are unaffected. The charge-window price sort, the battery value (refill cost) and the plan's adjusted import rate include it too. The C++ kernel mirrors the change (ABI 12, parity 21) and the binaries are rebuilt with zig 0.16.0. With the default of 0 every calculation is bit-identical to before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GwWwnCWzPxCS83RgVj4GXH
- Prediction copies metric_battery_grid_charge_cost directly: PredBat.reset() always sets it, so the getattr fallback was not needed. - Test the output.py changes: import_rate_adjusted in the JSON plan and the plan_debug HTML figure include the cost, and log_option_best names the setting only when it is non-zero. - Keep the battery_value_rate cap unchanged and pin it with a flat-tariff test: the grid charge cost is paid when charging, not when discharging, so it raises the replacement cost but not what a stored kWh can realise, which is what the cap bounds. Docstring and docs say so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GwWwnCWzPxCS83RgVj4GXH
… sort
- battery_value_rate docstring: the cap is what a stored kWh can save by avoiding an import at the
highest rate after losses and the cycle cost; the grid charge cost does not lift it because it is
paid on the way into the battery, not on the way out.
- New run_grid_charge_window_sort_tests: the cost shifts charge ("c") entries but not freeze charge
("cf") entries, so with windows at 10p and 12p and a 4p cost the 10p window's charge entry moves
ahead of the 12p window's freeze entry and the levels stop sharing bands.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GwWwnCWzPxCS83RgVj4GXH
fd54007 to
bf51e3f
Compare
|
Rebased onto current main. #6212 took kernel ABI 12 and parity revision 23, so this change now uses ABI 13 / parity 24. Kernel binaries rebuilt for all platforms with |
Fixes #6207
Adds an expert-mode setting
metric_battery_grid_charge_cost(p/kWh, default 0): an extra cost on grid energy that ends up in the battery, for installations where grid charging costs more than the import rate - e.g. a generation subsidy (green certificates in Flanders) paid on a generation meter between the inverter and the house, which counts grid energy going into the inverter as negative generation, so every kWh charged from the grid loses the subsidy on the round-trip losses. As discussed in the issue, it is a direct p/kWh setting (not auto-derived), and it only applies to grid energy charged into the battery - not to energy consumed directly by the house.What changes
prediction.py, mirrored inprediction_kernel.cpp): per step, only the grid import that covers the battery's AC charge demand is charged:grid_to_battery = min(diff - car_gone_kwh, -(battery_draw + pv_dc) / inverter_loss), if positive. PV serves the house first, house load imported during a charge window is not charged, and neither are solar charging or discharging.plan.py):battery_value_rateincludes the cost in the refill cost (not in the cap, see review), and the charge-window price sort (sort_window_by_price_combined) adds it to charge (c) entries the same way as the losses andmetric_battery_cycle. That keeps the order among charge windows, but moves them relative to the freeze-charge (cf) entries and changes the price bands.output.py): the plan's adjusted import rate includes it, and the settings line in the log shows it when non-zero.CONFIG_ITEMSentry (expert mode, min 0, default 0), read infetch_config_options(), default inPredBat.reset(), copied intoPredictionwith agetattrfallback for older debug dumps.docs/customisation.mdnext tometric_battery_cycle.With the default of 0 every calculation is bit-identical to before (
(rate + 0) / lossesis unchanged and the new simulation term is skipped).Deliberately not changed / design choices
compute_metric()(as the triage comment suggested, likemetric_battery_cycle):import_kwh_batterycounts all import during a charge window, including house load, so it would over-attribute (the triage comment flags this too). Counting only grid energy into the battery needs the per-step battery draw and PV, which only the simulation has. Adding a separate result for it would mean changing the 11-value prediction result tuple at ~18 call sites, so the cost is added to the simulated metric instead, the way the car rate premium is. A side effect is that it shows in cost predictions, which I think is right as it is a real cost, but I'm happy to rework it if you prefer.battery_value_rateincludes the cost because refilling the battery from the grid pays it too.Kernel
PkContextgainsbattery_grid_charge_costafteriboost_rate_threshold_export:PK_ABI_VERSION/KERNEL_ABI_VERSION11 -> 12,PK_PARITY_REVISION/KERNEL_PARITY_REVISION20 -> 21.build_kernel_cross.shusing zig 0.16.0 (as in CI), since the kernel-binaries job doesn't run on fork PRs. The x86_64 binary passesverify_kernel_binary.py.Testing
tests/test_battery_grid_charge_cost.py(registered asbattery_grid_charge_cost):battery_value_raterises by cost / losses;fetch_config_options());kernel_parityrandom scenarios now also randomise the new cost (drawn last, so the existing seeded scenarios are unchanged).pre-commit run --all-filespasses (check-json5 could not run locally; it only covers files this PR doesn't touch).unit_test.py --quickpasses withPREDBAT_KERNEL_REQUIRED=1, as dooptimise_windows_kernelanddebug_cases(Python 3.13 locally).detect-changes --scope compareagainst main): 19 files, 38 symbols, 14 flows, risk high -battery_value_rateis CRITICAL (it feedscompute_metricand the whole optimiser) andPrediction.run_predictionis central. That is expected for a cost term in the core model; with the default of 0 every calculation is bit-identical.metric_battery_grid_charge_cost4.05 the plan's adjusted import rate is e.g. (29.09 + 4.05) / (0.98 x 0.975) + 1 = 35.68p as expected.Nothing that I'm aware of is failing on
main.Developed with the help of Claude (Anthropic); I reviewed the change.