Skip to content

Commit

Permalink
Change method of metric keep
Browse files Browse the repository at this point in the history
  • Loading branch information
springfall2008 authored Dec 24, 2024
1 parent d9ed507 commit d8cf5f1
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 164 deletions.
12 changes: 12 additions & 0 deletions apps/predbat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@
"icon": "mdi:battery-50",
"default": 0.5,
},
{
"name": "best_soc_keep_weight",
"friendly_name": "Best SOC Keep Weighting",
"type": "input_number",
"min": 0.1,
"max": 5,
"step": 0.10,
"unit": "*",
"icon": "mdi:multiplication",
"default": 0.5,
"enable": "expert_mode",
},
{
"name": "metric_min_improvement",
"friendly_name": "Metric Min Improvement",
Expand Down
2 changes: 2 additions & 0 deletions apps/predbat/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,7 @@ def fetch_config_options(self):
self.best_soc_min = self.get_arg("best_soc_min")
self.best_soc_max = self.get_arg("best_soc_max")
self.best_soc_keep = self.get_arg("best_soc_keep")
self.best_soc_keep_weight = self.get_arg("best_soc_keep_weight")
self.set_soc_minutes = 30
self.set_window_minutes = 30
self.inverter_set_charge_before = self.get_arg("inverter_set_charge_before")
Expand Down Expand Up @@ -1791,6 +1792,7 @@ def fetch_config_options(self):
self.set_status_notify = self.get_arg("set_status_notify")
self.set_inverter_notify = self.get_arg("set_inverter_notify")
self.set_export_freeze_only = self.get_arg("set_export_freeze_only")
print("Getting set_export_freeze_only", self.set_export_freeze_only)
self.set_discharge_during_charge = self.get_arg("set_discharge_during_charge")

# Mode
Expand Down
1 change: 1 addition & 0 deletions apps/predbat/predbat.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ def reset(self):
self.best_soc_max = 0
self.best_soc_margin = 0
self.best_soc_keep = 0
self.best_soc_keep_weight = 0.5
self.rate_min = 0
self.rate_min_minute = 0
self.rate_min_forward = {}
Expand Down
13 changes: 5 additions & 8 deletions apps/predbat/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def __init__(self, base=None, pv_forecast_minute_step=None, pv_forecast_minute10
self.battery_loss = base.battery_loss
self.battery_loss_discharge = base.battery_loss_discharge
self.best_soc_keep = base.best_soc_keep
self.best_soc_keep_weight = base.best_soc_keep_weight
self.best_soc_min = base.best_soc_min
self.car_charging_battery_size = base.car_charging_battery_size
self.rate_import = base.rate_import
Expand Down Expand Up @@ -411,9 +412,9 @@ def run_prediction(self, charge_limit, charge_window, export_window, export_limi
if minute < 4 * 60:
keep_minute_scaling = 0
else:
keep_minute_scaling = min(((minute - 4 * 60) / (2 * 60)), 1.0) * 0.5
keep_minute_scaling = min(((minute - 4 * 60) / (2 * 60)), 1.0) * self.best_soc_keep_weight
else:
keep_minute_scaling = 0.5
keep_minute_scaling = self.best_soc_keep_weight

# Find charge & discharge windows
charge_window_n = charge_window_optimised.get(minute_absolute, -1)
Expand Down Expand Up @@ -808,12 +809,8 @@ def run_prediction(self, charge_limit, charge_window, export_window, export_limi
diff = get_diff(battery_draw, pv_dc, pv_ac, load_yesterday, inverter_loss)

# Metric keep - pretend the battery is empty and you have to import instead of using the battery
if soc < self.best_soc_keep:
# Apply keep as a percentage of the time in the future so it gets stronger over an 4 hour period
# Weight to 50% chance of the scenario
keep_diff = max(get_diff(0, 0, pv_now, load_yesterday, inverter_loss), battery_draw)
if keep_diff > 0:
metric_keep += rate_import[minute_absolute] * keep_diff * keep_minute_scaling
if self.best_soc_keep > 0 and soc <= self.best_soc_keep:
metric_keep += (self.best_soc_keep - soc) * rate_import[minute_absolute] * keep_minute_scaling * step / 60.0
if diff > 0:
# Import
# All imports must go to home (no inverter loss) or to the battery (inverter loss accounted before above)
Expand Down
Loading

0 comments on commit d8cf5f1

Please sign in to comment.