diff --git a/apps/predbat/compare.py b/apps/predbat/compare.py index 8269051fb..cf21853ae 100644 --- a/apps/predbat/compare.py +++ b/apps/predbat/compare.py @@ -20,6 +20,7 @@ from utils import dp0, dp2 import yaml import copy +from currency import round_currency # TODO: # 1. Update web UI to show a chart of historical differences @@ -369,11 +370,12 @@ def fetch_rates(self, tariff, rate_import_base, rate_export_base, io_adjusted_ba # rates is checked against them as they are, so only this tariff's override can make it differ. import_match = rate_import_match if (import_replaced and rate_import_match) else rate_import_base export_match = rate_export_match if (export_replaced and rate_export_match) else rate_export_base + scale = pb.currency_scale for minute in range(pb.minutes_now, pb.forecast_minutes + pb.minutes_now): - if dp2(pb.rate_import.get(minute, 0)) != dp2(import_match.get(minute, 0)): + if round_currency(pb.rate_import.get(minute, 0), scale, 2) != round_currency(import_match.get(minute, 0), scale, 2): self.log("Compare rate import is different, minute {} changed from {} to {}".format(minute, import_match.get(minute, 0), pb.rate_import.get(minute, 0))) return False - if dp2(pb.rate_export.get(minute, 0)) != dp2(export_match.get(minute, 0)): + if round_currency(pb.rate_export.get(minute, 0), scale, 2) != round_currency(export_match.get(minute, 0), scale, 2): self.log("Compare rate export is different, minute {} changed from {} to {}".format(minute, export_match.get(minute, 0), pb.rate_export.get(minute, 0))) return False return True @@ -385,7 +387,8 @@ def is_live_published(self, rates, live_rates): """ minutes_now = self.pb.minutes_now overlap = [minute for minute in rates if minute >= minutes_now and minute in live_rates] - return bool(overlap) and all(dp2(rates[minute]) == dp2(live_rates[minute]) for minute in overlap) + scale = self.pb.currency_scale + return bool(overlap) and all(round_currency(rates[minute], scale, 2) == round_currency(live_rates[minute], scale, 2) for minute in overlap) def apply_hardware_overrides(self, tariff, my_predbat): """ @@ -524,7 +527,7 @@ def run_scenario(self, end_record): "soc_min10": dp2(soc_min10), "battery_cycle": dp2(battery_cycle), "battery_cycle10": dp2(battery_cycle10), - "metric": dp2(metric), + "metric": round_currency(metric, my_predbat.currency_scale, 2), "metric_keep": dp2(metric_keep), "metric_keep10": dp2(metric_keep10), "final_iboost": dp2(final_iboost), @@ -537,7 +540,8 @@ def run_scenario(self, end_record): "end_record": end_record, } for item in result_data: - result_data[item] = dp2(result_data[item]) + if item != "metric": + result_data[item] = dp2(result_data[item]) result_data["html"] = html result_data["raw"] = raw_plan result_data["date"] = my_predbat.now_utc.strftime("%Y-%m-%d %H:%M:%S") @@ -616,7 +620,7 @@ def select_best(self, compare_list, results): Recommend the best tariff """ best_selected = "" - best_metric = 9999999999 + best_metric = float("inf") for compare in compare_list: tariff_id = compare.get("id", "") diff --git a/apps/predbat/currency.py b/apps/predbat/currency.py new file mode 100644 index 000000000..4aadd0aba --- /dev/null +++ b/apps/predbat/currency.py @@ -0,0 +1,16 @@ +"""Extended monetary heuristic and precision calibration for upstream Predbat currency_scale. + +Upstream v9.7.11 PR #6156 owns parsing of currency_scale and the ranges of +monetary HA settings. This module only rounds numerical optimisation outputs in +calibrated minor units. It NEVER converts tariff inputs or user-selected values. +""" + + +def round_currency(value, scale=1.0, digits=4): + """Round a monetary result at a scale-invariant algorithmic precision. + + Preserves the original built-in round() exactly at scale=1. + """ + if scale == 1.0: + return round(value, digits) + return round(value / scale, digits) * scale diff --git a/apps/predbat/fetch.py b/apps/predbat/fetch.py index 40a232d77..aa6e75853 100644 --- a/apps/predbat/fetch.py +++ b/apps/predbat/fetch.py @@ -56,6 +56,7 @@ from predbat_metrics import metrics from futurerate import FutureRate from axle import fetch_axle_sessions, load_axle_slot, fetch_axle_active +from currency import round_currency import ast import copy @@ -3300,7 +3301,7 @@ def find_charge_window(self, rates, minute, threshold_rate, find_high, alt_rates pv_light_dark_last = pv_state if rate_low_count: - rate_low_average = dp2(rate_low_average / rate_low_count) + rate_low_average = round_currency(rate_low_average / rate_low_count, self.currency_scale, 2) return rate_low_start, rate_low_end, rate_low_average def pre_saving_for_session_minutes(self, pre_saving, saving_minutes): @@ -3588,10 +3589,10 @@ def rate_minmax(self, rates): """ Work out min and max rates """ - rate_min = 99999 + rate_min = float("inf") rate_min_minute = 0 rate_max_minute = 0 - rate_max = 0 + rate_max = -float("inf") rate_average = 0 rate_n = 0 rate = 0 @@ -3611,10 +3612,11 @@ def rate_minmax(self, rates): rate_average += rate rate_n += 1 minute += 1 - if rate_n: - rate_average /= rate_n + if not rate_n: + return 99999, 0, 0, rate_min_minute, rate_max_minute + rate_average /= rate_n - return dp2(rate_min), dp2(rate_max), dp2(rate_average), rate_min_minute, rate_max_minute + return round_currency(rate_min, self.currency_scale, 2), round_currency(rate_max, self.currency_scale, 2), round_currency(rate_average, self.currency_scale, 2), rate_min_minute, rate_max_minute def rate_base_min_max(self, rates): """ @@ -3646,8 +3648,8 @@ def rate_minmax_excluding_saving(self, rates, saving_minutes, rate_base): if not saving_minutes or not rate_base: return self.rate_minmax(rates)[:3] - rate_min = 99999 - rate_max = 0 + rate_min = float("inf") + rate_max = -float("inf") rate_total = 0 rate_n = 0 @@ -3669,7 +3671,7 @@ def rate_minmax_excluding_saving(self, rates, saving_minutes, rate_base): if rate_n == 0: return self.rate_minmax(rates)[:3] - return dp2(rate_min), dp2(rate_max), dp2(rate_total / rate_n) + return round_currency(rate_min, self.currency_scale, 2), round_currency(rate_max, self.currency_scale, 2), round_currency(rate_total / rate_n, self.currency_scale, 2) def rate_min_forward_calc(self, rates): """ @@ -3703,15 +3705,15 @@ def rate_scan_window(self, rates, rate_low_min_window, threshold_rate, find_high pv_light_dark = pv_light_dark or {} minute = start_minute found_rates = [] - lowest = 99 - highest = -99 + lowest = float("inf") + highest = -float("inf") while True: rate_low_start, rate_low_end, rate_low_average = self.find_charge_window(rates, minute, threshold_rate, find_high, alt_rates=alt_rates, pv_light_dark=pv_light_dark) window = {} window["start"] = rate_low_start window["end"] = rate_low_end - window["average"] = dp2(rate_low_average) + window["average"] = round_currency(rate_low_average, self.currency_scale, 2) if rate_low_start >= 0: if (return_raw or (rate_low_end > self.minutes_now)) and (rate_low_end - rate_low_start) >= rate_low_min_window: @@ -3725,7 +3727,7 @@ def rate_scan_window(self, rates, rate_low_min_window, threshold_rate, find_high else: break - return found_rates, lowest, highest + return (found_rates, lowest, highest) if found_rates else (found_rates, 99, -99) def find_low_rate_windows(self, alt_rates=None, pv_light_dark=None): """ @@ -3831,31 +3833,31 @@ def set_rate_thresholds(self): self.rate_import_tariff_max = rate_max if self.rate_low_threshold > 0: - self.rate_import_cost_threshold = dp2(rate_average * self.rate_low_threshold) + self.rate_import_cost_threshold = round_currency(rate_average * self.rate_low_threshold, self.currency_scale, 2) else: # In automatic mode select the only rate or everything but the most expensive if (rate_max == rate_min) or (rate_export_max > rate_max) or have_alerts or have_manual_soc: - self.rate_import_cost_threshold = rate_max + 0.1 + self.rate_import_cost_threshold = rate_max + 0.1 * self.currency_scale else: - self.rate_import_cost_threshold = rate_max - 0.5 + self.rate_import_cost_threshold = rate_max - 0.5 * self.currency_scale # When we plan car on the rates we need to include all the rates up to the max car price if car_planning_on_rates: - self.rate_import_cost_threshold = max(self.rate_import_cost_threshold, car_charging_max_price + 0.1) + self.rate_import_cost_threshold = max(self.rate_import_cost_threshold, car_charging_max_price + 0.1 * self.currency_scale) # Ahead of an event worth charging for, every tariff slot is a candidate, as when export beats import above self.rate_import_pre_event_end = self.find_event_pre_charge(rate_max) - self.rate_import_pre_event_threshold = rate_max + 0.1 + self.rate_import_pre_event_threshold = rate_max + 0.1 * self.currency_scale # Compute the export rate threshold if self.rate_high_threshold > 0: - self.rate_export_cost_threshold = dp2(rate_export_average * self.rate_high_threshold) + self.rate_export_cost_threshold = round_currency(rate_export_average * self.rate_high_threshold, self.currency_scale, 2) else: # In automatic mode select the only rate or everything but the most cheapest if (rate_export_max == rate_export_min) or (rate_export_min > rate_min) or have_alerts: - self.rate_export_cost_threshold = rate_export_min - 0.1 + self.rate_export_cost_threshold = rate_export_min - 0.1 * self.currency_scale else: - self.rate_export_cost_threshold = rate_export_min + 0.5 + self.rate_export_cost_threshold = rate_export_min + 0.5 * self.currency_scale self.log( "Rate thresholds (for charge/export) are import {}{} ({}{}), export {}{} ({}{})".format( diff --git a/apps/predbat/octopus.py b/apps/predbat/octopus.py index 2c6df5807..6dd16c47f 100644 --- a/apps/predbat/octopus.py +++ b/apps/predbat/octopus.py @@ -4615,7 +4615,7 @@ def rate_add_io_slots(self, car_n, rates, octopus_slots): # certain, so it keeps its own rate and stays unmarked. rate_min_base is rounded by # dp2 but the tariff feed is not, so allow for that: an off-peak 5.2314p is not # lowered by the 5.23p dispatch (#5392). - if assumed_price < rates.get(minute, assumed_price) - IO_RATE_TOLERANCE: + if assumed_price < rates.get(minute, assumed_price) - IO_RATE_TOLERANCE * self.currency_scale: self.io_adjusted[minute] = True rates[minute] = assumed_price else: diff --git a/apps/predbat/plan.py b/apps/predbat/plan.py index 4583933b2..501f726cc 100644 --- a/apps/predbat/plan.py +++ b/apps/predbat/plan.py @@ -19,6 +19,7 @@ """ from datetime import datetime, timedelta, timezone +from currency import round_currency from multiprocessing import cpu_count from const import ( CLOUD_FACTOR_PV10, @@ -671,7 +672,7 @@ def io_confirmed_credit_update(self, save=True): period_start = (self.minutes_now // 30) * 30 run_minute = self.minutes_now while self.io_adjusted.get(run_minute, False) and run_minute < self.minutes_now + self.forecast_minutes: - credit[run_minute] = IO_CONFIRMED_CREDIT / (1 + (run_minute - period_start) // 30) + credit[run_minute] = (IO_CONFIRMED_CREDIT * self.currency_scale) / (1 + (run_minute - period_start) // 30) run_minute += 1 self.io_confirmed_credit = credit @@ -1167,7 +1168,7 @@ def optimise_charge_limit_price_threads( region_end=None, fast=False, quiet=False, - best_metric=9999999, + best_metric=float("inf"), best_cost=0, best_keep=0, best_soc_min=None, @@ -1205,8 +1206,8 @@ def optimise_charge_limit_price_threads( if levels_score is None: levels_score = {} else: - best_level_score = 9999999 - worst_level_score = -9999999 + best_level_score = float("inf") + worst_level_score = -float("inf") # Work out the best levels score so far for price in price_set: best_level_score = min(best_level_score, levels_score[price]) @@ -1227,7 +1228,7 @@ def optimise_charge_limit_price_threads( export_enable = False # Most expensive first - all_prices = price_set[::] + [dp1(price_set[-1] - 1)] + all_prices = price_set[::] + [round_currency(price_set[-1] - self.currency_scale, self.currency_scale, 1)] if not quiet: self.log("All prices {}".format(all_prices)) if region_start: @@ -1340,7 +1341,7 @@ def export_window_allowed(window_n): # Start loop of trials for loop_price in all_prices: if best_level_score is not None: - this_level_score = levels_score.get(loop_price, 9999999) + this_level_score = levels_score.get(loop_price, float("inf")) if abs(this_level_score - best_level_score) > (0.2 * level_score_range): if self.debug_enable: self.log("Skipping price {} as level score {} is not within 20% of best {}".format(loop_price, this_level_score, best_level_score)) @@ -1476,7 +1477,7 @@ def export_selection_for(max_slots, allow_freeze): try_value = tried_list[try_hash] if try_value is not True: if loop_price not in levels_score: - levels_score[loop_price] = 9999999 + levels_score[loop_price] = float("inf") levels_score[loop_price] = min(levels_score[loop_price], tried_list[try_hash]) continue @@ -1563,7 +1564,7 @@ def export_selection_for(max_slots, allow_freeze): ) if loop_price not in levels_score: - levels_score[loop_price] = 9999999 + levels_score[loop_price] = float("inf") levels_score[loop_price] = min(levels_score[loop_price], metric) # For the first pass just pick the most cost effective threshold, consider soc keep later @@ -2029,7 +2030,7 @@ def should_replace_plan(self, metric_prev, metric_new, fragmentation_prev, fragm return True if improvement >= 0 and fragmentation_new < fragmentation_prev: return True - if self.io_confirmed_tiebreak and improvement > 0 and cost_prev is not None and cost_new is not None and dp2(cost_new) <= dp2(cost_prev): + if self.io_confirmed_tiebreak and improvement > 0 and cost_prev is not None and cost_new is not None and round_currency(cost_new, self.currency_scale, 2) <= round_currency(cost_prev, self.currency_scale, 2): return True return False @@ -2740,7 +2741,7 @@ def battery_value_rate(self, minute): # The export tariff's own minimum, before any saving/Axle session - rate_export_min is scanned with them in export_min = self.rate_export_min if self.rate_export_min_base is None else self.rate_export_min_base rate_export_min = export_min * self.inverter_loss * self.battery_loss_discharge - self.metric_battery_cycle - rate_min - return max(rate_min, 1.0, rate_export_min) + return max(rate_min, self.currency_scale, rate_export_min) def compute_metric(self, end_record, soc, soc10, cost, cost10, final_iboost, final_iboost10, battery_cycle, metric_keep, final_carbon_g, import_kwh_battery, import_kwh_house, export_kwh, soc90=None, cost90=None, final_iboost90=0.0): """ @@ -2786,7 +2787,7 @@ def compute_metric(self, end_record, soc, soc10, cost, cost10, final_iboost, fin # Adjustment for battery cycles metric metric += battery_cycle * self.metric_battery_cycle + metric_keep - return dp4(metric), dp4(battery_value) + return round_currency(metric, self.currency_scale), round_currency(battery_value, self.currency_scale) def optimise_charge_limit(self, window_n, record_charge_windows, charge_limit, charge_window, export_window, export_limits, all_n=None, end_record=None, freeze_only=False, allow_freeze=True): """ @@ -2796,9 +2797,9 @@ def optimise_charge_limit(self, window_n, record_charge_windows, charge_limit, c best_soc = self.soc_max best_soc_min = 0 best_soc_min_minute = 0 - best_metric = 9999999 + best_metric = float("inf") best_metric_first = best_metric - best_metric_plan = 9999999 + best_metric_plan = float("inf") best_cost = 0 best_import = 0 best_soc_step = self.best_soc_step @@ -3085,20 +3086,20 @@ def optimise_charge_limit(self, window_n, record_charge_windows, charge_limit, c # to try to avoid constant small changes to SoC target by forcing to keep the current % during a charge period # if changing it has little impact - the same weighting a running export gets if keep_charge_target is not None and keep_charge_target == calc_percent_limit(try_soc, self.soc_max): - metric -= max(0.5, self.metric_min_improvement) + metric -= max(0.5 * self.currency_scale, self.metric_min_improvement) if try_soc == best_soc_min_setting: # Minor weighting to 0% - metric -= 0.003 + metric -= 0.003 * self.currency_scale elif try_soc == self.soc_max: # Minor weighting to 100% - metric -= 0.002 + metric -= 0.002 * self.currency_scale elif self.set_charge_freeze and try_soc == self.reserve: # Minor weighting to freeze - metric -= 0.001 + metric -= 0.001 * self.currency_scale # Round metric to 4 DP - metric = dp4(metric) + metric = round_currency(metric, self.currency_scale) if self.debug_enable: self.log( @@ -3271,11 +3272,11 @@ def optimise_export(self, window_n, record_charge_windows, try_charge_limit, cha # instruction - not False. It used to be returned as a bare False and compared numerically, # where it read as 0: a full discharge, the opposite of the intent. best_export = pack_export_limit(EXPORT_MODE_IDLE) - best_metric = 9999999 - best_score = 9999999 - best_metric_plan = 9999999 - off_metric = 9999999 - off_gate = 9999999 + best_metric = float("inf") + best_score = float("inf") + best_metric_plan = float("inf") + off_metric = float("inf") + off_gate = float("inf") best_cost = 0 best_soc_min = 0 best_soc_min_minute = 0 @@ -3405,10 +3406,10 @@ def optimise_export(self, window_n, record_charge_windows, try_charge_limit, cha if export_mode_of(this_export_limit) == EXPORT_MODE_IDLE: # Minor weighting to off - metric -= 0.002 + metric -= 0.002 * self.currency_scale elif export_limit_is_full_discharge(this_export_limit): # Minor weighting to 0% - metric -= 0.001 + metric -= 0.001 * self.currency_scale # Adjust to try to keep existing windows keep_export = False @@ -3422,11 +3423,11 @@ def optimise_export(self, window_n, record_charge_windows, try_charge_limit, cha # same bonus cancels it out and lets "stop now, restart later in this window" win, which is # the export flapping this commitment exists to prevent. if start <= self.minutes_now: - metric -= max(0.5, self.metric_min_improvement_export) + metric -= max(0.5 * self.currency_scale, self.metric_min_improvement_export) keep_export = True # Round metric to 4 DP - metric = dp4(metric) + metric = round_currency(metric, self.currency_scale) if self.debug_enable: self.log( @@ -3468,7 +3469,7 @@ def optimise_export(self, window_n, record_charge_windows, try_charge_limit, cha # It prevents export oscillation on near-flat multi-slot price peaks where exporting now versus # holding and exporting the adjacent equal-priced slot is otherwise a cost coin-toss each cycle. if keep_export: - min_improvement_scaled = min(min_improvement_scaled, -max(0.5, self.metric_min_improvement_export)) + min_improvement_scaled = min(min_improvement_scaled, -max(0.5 * self.currency_scale, self.metric_min_improvement_export)) # Rank on the metric plus the option's share of the threshold, so every 5 minutes of export has to earn # its share: a step that earns less is only kept when the steps beyond it more than repay it. An export @@ -3494,7 +3495,7 @@ def optimise_export(self, window_n, record_charge_windows, try_charge_limit, cha best_import = import_kwh_battery + import_kwh_house # Store the metric and gate metric for export off - if off_metric == 9999999: + if off_metric == float("inf"): off_metric = metric off_gate = gate @@ -3535,12 +3536,6 @@ def optimise_export(self, window_n, record_charge_windows, try_charge_limit, cha return best_export, best_start, best_metric, best_cost, best_soc_min, best_soc_min_minute, best_keep, best_cycle, best_carbon, best_import, best_metric_plan - def window_sort_func(self, window): - """ - Helper sort index function - """ - return float(window["key"]) - def window_sort_func_start(self, window): """ Helper sort index function @@ -3593,7 +3588,7 @@ def _io_rate_adjustment(self, window_start, run_start): if hours_ahead > IO_ADJUST_DISCOUNT_HORIZON_HOURS: # Distant period: suppress the discount but keep any penalty gradient = max(gradient, 0.0) - return gradient + return gradient * self.currency_scale def sort_window_by_price_combined(self, charge_windows, export_windows, calculate_import_low_export=False, calculate_export_high_import=False): """ @@ -3615,39 +3610,39 @@ def sort_window_by_price_combined(self, charge_windows, export_windows, calculat average = window["average"] / self.inverter_loss / self.battery_loss + self.metric_battery_cycle if self.carbon_enable: carbon_intensity = self.carbon_intensity.get(max(window["start"] - self.minutes_now, 0), 0) - average += dp1(carbon_intensity * self.carbon_metric / 1000.0) + average += round_currency(carbon_intensity * self.carbon_metric / 1000.0, self.currency_scale, 1) if window["start"] in charge_io_run_starts: # IOG (planned-dispatch) slot: apply the earlier-charge skew gradient average += self._io_rate_adjustment(window["start"], charge_io_run_starts[window["start"]]) average += self.metric_self_sufficiency - average = dp2(average) # Round to nearest 0.01 penny to avoid too many bands + average = round_currency(average, self.currency_scale, 2) # Round in calibrated monetary units to bound the number of bands if calculate_import_low_export: - average_export = dp2((self.rate_export.get(window["start"], 0) + self.rate_export.get(window["end"] - PREDICT_STEP, 0)) / 2) + average_export = round_currency((self.rate_export.get(window["start"], 0) + self.rate_export.get(window["end"] - PREDICT_STEP, 0)) / 2, self.currency_scale, 2) else: average_export = 0 window_start = window["start"] - sort_key = "%04.2f_%04.2f_%04d_c%02d" % (5000 - average, 5000 - average_export, 9999 - window_start, id) + sort_key = (0, -average, -average_export, -window_start, "c%02d" % id) window_sort.append(sort_key) window_links[sort_key] = {} window_links[sort_key]["type"] = "c" window_links[sort_key]["id"] = id - window_links[sort_key]["average"] = dp1(average / 2) * 2 # Round to nearest 0.2 penny to avoid too many bands - window_links[sort_key]["average_secondary"] = dp1(average_export) # Round to nearest 0.1 penny to avoid too many bands + window_links[sort_key]["average"] = round_currency(average / 2, self.currency_scale, 1) * 2 # 0.2 calibrated penny bands + window_links[sort_key]["average_secondary"] = round_currency(average_export, self.currency_scale, 1) # 0.1 calibrated penny bands if self.set_charge_freeze: average = window["average"] if self.carbon_enable: carbon_intensity = self.carbon_intensity.get(window["start"] - self.minutes_now, 0) - average += dp1(carbon_intensity * self.carbon_metric / 1000.0) + average += round_currency(carbon_intensity * self.carbon_metric / 1000.0, self.currency_scale, 1) average += self.metric_self_sufficiency - average = dp2(average) # Round to nearest 0.01 penny to avoid too many bands - sort_key = "%04.2f_%04.2f_%04d_cf%02d" % (5000 - average, 5000 - average_export, 9999 - window_start, id) + average = round_currency(average, self.currency_scale, 2) # Round in calibrated monetary units to bound the number of bands + sort_key = (0, -average, -average_export, -window_start, "cf%02d" % id) window_sort.append(sort_key) window_links[sort_key] = {} window_links[sort_key]["type"] = "cf" window_links[sort_key]["id"] = id - window_links[sort_key]["average"] = dp1(average / 2) * 2 # Round to nearest 0.2 penny to avoid too many bands - window_links[sort_key]["average_secondary"] = dp1(average_export) # Round to nearest 0.1 penny to avoid too many bands + window_links[sort_key]["average"] = round_currency(average / 2, self.currency_scale, 1) * 2 # 0.2 calibrated penny bands + window_links[sort_key]["average_secondary"] = round_currency(average_export, self.currency_scale, 1) # 0.1 calibrated penny bands id += 1 @@ -3660,26 +3655,26 @@ def sort_window_by_price_combined(self, charge_windows, export_windows, calculat average = window["average"] * self.inverter_loss * self.battery_loss_discharge - self.metric_battery_cycle if self.carbon_enable: carbon_intensity = self.carbon_intensity.get(max(window["start"] - self.minutes_now, 0), 0) - average += dp1(carbon_intensity * self.carbon_metric / 1000.0) - average = dp1(average) # Round to nearest 0.01 penny to avoid too many bands + average += round_currency(carbon_intensity * self.carbon_metric / 1000.0, self.currency_scale, 1) + average = round_currency(average, self.currency_scale, 1) # Round in calibrated monetary units to bound the number of bands if calculate_export_high_import: - average_import = dp2((self.rate_import.get(window["start"], 0) + self.rate_import.get(window["end"] - PREDICT_STEP, 0)) / 2) + average_import = round_currency((self.rate_import.get(window["start"], 0) + self.rate_import.get(window["end"] - PREDICT_STEP, 0)) / 2, self.currency_scale, 2) if window["start"] in export_io_run_starts: # IOG (planned-dispatch) slot on the import side: apply the earlier-charge skew gradient average_import += self._io_rate_adjustment(window["start"], export_io_run_starts[window["start"]]) else: average_import = 0 window_start = window["start"] - sort_key = "%04.2f_%04.2f_%04d_d%02d" % (5000 - average, 5000 - average_import, 9999 - window_start, id) + sort_key = (0, -average, -average_import, -window_start, "d%02d" % id) if not self.calculate_export_first: # Push export last if first is not set - sort_key = "zz_" + sort_key + sort_key = (1,) + sort_key[1:] window_sort.append(sort_key) window_links[sort_key] = {} window_links[sort_key]["type"] = "d" window_links[sort_key]["id"] = id - window_links[sort_key]["average"] = dp1(average / 2) * 2 # Round to nearest 0.2 penny to avoid too many bands - window_links[sort_key]["average_secondary"] = dp1(average_import) # Round to nearest 0.1 penny to avoid too many bands + window_links[sort_key]["average"] = round_currency(average / 2, self.currency_scale, 1) * 2 # 0.2 calibrated penny bands + window_links[sort_key]["average_secondary"] = round_currency(average_import, self.currency_scale, 1) # 0.1 calibrated penny bands if self.set_export_freeze: pv_period = 0 @@ -3690,18 +3685,18 @@ def sort_window_by_price_combined(self, charge_windows, export_windows, calculat average = window["average"] if self.carbon_enable: carbon_intensity = self.carbon_intensity.get(window["start"] - self.minutes_now, 0) - average += dp1(carbon_intensity * self.carbon_metric / 1000.0) - average = dp2(average) # Round to nearest 0.01 penny to avoid too many bands - sort_key = "%04.2f_%04.2f_%04d_df%02d" % (5000 - average, 5000 - average_import, 9999 - window_start, id) + average += round_currency(carbon_intensity * self.carbon_metric / 1000.0, self.currency_scale, 1) + average = round_currency(average, self.currency_scale, 2) # Round in calibrated monetary units to bound the number of bands + sort_key = (0, -average, -average_import, -window_start, "df%02d" % id) if not self.calculate_export_first: # Push export last if first is not set - sort_key = "zz_" + sort_key + sort_key = (1,) + sort_key[1:] window_sort.append(sort_key) window_links[sort_key] = {} window_links[sort_key]["type"] = "df" window_links[sort_key]["id"] = id - window_links[sort_key]["average"] = dp1(average / 2) * 2 # Round to nearest 0.2 penny to avoid too many bands - window_links[sort_key]["average_secondary"] = dp1(average_import) # Round to nearest 0.1 penny to avoid too many bands + window_links[sort_key]["average"] = round_currency(average / 2, self.currency_scale, 1) * 2 # 0.2 calibrated penny bands + window_links[sort_key]["average_secondary"] = round_currency(average_import, self.currency_scale, 1) # 0.1 calibrated penny bands id += 1 @@ -3758,11 +3753,11 @@ def sort_window_by_price(self, windows, reverse_time=False): for window in window_with_id: window["id"] = wid if reverse_time: - window["key"] = "%04.2f%02d" % (5000 - window["average"], 999 - window["id"]) + window["key"] = (-round_currency(window["average"], self.currency_scale, 2), -window["id"]) else: - window["key"] = "%04.2f%02d" % (5000 - window["average"], window["id"]) + window["key"] = (-round_currency(window["average"], self.currency_scale, 2), window["id"]) wid += 1 - window_with_id.sort(key=self.window_sort_func) + window_with_id.sort(key=lambda window: window["key"]) id_list = [] for window in window_with_id: id_list.append(window["id"]) @@ -3807,7 +3802,7 @@ def discard_unused_charge_slots(self, charge_limit_best, charge_window_best, res # Combine two windows of the same charge target provided the rates are the same or low power mode is off (low power mode can skew the charge into the more expensive slot) new_window_best[-1]["end"] = end new_window_best[-1]["target"] = window.get("target", limit) - new_window_best[-1]["average"] = dp2((new_window_best[-1]["average"] + window["average"]) / 2) + new_window_best[-1]["average"] = round_currency((new_window_best[-1]["average"] + window["average"]) / 2, self.currency_scale, 2) if self.debug_enable: self.log("Combine charge slot {} with previous (same target) - target soc {} kWh slot {} start {} end {} limit {}".format(window_n, new_limit_best[-1], new_window_best[-1], start, end, limit)) elif ( @@ -3926,7 +3921,7 @@ def prune_dead_plan_slots(self): weightings), and the fix was to gate on the unadjusted whole-plan metric, which is what this trial uses. An in-progress export worth anything at all fails the gate and is kept. """ - eps = 0.02 + eps = 0.02 * self.currency_scale soc_eps = 0.1 record_limit = self.end_record + self.minutes_now baseline = None @@ -4204,7 +4199,7 @@ def merge_export_staircase(self): Returns the number of merges made. """ - eps = 0.02 + eps = 0.02 * self.currency_scale windows = list(self.export_window_best) limits = list(self.export_limits_best) baseline = None @@ -4821,7 +4816,7 @@ def optimise_swap_charge(self, record_charge_windows, debug_mode=False): # defaults negative (it deliberately breaks ties towards later windows) which for a # symmetric charge swap would let the metric increase and cause churn, so use a small # positive gate here: a swap is only kept if it lowers the metric by at least this amount. - min_improvement_swap = 0.1 + min_improvement_swap = 0.1 * self.currency_scale swapped_target = {} swapped = True diff --git a/apps/predbat/prediction.py b/apps/predbat/prediction.py index 2beeca3ec..f8b24ca92 100644 --- a/apps/predbat/prediction.py +++ b/apps/predbat/prediction.py @@ -18,6 +18,7 @@ """ from datetime import timedelta +from currency import round_currency from const import KEEP_TARGET_MIN_RATE, PREDICT_STEP, PV_SCENARIO_NOMINAL, PV_SCENARIO_PV10, PV_SCENARIO_PV90, RUN_EVERY, TIME_FORMAT, EXPORT_LIMIT_FREEZE, EXPORT_LIMIT_IDLE, EXPORT_MODE_TARGET, EXPORT_MODE_FREEZE, EXPORT_MODE_IDLE from utils import ( @@ -99,6 +100,7 @@ def __init__( car_charging_slots_model() so a car charging now outside its plan is modelled too. """ if base: + self.currency_scale = base.currency_scale self.minutes_now = base.minutes_now self.log = base.log self.time_abs_str = base.time_abs_str @@ -885,7 +887,7 @@ def run_prediction(self, charge_limit, charge_window, export_window, export_limi minute_timestamp = self.midnight_utc + timedelta(seconds=60 * minute_absolute) stamp = minute_timestamp.strftime(TIME_FORMAT) predict_soc_time[stamp] = round(soc, 3) - metric_time[stamp] = round(metric, 3) + metric_time[stamp] = round_currency(metric, self.currency_scale, 3) load_kwh_time[stamp] = round(load_kwh, 3) pv_kwh_time[stamp] = round(pv_kwh, 2) import_kwh_time[stamp] = round(import_kwh, 2) @@ -896,7 +898,7 @@ def run_prediction(self, charge_limit, charge_window, export_window, export_limi record_time[stamp] = 0 if record else soc_max if enable_save_stats or enable_save_scenario: self.predict_soc_best[minute] = round(soc, 3) - self.predict_metric_best[minute] = round(metric, 3) + self.predict_metric_best[minute] = round_currency(metric, self.currency_scale, 3) if enable_save_stats: self.predict_iboost_best[minute] = round(iboost_today_kwh, 2) self.predict_carbon_best[minute] = round(carbon_g, 0) @@ -988,13 +990,13 @@ def run_prediction(self, charge_limit, charge_window, export_window, export_limi # Boost on gas vs import rate if self.iboost_gas and self.rate_gas: - gas_rate = self.rate_gas.get(minute_absolute, 99) * self.iboost_gas_scale + gas_rate = self.rate_gas.get(minute_absolute, 99 * self.currency_scale) * self.iboost_gas_scale if import_rate > gas_rate: iboost_rate_okay = False # Boost on gas vs export rate if self.iboost_gas_export and self.rate_gas: - gas_rate = self.rate_gas.get(minute_absolute, 99) * self.iboost_gas_scale + gas_rate = self.rate_gas.get(minute_absolute, 99 * self.currency_scale) * self.iboost_gas_scale if export_rate > gas_rate: iboost_rate_okay = False @@ -1418,13 +1420,13 @@ def run_prediction(self, charge_limit, charge_window, export_window, export_limi # Metric keep - pretend the battery is empty and you have to import instead of using the battery if best_soc_keep > 0 and soc <= best_soc_keep: # A manual or alert floor still costs something to miss at a free or negative rate (#5162) - keep_rate = max(import_rate, KEEP_TARGET_MIN_RATE) if alert_keep > 0 else import_rate + keep_rate = max(import_rate, KEEP_TARGET_MIN_RATE * self.currency_scale) if alert_keep > 0 else import_rate metric_keep += (best_soc_keep - soc) * keep_rate * keep_minute_scaling * step / 60.0 # Metric keep max - pretend the excess above the ceiling should have been exported instead of held if best_soc_max >= 0 and soc >= best_soc_max: # Likewise a ceiling still costs something to break at a free or negative export rate (#5162) - metric_keep += (soc - best_soc_max) * max(export_rate, KEEP_TARGET_MIN_RATE) * keep_minute_scaling * step / 60.0 + metric_keep += (soc - best_soc_max) * max(export_rate, KEEP_TARGET_MIN_RATE * self.currency_scale) * keep_minute_scaling * step / 60.0 if diff > 0: # Import @@ -1539,8 +1541,8 @@ def run_prediction(self, charge_limit, charge_window, export_window, export_limi self.final_car_soc = final_car_soc self.predict_car_soc_time = predict_car_soc_time self.final_soc = round(final_soc, 4) - self.final_metric = round(final_metric, 4) - self.final_metric_keep = round(final_metric_keep, 4) + self.final_metric = round_currency(final_metric, self.currency_scale) + self.final_metric_keep = round_currency(final_metric_keep, self.currency_scale) self.final_import_kwh = round(final_import_kwh, 4) self.final_import_kwh_battery = round(final_import_kwh_battery, 4) self.final_import_kwh_house = round(final_import_kwh_house, 4) @@ -1578,7 +1580,7 @@ def run_prediction(self, charge_limit, charge_window, export_window, export_limi if sim_hash is not None: self.prediction_cache[sim_hash] = ( - round(final_metric, 4), + round_currency(final_metric, self.currency_scale), round(final_import_kwh_battery, 4), round(final_import_kwh_house, 4), round(final_export_kwh, 4), @@ -1586,7 +1588,7 @@ def run_prediction(self, charge_limit, charge_window, export_window, export_limi round(final_soc, 4), soc_min_minute, round(final_battery_cycle, 4), - round(final_metric_keep, 4), + round_currency(final_metric_keep, self.currency_scale), round(final_iboost_kwh, 4), round(final_carbon_g, 4), [], @@ -1598,7 +1600,7 @@ def run_prediction(self, charge_limit, charge_window, export_window, export_limi ) return ( - round(final_metric, 4), + round_currency(final_metric, self.currency_scale), round(final_import_kwh_battery, 4), round(final_import_kwh_house, 4), round(final_export_kwh, 4), @@ -1606,7 +1608,7 @@ def run_prediction(self, charge_limit, charge_window, export_window, export_limi round(final_soc, 4), soc_min_minute, round(final_battery_cycle, 4), - round(final_metric_keep, 4), + round_currency(final_metric_keep, self.currency_scale), round(final_iboost_kwh, 4), round(final_carbon_g, 4), predict_soc if series else {}, diff --git a/apps/predbat/prediction_kernel.cpp b/apps/predbat/prediction_kernel.cpp index df9884a8d..bb2824f3b 100644 --- a/apps/predbat/prediction_kernel.cpp +++ b/apps/predbat/prediction_kernel.cpp @@ -62,7 +62,7 @@ // ABI 11: the ten scalar PkResult fields from final_metric to final_carbon_g are returned already // rounded to 4dp (round4_fast), and Python no longer rounds them. The layout is unchanged, but a binary // from before this returns them unrounded, so it must be refused rather than read. -#define PK_ABI_VERSION 12 +#define PK_ABI_VERSION 13 #define PK_CURVE_WIDTH 103 // a rate grid curve row: 101 SoC percents, the rated rate, then the absolute temperature cap // Parity 14: the explicit (mode, target, power) fields replace the packed double throughout the hot // loop (see the ABI 6 note); the floor a target exports to now reads the target field, matching @@ -81,7 +81,8 @@ // Parity 20: below a manual_soc or alert floor the keep penalty's import rate, and above a manual_soc_max // ceiling its export rate, is at least PK_KEEP_TARGET_MIN_RATE, so a target in a free or negative-price // slot still costs something to miss (#5162). -#define PK_PARITY_REVISION 23 +// Parity 24: calibrated monetary floor and currency-relative prediction rounding. +#define PK_PARITY_REVISION 24 #define PK_MAX_CARS 8 #define PK_RUN_EVERY 5 // const.py RUN_EVERY #define PK_KEEP_TARGET_MIN_RATE 1.0 // const.py KEEP_TARGET_MIN_RATE @@ -299,6 +300,7 @@ struct PkContext { int32_t iboost_on_export; int32_t has_rate_gas; int32_t has_iboost_plan; + double currency_scale; // ABI 13: same economic calibration as Python Prediction }; // Per-scenario inputs; field order MUST match the ctypes Structure in prediction_kernel.py. @@ -1463,7 +1465,7 @@ static int32_t pk_run_one(const ContextStore *store, const PkScenario *s, PkResu // Metric keep - prediction.py:1100-1102 if (best_soc_keep > 0 && soc <= best_soc_keep) { // A manual or alert floor still costs something to miss at a free or negative rate (#5162) - const double keep_rate = (alert_keep > 0) ? std::max(import_rate, PK_KEEP_TARGET_MIN_RATE) : import_rate; + const double keep_rate = (alert_keep > 0) ? std::max(import_rate, PK_KEEP_TARGET_MIN_RATE * c->currency_scale) : import_rate; metric_keep += (best_soc_keep - soc) * keep_rate * keep_minute_scaling * step / 60.0; } @@ -1471,7 +1473,7 @@ static int32_t pk_run_one(const ContextStore *store, const PkScenario *s, PkResu // of held - mirrors prediction.py's best_soc_max block right after the floor. if (best_soc_max >= 0 && soc >= best_soc_max) { // Likewise a ceiling still costs something to break at a free or negative export rate (#5162) - metric_keep += (soc - best_soc_max) * std::max(export_rate, PK_KEEP_TARGET_MIN_RATE) * keep_minute_scaling * step / 60.0; + metric_keep += (soc - best_soc_max) * std::max(export_rate, PK_KEEP_TARGET_MIN_RATE * c->currency_scale) * keep_minute_scaling * step / 60.0; } // Import/export accounting - prediction.py:1104-1143 @@ -1542,15 +1544,16 @@ static int32_t pk_run_one(const ContextStore *store, const PkScenario *s, PkResu } } - // Returned rounded to 4dp (see round4_fast and ABI 11), so the Python side copies them as they are - out->final_metric = round4_fast(final_metric); + // Most fields are pre-rounded to 4dp. At non-unit currency_scale, final_metric and metric_keep + // remain unrounded until Python applies round_currency() to avoid losing fine monetary boundaries. + out->final_metric = (c->currency_scale == 1.0) ? round4_fast(final_metric) : final_metric; out->import_kwh_battery = round4_fast(final_import_kwh_battery); out->import_kwh_house = round4_fast(final_import_kwh_house); out->export_kwh = round4_fast(final_export_kwh); out->soc_min = round4_fast(soc_min); out->final_soc = round4_fast(final_soc); out->battery_cycle = round4_fast(final_battery_cycle); - out->metric_keep = round4_fast(final_metric_keep); + out->metric_keep = (c->currency_scale == 1.0) ? round4_fast(final_metric_keep) : final_metric_keep; out->final_iboost = round4_fast(final_iboost_kwh); out->final_carbon_g = round4_fast(final_carbon_g); for (int32_t car_n = 0; car_n < PK_MAX_CARS; car_n++) { diff --git a/apps/predbat/prediction_kernel.py b/apps/predbat/prediction_kernel.py index 440b3c95d..ad652cd7e 100644 --- a/apps/predbat/prediction_kernel.py +++ b/apps/predbat/prediction_kernel.py @@ -22,6 +22,8 @@ import array import ctypes +from currency import round_currency + import struct import os import platform @@ -32,8 +34,8 @@ from utils import in_car_slot, in_iboost_slot # Expected ABI/parity revisions of the shared library (see prediction_kernel.cpp) -KERNEL_ABI_VERSION = 12 -KERNEL_PARITY_REVISION = 23 +KERNEL_ABI_VERSION = 13 +KERNEL_PARITY_REVISION = 24 # Maximum number of cars supported by the kernel (PK_MAX_CARS in prediction_kernel.cpp) KERNEL_MAX_CARS = PREDBAT_MAX_CARS @@ -138,6 +140,7 @@ class PkContext(ctypes.Structure): ("iboost_on_export", ctypes.c_int32), ("has_rate_gas", ctypes.c_int32), ("has_iboost_plan", ctypes.c_int32), + ("currency_scale", ctypes.c_double), ] @@ -748,7 +751,7 @@ def build_static_context_arrays(pred, n_steps, minutes_now, num_cars): discharge_curve_row.append(step_rows[1]) carbon.append(pred.carbon_intensity.get(minute, 0) if pred.carbon_intensity else 0) # Gas rate pre-scaled by iboost_gas_scale - mirrors prediction.py:719/725 - gas_rate.append((pred.rate_gas.get(minute_absolute, 99) * pred.iboost_gas_scale) if pred.rate_gas else 0) + gas_rate.append((pred.rate_gas.get(minute_absolute, 99 * pred.currency_scale) * pred.iboost_gas_scale) if pred.rate_gas else 0) iboost_plan_load.append(in_iboost_slot(minute_absolute, pred.iboost_plan) if pred.iboost_plan else 0) if num_cars > 0: car_load, car_rate_slot = in_car_slot(minute_absolute, num_cars, pred.car_charging_slots) @@ -898,6 +901,7 @@ def create_kernel_context(pred, static_cache=None): ctx.iboost_on_export = 1 if pred.iboost_on_export else 0 ctx.has_rate_gas = 1 if pred.rate_gas else 0 ctx.has_iboost_plan = 1 if pred.iboost_plan else 0 + ctx.currency_scale = pred.currency_scale handle = lib.pk_context_create(ctypes.byref(ctx)) if handle: @@ -925,11 +929,12 @@ def kernel_result_tuple(pred, result, predict_soc): Shared by the single-scenario and batch paths - mirrors prediction.py:626-628, 1266-1284. - The ten scalar fields arrive already rounded to 4dp (the kernel's round4_fast, ABI 11), so they - are copied as they are. round(x, 4) on each was most of what converting a result cost - about - 1.5us of the 1.7us per simulation, paid on the calling thread for every job in a batch. The kernel - rounds the scaled value rather than the exact binary one, so a field can sit one unit in the 4th - decimal place away from what round(x, 4) gives; test_kernel_parity allows for exactly that. + Most scalar fields arrive rounded to four decimal places by the native kernel and can be copied + directly, avoiding per-field Python rounding in large batches. At currency_scale == 1 the + final_metric and metric_keep fields follow that same fast path. At a non-unit scale the kernel + returns those two monetary fields unrounded; Python applies round_currency() at this boundary + so a fixed native 4dp pre-round cannot erase a currency-calibrated decision boundary. + Small native-vs-Python binary rounding differences at scale 1 are covered by kernel parity tests. """ car_charging_soc_next = pred.car_charging_soc_next[:] if result.car_soc_next_valid: @@ -939,9 +944,10 @@ def kernel_result_tuple(pred, result, predict_soc): car_charging_soc_next[car_n] = car_soc_next[car_n] iboost_next = result.iboost_next if pred.iboost_enable else pred.iboost_next + currency_scale = pred.currency_scale return ( - result.final_metric, + round_currency(result.final_metric, currency_scale) if currency_scale != 1.0 else result.final_metric, result.import_kwh_battery, result.import_kwh_house, result.export_kwh, @@ -949,7 +955,7 @@ def kernel_result_tuple(pred, result, predict_soc): result.final_soc, result.soc_min_minute, result.battery_cycle, - result.metric_keep, + round_currency(result.metric_keep, currency_scale) if currency_scale != 1.0 else result.metric_keep, result.final_iboost, result.final_carbon_g, predict_soc, diff --git a/apps/predbat/prediction_kernel_lib_aarch64.so b/apps/predbat/prediction_kernel_lib_aarch64.so index 7041c162b..ed279063b 100755 Binary files a/apps/predbat/prediction_kernel_lib_aarch64.so and b/apps/predbat/prediction_kernel_lib_aarch64.so differ diff --git a/apps/predbat/prediction_kernel_lib_armv7l.so b/apps/predbat/prediction_kernel_lib_armv7l.so index f19bf75eb..cc636a7ac 100755 Binary files a/apps/predbat/prediction_kernel_lib_armv7l.so and b/apps/predbat/prediction_kernel_lib_armv7l.so differ diff --git a/apps/predbat/prediction_kernel_lib_darwin_arm64.so b/apps/predbat/prediction_kernel_lib_darwin_arm64.so index 6ebfc2abf..da889a25f 100755 Binary files a/apps/predbat/prediction_kernel_lib_darwin_arm64.so and b/apps/predbat/prediction_kernel_lib_darwin_arm64.so differ diff --git a/apps/predbat/prediction_kernel_lib_darwin_x86_64.so b/apps/predbat/prediction_kernel_lib_darwin_x86_64.so index cfb6d9ea3..2e6d86b89 100755 Binary files a/apps/predbat/prediction_kernel_lib_darwin_x86_64.so and b/apps/predbat/prediction_kernel_lib_darwin_x86_64.so differ diff --git a/apps/predbat/prediction_kernel_lib_i686.so b/apps/predbat/prediction_kernel_lib_i686.so index 78f877577..f027ff505 100755 Binary files a/apps/predbat/prediction_kernel_lib_i686.so and b/apps/predbat/prediction_kernel_lib_i686.so differ diff --git a/apps/predbat/prediction_kernel_lib_x86_64.so b/apps/predbat/prediction_kernel_lib_x86_64.so index 1c1cc6f7d..275f21bb2 100755 Binary files a/apps/predbat/prediction_kernel_lib_x86_64.so and b/apps/predbat/prediction_kernel_lib_x86_64.so differ diff --git a/apps/predbat/tests/test_compare.py b/apps/predbat/tests/test_compare.py index 1f66e3b42..4140a2e7f 100644 --- a/apps/predbat/tests/test_compare.py +++ b/apps/predbat/tests/test_compare.py @@ -51,6 +51,8 @@ def __init__(self): self.config_root = "." self.prefix = "predbat" self.currency_symbols = ["£", "p"] + # PredBat.reset() establishes this attribute before the Compare engine is used. + self.currency_scale = 1.0 self.comparisons = {} def log(self, msg): diff --git a/apps/predbat/tests/test_currency_calibration.py b/apps/predbat/tests/test_currency_calibration.py new file mode 100644 index 000000000..2652bedc8 --- /dev/null +++ b/apps/predbat/tests/test_currency_calibration.py @@ -0,0 +1,536 @@ +"""Currency calibration regressions using the native HA fixture and real engines.""" + +import copy +import math +import random +import time +from contextlib import ExitStack +from types import SimpleNamespace +from unittest.mock import patch + +from config import CONFIG_ITEMS, CURRENCY_UNITS +from const import EXPORT_MODE_IDLE, EXPORT_MODE_TARGET, PV_SCENARIO_NOMINAL, PREDBAT_MAX_CARS +from currency import round_currency + +from prediction import Prediction +from prediction_kernel import build_static_context_arrays +from tests.test_export_commitment import setup_single_export_window +from tests.test_infra import TestHAInterface, clear_manual_times, reset_inverter, reset_rates, update_rates_import, update_rates_export +from tests.test_kernel_parity import apply_random_scenario, dual_run, make_step_data, make_windows +from utils import pack_export_limit + +MONETARY_UNITS = frozenset(CURRENCY_UNITS) + + +def configured_predbat(scale=None, overrides=None): + """Use an isolated HA fixture with no stored monetary values and explicit overrides.""" + from unit_test import create_predbat + + base = create_predbat() + for item in base.CONFIG_ITEMS: + if item.get("unit") in MONETARY_UNITS: + base.args.pop(item["name"], None) + base.args.pop("currency_scale", None) + if scale is not None: + base.args["currency_scale"] = scale + base.args.update(overrides or {}) + # Upstream PR #6156 applies its ranges at reset(), not at every reload. + base.reset() + base.update_time(print=False) + base.ha_interface = TestHAInterface() + base.ha_interface.base = base + base.ha_interface.history_enable = False + base.args["expert_mode"] = True + with patch.object(base, "load_previous_value_from_ha", return_value=None): + base.load_user_config() + # Model equivalent manually chosen settings for the cross-scale scenario tests. + # This is test input normalisation, NOT a change to production defaults. + if isinstance(scale, (int, float)) and not isinstance(scale, bool) and math.isfinite(scale) and scale >= 1 and scale != 1: + # Only scenario tests explicitly opt into equivalent manually supplied + # monetary settings. No runtime/default conversion occurs in Predbat. + for item in base.CONFIG_ITEMS: + if item.get("type") == "input_number" and item.get("unit") in MONETARY_UNITS and item["name"] not in (overrides or {}): + value = item.get("value") + if isinstance(value, (int, float)) and not isinstance(value, bool): + item["value"] = value * scale + base.fetch_config_options() + return base + + +def test_currency_configuration(): + """Upstream owns bounds and validation; the extension adds steps, not default conversion.""" + canonical = copy.deepcopy(CONFIG_ITEMS) + default = configured_predbat(1) + scaled = configured_predbat(29) + fractional = configured_predbat(8.5) + for item in CONFIG_ITEMS: + name = item["name"] + if item.get("type") != "input_number" or item.get("unit") not in MONETARY_UNITS: + continue + for field in ("min", "max", "step"): + assert math.isclose(default.config_index[name][field], item[field]), (name, field) + assert math.isclose(scaled.config_index[name][field], item[field] * 29), (name, field) + assert math.isclose(fractional.config_index[name][field], item[field] * 8.5), (name, field) + # Upstream policy: defaults stay unchanged and are not an exchange-rate service. + assert scaled.config_index[name]["default"] == item["default"], (name, "default") + assert scaled.currency_scale == 29 + assert default.currency_scale == 1 + for bad in (0, -1, math.nan, math.inf, -math.inf, "bad", "nan"): + check = configured_predbat(bad) + assert check.currency_scale == 1.0, bad + assert CONFIG_ITEMS == canonical + + # A value above the declared money range is preserved instead of silently + # clamped; non-money settings still retain the original clamp logic. + explicit = configured_predbat(29, {"metric_min_improvement_plan": 8000}) + assert explicit.config_index["metric_min_improvement_plan"]["value"] == 8000 + assert explicit.config_index["metric_min_improvement_plan"]["max"] >= 8000 + + +def test_currency_value_range_compatibility(): + """The default scale keeps the original clamp; calibrated installs retain saved outliers.""" + unscaled = configured_predbat(1, {"metric_battery_cycle": 500}) + assert unscaled.config_index["metric_battery_cycle"]["value"] == 50 + assert unscaled.config_index["metric_battery_cycle"]["max"] == 50 + + invalid = configured_predbat("nan", {"metric_battery_cycle": 500}) + assert invalid.currency_scale == 1.0 + assert invalid.config_index["metric_battery_cycle"]["value"] == 50 + assert invalid.config_index["metric_battery_cycle"]["max"] == 50 + + scaled = configured_predbat(29, {"metric_battery_cycle": 2000}) + assert scaled.config_index["metric_battery_cycle"]["value"] == 2000 + assert scaled.config_index["metric_battery_cycle"]["max"] == 2000 + + +def test_currency_prices_and_sorting(): + """Keep incoming prices intact and order large/negative prices without collisions.""" + for scale in (1.0, 8.5, 29.0): + base = configured_predbat(scale) + reset_rates(base, 363.894, 0) + base.set_rate_thresholds() + assert base.rate_import[base.minutes_now] == 363.894 + assert abs(base.rate_import_cost_threshold - (round_currency(363.894, scale, 2) + 0.1 * scale)) < 1e-9 + base.rate_import = {base.minutes_now + i: value * scale for i, value in enumerate((-200000, -100000))} + low, high, _, _, _ = base.rate_minmax(base.rate_import) + assert math.isclose(low / scale, -200000) + assert math.isclose(high / scale, -100000) + base.rate_import = {base.minutes_now: 200000 * scale} + assert math.isclose(base.rate_minmax(base.rate_import)[0] / scale, 200000) + assert base.rate_minmax({})[:3] == (99999, 0, 0) + base.prediction = SimpleNamespace(pv_forecast_minute_step={}) + base.calculate_best_charge = True + base.calculate_best_export = True + base.carbon_enable = False + base.inverter_loss = base.battery_loss = base.battery_loss_discharge = 1 + base.metric_battery_cycle = base.metric_self_sufficiency = 0 + base.set_charge_freeze = base.set_export_freeze = False + prices = [-10000, 0, 10, 10, 5001, 100000] + windows = [{"start": 720 + i * 30, "end": 750 + i * 30, "average": price * scale} for i, price in enumerate(prices)] + assert base.sort_window_by_price(windows) == [5, 4, 2, 3, 1, 0] + assert base.sort_window_by_price(windows, reverse_time=True) == [5, 4, 3, 2, 1, 0] + ordered, links, bands, _ = base.sort_window_by_price_combined(windows, []) + assert [links[key]["id"] for key in ordered] == [5, 4, 3, 2, 1, 0] + assert len(ordered) == len(set(ordered)) == 6 + assert len(bands) == 5 + base.minutes_now = 720 + assert math.isclose(base._io_rate_adjustment(720, 720), -1.5 * scale) + base.rate_min_forward = {720: 0} + base.rate_min = base.rate_max = base.rate_max_base = base.rate_export_min = 0 + assert base.battery_value_rate(720) == scale + # Rounding precision is relative to calibration, not to the currency symbol. + assert math.isclose(round_currency(0.123456 * scale, scale) / scale, 0.1235, abs_tol=1e-15) + + +def run_currency_planner(scale, price_pair=(2.0, 30.0)): + """Optimise the same six-hour physical problem and return plan/candidate evidence.""" + base = configured_predbat(scale) + reset_inverter(base) + base.minutes_now = 0 + base.now_utc = base.midnight_utc + base.forecast_minutes = 360 + base.end_record = 360 + base.soc_max = 4.0 + base.soc_kw = 0.5 + base.best_soc_step = 0.25 + base.calculate_best_charge = base.calculate_best_export = True + base.calculate_export_oncharge = True + base.debug_enable = False + base.prediction_kernel_enable = True + base.rate_import = {} + base.rate_export = {} + reset_rates(base, 12 * scale, 5 * scale) + charge = [{"start": i * 30, "end": 30 + i * 30, "average": price_pair[0] * scale} for i in range(3)] + export = [{"start": 180 + i * 30, "end": 210 + i * 30, "average": price_pair[1] * scale} for i in range(3)] + update_rates_import(base, charge) + update_rates_export(base, export) + base.rate_min_forward = base.rate_min_forward_calc(base.rate_import) + base.rate_max_base = base.rate_max + pv = {minute: 0.0 for minute in range(0, 360, 5)} + load = {minute: 0.04 for minute in range(0, 360, 5)} + base.prediction = Prediction(base, pv, pv, load, load) + ordered, links, prices, price_links = base.sort_window_by_price_combined(charge, export) + base.optimise_charge_windows_reset(reset_all=True) + base.optimise_charge_windows_manual() + start = time.monotonic() + result = base.optimise_charge_limit_price_threads(prices, price_links, links, 3, 3, [0] * 3, charge, export, [pack_export_limit(EXPORT_MODE_IDLE)] * 3, end_record=360, fast=True, quiet=True) + elapsed = time.monotonic() - start + # Actual candidate counts, not just elapsed time, catch a scale-dependent explosion. + evidence = (len(prices), len(ordered), len(result[10])) + print("currency benchmark scale={} bands/windows/tried={} time={:.4f}s".format(scale, evidence, elapsed)) + return result, evidence + + +def test_currency_planner_equivalence(): + """Verify actual optimisation and thresholds at ordinary, zero and negative prices.""" + for pair in ((2, 30), (-2, 0), (0, 0), (12, 12)): + reference, counts = run_currency_planner(1, pair) + for scale in (8.5, 29): + result, scaled_counts = run_currency_planner(scale, pair) + assert result[:2] == reference[:2], (pair, scale, reference[:2], result[:2]) + assert scaled_counts == counts, (pair, scale, counts, scaled_counts) + for index in (2, 3, 4, 9): + # Each engine/metric boundary rounds at 1e-4 calibrated penny. + assert abs(result[index] / scale - reference[index]) <= 0.0002, (pair, scale, index, result[index], reference[index]) + + +def test_currency_kernel_parity(): + """Exercise real compiled C++ and Python at three scales, including iBoost/EV.""" + for seed in range(6): + reference = None + for scale in (1, 8.5, 29): + base = configured_predbat(scale) + reset_inverter(base) + base.car_charging_slots = [[] for _ in range(PREDBAT_MAX_CARS)] + rng = random.Random(seed) + apply_random_scenario(base, rng) + charge_window = make_windows(rng, base.minutes_now, base.forecast_minutes, 3) + export_window = make_windows(rng, base.minutes_now, base.forecast_minutes, 3) + charge_limit = [base.soc_max] * len(charge_window) + export_limits = [0] * len(export_window) + end_record = base.forecast_minutes + for name in ("rate_import", "rate_export", "rate_gas"): + setattr(base, name, {minute: value * scale for minute, value in getattr(base, name).items()}) + for name in ("cost_today_sofar", "metric_standing_charge", "iboost_rate_threshold", "iboost_rate_threshold_export"): + setattr(base, name, getattr(base, name) * scale) + for slots in base.car_charging_slots: + for slot in slots: + slot["average"] = slot.get("average", 0) * scale + # A nonempty but incomplete gas series exercises the priced 99 fallback. + if seed == 0: + base.iboost_enable = base.iboost_gas = base.iboost_gas_export = True + base.rate_gas = {-1: 7 * scale} + data = make_step_data(base, rng) + base.prediction_kernel_enable = False + prediction = Prediction(base, *data) + if seed == 0: + arrays = build_static_context_arrays(prediction, base.forecast_minutes // 5, base.minutes_now, base.num_cars) + assert all(math.isclose(value, 99 * scale * base.iboost_gas_scale) for value in arrays[12]) + simulation = prediction.run_prediction(charge_limit, charge_window, export_window, [pack_export_limit(0, 0, 1) for _ in export_window], PV_SCENARIO_NOMINAL, end_record) + if reference is None: + reference = simulation + else: + for index in range(11): + value = simulation[index] / scale if index in (0, 8) else simulation[index] + assert abs(value - reference[index]) < 0.0002, (seed, scale, index, value, reference[index]) + assert not dual_run("currency_{}_{}".format(seed, scale), base, *data, charge_limit, charge_window, export_window, export_limits, PV_SCENARIO_NOMINAL, end_record) + + +def test_currency_decision_boundaries(): + """Check both sides of pruning and swap gates at calibrated monetary precision.""" + for scale in (1, 8.5, 29): + base = configured_predbat(scale) + base.metric_min_improvement_plan = 2 * scale + base.io_confirmed_tiebreak = False + assert base.should_replace_plan(100 * scale, 98 * scale, 1, 1) + assert not base.should_replace_plan(100 * scale, 98.001 * scale, 1, 1) + clear_manual_times(base) + base.end_record = 120 + base.export_window_best = [] + base.export_limits_best = [] + for gap, keep in ((0.019, False), (0.021, True)): + base.charge_window_best = [{"start": base.minutes_now, "end": base.minutes_now + 30, "average": 10 * scale}] + base.charge_limit_best = [2] + with patch.object(base, "run_prediction_metric", side_effect=lambda charge, *args, **kwargs: ((100 + (gap if charge[0] == 0 else 0)) * scale,) * 8): + base.prune_dead_plan_slots() + assert (base.charge_limit_best[0] == 2) == keep + for gain, keep in ((0.099, False), (0.101, True)): + base.calculate_best_charge = True + base.calculate_export_oncharge = True + base.charge_window_best = [{"start": base.minutes_now + i * 30, "end": base.minutes_now + (i + 1) * 30} for i in range(2)] + base.charge_limit_best = [2, 0] + + def metric(charge, *args, **kwargs): + """Give the later-slot swap a controlled, calibrated improvement.""" + value = (100 - gain if charge == [0, 2] else 100) * scale + return (value,) * 8 + + # Since current upstream swap candidates are queued via launch_run_prediction_metric + # and consumed through get_run_prediction_metric, rather than by calling + # run_prediction_metric synchronously. + with ( + patch.object(base, "launch_run_prediction_metric", side_effect=lambda charge, *args, **kwargs: charge), + patch.object(base, "get_run_prediction_metric", side_effect=lambda charge: metric(charge)), + ): + base.optimise_swap_charge(2) + assert (base.charge_limit_best == [0, 2]) == keep + + +def test_currency_export_commitment(): + """Compare actual running-export score bonuses and configured gates at three scales.""" + for exporting in (False, True): + for gate in (0.1, 1.0): + reference = None + for scale in (1, 8.5, 29): + base = configured_predbat(scale) + windows, count, end_record = setup_single_export_window(base, rate_import=10 * scale, rate_export=30 * scale, battery_size=1, battery_soc=1) + base.metric_min_improvement_export = gate * scale + base.isExporting = exporting + base.export_window = copy.deepcopy(windows) if exporting else [] + result = base.optimise_export(0, count, [], [], windows, [pack_export_limit(0, 0)], end_record=end_record) + if reference is None: + reference = result + assert result[0][0] != EXPORT_MODE_IDLE, "Test must exercise a real export" + else: + assert result[:2] == reference[:2], (exporting, gate, scale, result[:2], reference[:2]) + for index in (2, 3, 6, 10): + assert abs(result[index] / scale - reference[index]) <= 0.0002, (exporting, gate, scale, index) + + +def test_currency_merge_and_compare(): + """Keep merge decisions, tariff identity and tariff ranking scale invariant.""" + from compare import Compare + + for scale in (1, 8.5, 29): + base = configured_predbat(scale) + base.minutes_now = 0 + base.forecast_minutes = 60 + base.predict_soc = {} + clear_manual_times(base) + base.all_active_keep = [] + base.set_charge_low_power = True + windows = [{"start": n * 30, "end": (n + 1) * 30, "average": rate * scale} for n, rate in enumerate((10.004, 10.002, 10.001))] + limits, merged = base.discard_unused_charge_slots([5, 5, 5], windows, 0) + assert limits == [5, 5], (scale, merged) + assert [(window["start"], window["end"]) for window in merged] == [(0, 60), (60, 90)] + + with patch.object(Compare, "load_yaml"): + comparison = Compare(base) + # 0.004 and 0.006 lie on opposite sides of the identity rounding boundary. + for delta, expected in ((0.004, True), (0.006, False)): + for side in ("import", "export"): + rates = {minute: 10 * scale for minute in range(60)} + tariff = {"id": "boundary", "rates_" + side: [{"rate": (10 + delta) * scale}]} + assert comparison.fetch_rates(tariff, rates, rates) == expected, (scale, side, delta) + + # Exercise the real result construction: its metric is later used for ranking. + base.pv_forecast_minute_step = {} + base.pv_forecast_minute10_step = {} + base.load_minutes_step = {} + base.load_minutes_step10 = {} + results = {} + for name, metric in (("higher", 10.016), ("lower", 10.006)): + with ( + patch.object(base, "calculate_plan"), + patch.object(base, "run_prediction", return_value=(0,) * 11), + patch.object(base, "compute_metric", side_effect=[(0, 0), (metric * scale, 0)]), + patch.object(base, "publish_html_plan", return_value=("", {})), + ): + results[name] = comparison.run_scenario(60) + comparison.select_best([{"id": name} for name in results], results) + assert results["lower"]["best"] and not results["higher"]["best"], (scale, results) + large = {"large": {"metric": 1e12 * scale}} + comparison.select_best([{"id": "large"}], large) + assert large["large"]["best"] + + +def test_currency_v960_boundaries(): + """Keep new 9.6.0 tariff identity and export staircase gates scale invariant.""" + from compare import Compare + + for scale in (1, 8.5, 29): + base = configured_predbat(scale) + base.minutes_now = 0 + with patch.object(Compare, "load_yaml"): + comparison = Compare(base) + for delta, expected in ((0.004, True), (0.006, False)): + assert comparison.is_live_published({0: (10 + delta) * scale}, {0: 10 * scale}) == expected + assert not comparison.is_live_published({}, {0: 10 * scale}) + clear_manual_times(base) + base.all_active_keep_max = {} + base.debug_enable = False + base.charge_window_best = [] + base.charge_limit_best = [] + for gap, expected in ((0.019, 1), (0.021, 0)): + base.export_window_best = [{"start": 0, "end": 30, "average": 10 * scale}, {"start": 30, "end": 60, "average": 10 * scale}] + base.export_limits_best = [pack_export_limit(EXPORT_MODE_TARGET, 50), pack_export_limit(EXPORT_MODE_TARGET, 20)] + with patch.object(base, "run_prediction_metric", side_effect=[(100 * scale,), ((100 + gap) * scale,)]): + assert base.merge_export_staircase() == expected, (scale, gap) + + +def test_currency_saved_scenarios(): + """Preserve calibrated per-minute costs in the v9.7.10 PV10 and PV90 series.""" + for save in ("best10", "best90"): + reference = None + for scale in (1, 8.5, 29): + base = configured_predbat(scale) + reset_inverter(base) + reset_rates(base, 0, 0) + base.minutes_now = 0 + base.forecast_minutes = 60 + base.soc_kw = base.reserve = 0 + base.cost_today_sofar = base.metric_standing_charge = 0 + base.rate_import = {minute: 0.123456 * scale for minute in range(120)} + base.rate_export = {minute: 0 for minute in range(120)} + pv = {minute: 0.0 for minute in range(0, 60, 5)} + load = {minute: 0.04 for minute in range(0, 60, 5)} + prediction = Prediction(base, pv, pv, load, load) + prediction.run_prediction([], [], [], [], PV_SCENARIO_NOMINAL, 60, save=save) + metrics = {minute: value / scale for minute, value in prediction.predict_metric_best.items()} + assert metrics and any(value != 0 for value in metrics.values()), (save, scale, metrics) + if reference is None: + reference = (metrics, prediction.predict_soc_best.copy()) + else: + assert prediction.predict_soc_best == reference[1], (save, scale) + assert metrics.keys() == reference[0].keys(), (save, scale) + assert all(math.isclose(value, reference[0][minute], abs_tol=1e-12) for minute, value in metrics.items()), (save, scale, metrics, reference[0]) + + +def test_currency_manual_soc_target_floor(): + """Backport #6140 with its 1 minor-unit keep floor calibrated for manual SoC boundaries.""" + for ceiling in (False, True): + reference = None + for scale in (1, 8.5, 29): + base = configured_predbat(scale) + reset_inverter(base) + base.minutes_now = 0 + base.forecast_minutes = 60 + base.soc_max = 10.0 + base.soc_kw = 8.0 if ceiling else 2.0 + base.reserve = 0.0 + base.best_soc_keep = 0.0 + base.best_soc_keep_weight = 1.0 + base.cost_today_sofar = 0.0 + base.debug_enable = False + base.all_active_keep = {} if ceiling else {minute: 50.0 for minute in range(60)} + base.all_active_keep_max = {minute: 50.0 for minute in range(60)} if ceiling else {} + pv = {minute: 0.0 for minute in range(0, 60, 5)} + load = dict(pv) + penalty = [] + for rate in (0.0, -5.0, 10.0): + reset_rates(base, rate * scale, rate * scale) + base.prediction_kernel_enable = False + prediction = Prediction(base, pv, pv, load, load) + actual = prediction.run_prediction([], [], [], [], PV_SCENARIO_NOMINAL, 60, cache=False)[8] + penalty.append(actual / scale) + # This exercises the same manual-SoC scenario in native C++, including the + # single/batch-shared result shaping. Requires a freshly rebuilt ABI 13 binary. + assert not dual_run("currency_manual_soc_{}_{}_{}".format("max" if ceiling else "min", scale, rate), base, pv, pv, load, load, [], [], [], [], PV_SCENARIO_NOMINAL, 60) + assert penalty[0] > 0, (scale, ceiling, penalty) + assert abs(penalty[0] - penalty[1]) < 0.00025, (scale, ceiling, penalty) + assert abs(penalty[2] - penalty[0] * 10) < 0.00025, (scale, ceiling, penalty) + if reference is None: + reference = penalty + else: + assert all(abs(a - b) < 0.00025 for a, b in zip(penalty, reference)), (scale, ceiling, penalty, reference) + + # Plain best_soc_keep remains rate-weighted; no manual/alert floor means no clamp. + if not ceiling: + base.all_active_keep = {} + base.best_soc_keep = 5.0 + reset_rates(base, 0.0, 0.0) + base.prediction_kernel_enable = False + prediction = Prediction(base, pv, pv, load, load) + assert prediction.run_prediction([], [], [], [], PV_SCENARIO_NOMINAL, 60, cache=False)[8] == 0 + + +def test_currency_v9711_kernel_rounding(): + """Non-unit scales must not round native monetary results to fixed 4dp first.""" + from prediction_kernel import PkResult, kernel_result_tuple + + for scale in (1, 8.5, 29): + pred = SimpleNamespace(currency_scale=scale, car_charging_soc_next=[], num_cars=0, iboost_enable=False, iboost_next=0) + result = PkResult() + raw_money = 0.000051 * scale + # C++ keeps its current upstream fast-4dp behaviour at 1; at non-unit + # scales ABI 13 returns both monetary fields unrounded. + result.final_metric = round(raw_money, 4) if scale == 1 else raw_money + result.metric_keep = result.final_metric + returned = kernel_result_tuple(pred, result, {}) + expected = round_currency(raw_money, scale) + assert returned[0] == expected and returned[8] == expected, (scale, returned[:9], expected) + if scale == 8.5: + # Fixed 4dp native pre-rounding would lose a calibrated low-value boundary. + double_rounded = round_currency(round(raw_money, 4), scale) + assert double_rounded != expected, (double_rounded, expected) + + +def run_currency_calibration_tests(my_predbat): + """Run calibration tests through Predbat's TEST_REGISTRY contract.""" + del my_predbat + test_currency_configuration() + test_currency_value_range_compatibility() + test_currency_prices_and_sorting() + test_currency_decision_boundaries() + test_currency_planner_equivalence() + test_currency_kernel_parity() + test_currency_export_commitment() + test_currency_merge_and_compare() + test_currency_v960_boundaries() + test_currency_saved_scenarios() + test_currency_manual_soc_target_floor() + test_currency_v9711_kernel_rounding() + return False + + +def test_currency_debug_plan(my_predbat): + """Replan a committed debug fixture with rescaled prices, comparing entire schedules.""" + from unit_test import create_predbat + + del my_predbat + reference = None + money_names = {item["name"] for item in CONFIG_ITEMS if item.get("unit") in MONETARY_UNITS} + money_names.update(("cost_today_sofar", "cost_total_car", "cost_yesterday_car", "metric_standing_charge")) + rate_tables = ("rate_import", "rate_export", "rate_gas", "rate_min_forward", "rate_export_max_forward", "rate_import_pre_saving", "rate_export_pre_saving", "io_confirmed_credit") + for scale in (1, 8.5, 29): + base = create_predbat() + reset_inverter(base) + base.read_debug_yaml("cases/predbat_debug_agile1.yaml") + base.load_user_config() + base.currency_scale = scale + base.args["currency_scale"] = scale + for name, value in list(base.__dict__.items()): + is_rate_stat = name.startswith("rate_") and any(name.endswith(suffix) for suffix in ("_min", "_max", "_average", "_base", "_threshold_charge", "_threshold_export", "_cost_threshold", "_tariff_max")) + if (name in money_names or is_rate_stat) and isinstance(value, (int, float)) and not isinstance(value, bool): + setattr(base, name, value * scale) + for name in rate_tables: + table = getattr(base, name, {}) + setattr(base, name, {minute: value * scale for minute, value in table.items()}) + for name in ("low_rates", "high_export_rates", "charge_window", "export_window", "charge_window_best", "export_window_best"): + for window in getattr(base, name): + if "average" in window: + window["average"] *= scale + for slots in base.car_charging_slots: + for slot in slots: + for field in ("average", "cost"): + if field in slot: + slot[field] *= scale + base.prediction_kernel_enable = True + base.plan_debug = base.debug_enable = False + start = time.monotonic() + with ExitStack() as stack: + launches = [stack.enter_context(patch.object(base, name, wraps=getattr(base, name))) for name in ("launch_run_prediction_single", "launch_run_prediction_charge", "launch_run_prediction_charge_min_max", "launch_run_prediction_export")] + base.calculate_plan(recompute=True, publish=False) + candidate_count = tuple(launch.call_count for launch in launches) + elapsed = time.monotonic() - start + schedule = ([(w["start"], w["end"]) for w in base.charge_window_best], base.charge_limit_best, [(w["start"], w["end"]) for w in base.export_window_best], base.export_limits_best) + metric = base.run_prediction_metric(base.charge_limit_best, base.charge_window_best, base.export_window_best, base.export_limits_best, end_record=base.end_record) + print("currency full-plan scale={} candidates={} runtime={:.3f}s metric={}".format(scale, candidate_count, elapsed, metric[0])) + if reference is None: + reference = (copy.deepcopy(schedule), candidate_count, metric) + else: + assert schedule == reference[0], (scale, schedule, reference[0]) + assert candidate_count == reference[1], (scale, candidate_count, reference[1]) + for index in range(4): + assert abs(metric[index] / scale - reference[2][index]) <= 0.00025, (scale, index, metric[index], reference[2][index]) + return False diff --git a/apps/predbat/tests/test_currency_scale.py b/apps/predbat/tests/test_currency_scale.py index 68e335496..72574172a 100644 --- a/apps/predbat/tests/test_currency_scale.py +++ b/apps/predbat/tests/test_currency_scale.py @@ -82,7 +82,9 @@ def run_currency_scale_tests(my_predbat): for bad in ["twenty", 0.5, -3, "nan", "inf"]: print(" [currency_scale] {!r} is ignored".format(bad)) _restart(my_predbat, currency_scale=bad) + # A rejected scale falls back to scale=1, including its original limits and clamp. failed |= _check("metric_min_improvement_export range with {!r}".format(bad), _range(my_predbat, "metric_min_improvement_export"), (-50, 50.0)) + failed |= _check("clamped monetary value with {!r}".format(bad), my_predbat.config_index["metric_min_improvement_export"]["value"], 50) failed |= _check("currency_scale attribute with {!r}".format(bad), getattr(my_predbat, "currency_scale", None), 1.0) finally: my_predbat.args.clear() diff --git a/apps/predbat/tests/test_integer_config.py b/apps/predbat/tests/test_integer_config.py index 48174eb5c..7399aaaa1 100644 --- a/apps/predbat/tests/test_integer_config.py +++ b/apps/predbat/tests/test_integer_config.py @@ -249,10 +249,8 @@ def test_config_item_range_clamp(my_predbat): assert my_predbat.had_errors is False, "In-range config value should not flag had_errors" print("✓ In-range value (0.3) passes through unclamped") - # Test 4: an integer-step item with a float-typed max (metric_min_improvement_plan: step=1, - # max=250.0) must still come out as an int after clamping, not the raw float max - clamping - # runs before the integer-preservation check, so it needs to re-apply after the clamp rather - # than just handing back the schema's float boundary value untouched. + # Test 4: at the default currency scale, an integer-step monetary item must still + # clamp to its declared float-typed max and preserve integer formatting. int_item = None for config_item in my_predbat.CONFIG_ITEMS: if config_item.get("name") == "metric_min_improvement_plan": @@ -271,10 +269,12 @@ def test_config_item_range_clamp(my_predbat): my_predbat.expose_config("metric_min_improvement_plan", 300, force_ha=True) my_predbat.had_errors = False my_predbat.load_user_config() + assert my_predbat.currency_scale == 1.0, "This test exercises the unscaled compatibility path" assert int_item["value"] == 250, f"Expected clamp to max 250, got {int_item['value']}" + assert int_item["max"] == 250.0, f"Default-scale range should stay unchanged, got {int_item['max']}" assert isinstance(int_item["value"], int), f"Clamped value for an integer-step item should stay an int, got {type(int_item['value'])}" assert my_predbat.had_errors is True, "Out-of-range config value should flag had_errors" - print("✓ Above-max value (300) on an integer-step item clamps to an int (250), not a float") + print("✓ Above-max monetary value (300) clamps to 250 as an int at currency_scale=1") my_predbat.expose_config("metric_min_improvement_plan", int_item.get("default"), force_ha=True) my_predbat.expose_config("expert_mode", original_expert_mode, force_ha=True) diff --git a/apps/predbat/unit_test.py b/apps/predbat/unit_test.py index cf2d04554..24d76e380 100644 --- a/apps/predbat/unit_test.py +++ b/apps/predbat/unit_test.py @@ -166,6 +166,7 @@ from tests.test_web_curves_chart import run_web_curves_chart_tests from tests.test_web_chart_currency import test_rates_chart_series_names_use_currency_symbol, test_pv_chart_forecast_history_is_uncalibrated from tests.test_currency_scale import run_currency_scale_tests +from tests.test_currency_calibration import run_currency_calibration_tests, test_currency_debug_plan from tests.test_web_debug_history_routes import test_web_debug_history_routes from tests.test_agent_tools import run_agent_tools_tests from tests.test_chat_store import run_chat_store_tests @@ -717,6 +718,8 @@ def main(): ("web_curves_chart", run_web_curves_chart_tests, "Curves chart page: curve sensor status, SoC and temperature curve charts", False), ("web_chart_currency", test_rates_chart_series_names_use_currency_symbol, "Rates chart series names follow currency_symbols tests", False), ("currency_scale", run_currency_scale_tests, "apps.yaml currency_scale widens money setting ranges tests (#6155)", False), + ("currency_calibration", run_currency_calibration_tests, "Extended planner/price calibration based on v9.7.11 currency_scale", False), + ("currency_debug_plan", test_currency_debug_plan, "Extended full-plan currency-scale replay", False), ("web_chart_pv_forecast", test_pv_chart_forecast_history_is_uncalibrated, "PV chart plots the uncalibrated forecast history tests", False), ("web_debug_history_routes", test_web_debug_history_routes, "Debug-history web routes tests (#4438 review items 4, 6, 21)", False), ("agent_tools", run_agent_tools_tests, "Shared agent tool layer and schema projection tests", False), diff --git a/apps/predbat/userinterface.py b/apps/predbat/userinterface.py index 77f9c667d..b6694bae8 100644 --- a/apps/predbat/userinterface.py +++ b/apps/predbat/userinterface.py @@ -665,7 +665,12 @@ def apply_currency_scale(self): if value == int(value): value = int(value) item[bound] = value - self.log("Scaled the range of money settings by currency_scale {}".format(scale)) + # Extend upstream PR #6156: calibrate monetary input_number increments as well. + # Preserve both built-in defaults and explicitly configured values in local minor units. + if "step" in item: + step = round(item["step"] * scale, 4) + item["step"] = int(step) if step == int(step) else step + self.log("Scaled the ranges and steps of money settings by currency_scale {}".format(scale)) def convert_currency_unit(self, unit): """ @@ -1351,7 +1356,10 @@ def load_user_config(self, quiet=True, register=False, load_config=False): ha_value = None if not enabled: - item["value"] = None + if self.currency_scale == 1.0 or item.get("unit") not in CURRENCY_UNITS: + item["value"] = None + else: + item.setdefault("value", None) # Remove the state if the entity still exists ha_value = self.get_state_wrapper(entity) @@ -1399,6 +1407,14 @@ def load_user_config(self, quiet=True, register=False, load_config=False): # of, say, 30 passes straight through unclamped). item_min = item.get("min", None) item_max = item.get("max", None) + if self.currency_scale != 1.0 and item.get("unit") in CURRENCY_UNITS: + # Preserve stored monetary values beyond calibrated limits only for scaled installs. + # Widen the advertised slider for this value, with a visible warning. + if (item_min is not None and ha_value < item_min) or (item_max is not None and ha_value > item_max): + self.record_status("Warn: Preserving monetary config {}={} outside calibrated range [{}, {}]; extending range".format(name, ha_value, item_min, item_max), had_errors=True) + item["min"] = min(item_min, ha_value) + item["max"] = max(item_max, ha_value) + item_min, item_max = item["min"], item["max"] if item_min is not None and ha_value < item_min: self.record_status("Warn: Config item {} value {} is below the minimum {} - clamping to {}".format(name, ha_value, item_min, item_min), had_errors=True) ha_value = float(item_min) diff --git a/docs/apps-yaml.md b/docs/apps-yaml.md index 45ee6c61a..a2d7ad44c 100644 --- a/docs/apps-yaml.md +++ b/docs/apps-yaml.md @@ -316,21 +316,38 @@ Sets your symbol to use for your main currency e.g. £, € or $ and for 1/100th ### currency_scale -Optional, default 1. Predbat plans in your minor currency unit (pence, cents, øre, haléř...), and the ranges of its money settings, -such as **input_number.predbat_metric_min_improvement_export**, were chosen in pence. -If your minor unit is worth much less than a penny the top of those ranges can be too small to be a useful threshold, -e.g. the 50 limit on the metric_min_improvement settings is only 0.50 CZK. +Optional, default 1. Predbat calculates costs using your currency's minor unit (such as pence or cents). +The ranges and steps of monetary settings were originally chosen for pence. If your minor unit is worth substantially +less than a penny, the usual limits may be too low to set useful thresholds. -Set `currency_scale` to roughly how many of your minor units make one penny, and the minimum and maximum of every setting measured in your minor unit -(the `p`, `p/kWh` and `p/kg` settings) is multiplied by it when Predbat starts, e.g. for Czech koruna (about 29 haléř to the penny): +Set `currency_scale` to roughly the number of your minor units equivalent in value to one penny. +For example, if one penny is approximately equivalent to 10 local minor units, use: ```yaml - currency_scale: 29 + currency_scale: 10 ``` -Only the ranges change. The defaults and the values you have already set stay as they are, so you still need to raise the thresholds yourself -to what suits you - multiplying the defaults by the same factor is a good starting point. -Values below 1 are ignored, as they would narrow the ranges and cut back settings you have already made. +Predbat uses this setting when it starts to adjust: + +- The minimum, maximum and step of monetary Home Assistant controls, such as + `input_number.predbat_metric_min_improvement_export`. +- Internal monetary preferences and thresholds used to choose charge and export windows, + value stored energy, compare prices and group similar tariff periods. +- Monetary rounding precision in planning and prediction, including the native prediction kernel. + +**This is a calibration factor, not a currency conversion.** Tariff prices, manually entered amounts, +previously saved configuration values and built-in setting defaults are not multiplied or converted. +For example, a tariff entered as 12.5 local minor units/kWh is still treated as 12.5 local minor units/kWh. +You may still need to review your saved monetary thresholds to suit your local prices. + +With `currency_scale: 1`, Predbat retains the original limits, clamping and monetary calculations. +When a larger scale is configured, an existing monetary value outside the new control range is preserved: +Predbat widens the displayed range and logs a warning instead of silently changing the amount. +Non-monetary settings keep their normal limits. Physical power, energy and battery state-of-charge +settings are not scaled, and currency symbols and provider-specific currency handling are unchanged. + +Use a finite factor of at least 1. Invalid factors and values below 1 fall back to 1. +Restart Predbat after changing `currency_scale`, and review your monetary settings afterwards. ### template