Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions apps/predbat/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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),
Expand All @@ -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")
Expand Down Expand Up @@ -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", "")
Expand Down
16 changes: 16 additions & 0 deletions apps/predbat/currency.py
Original file line number Diff line number Diff line change
@@ -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
44 changes: 23 additions & 21 deletions apps/predbat/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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

Expand All @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion apps/predbat/octopus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading
Loading