From ad2d150e3513edab42b30c85125be459d6ef955f Mon Sep 17 00:00:00 2001 From: Trefor Southwell <48591903+springfall2008@users.noreply.github.com> Date: Thu, 1 Feb 2024 19:33:01 +0000 Subject: [PATCH 1/3] Fix to repeat of incremental rate adjustments --- apps/predbat/predbat.py | 64 ++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 40 deletions(-) diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index 1f7096c38..cac29cec4 100644 --- a/apps/predbat/predbat.py +++ b/apps/predbat/predbat.py @@ -1129,7 +1129,7 @@ def find_charge_curve(self): if rate_scaling > 0: for index in final_curve: final_curve[index] = round(final_curve[index] / rate_scaling, 2) - + # If we have the correct data then output it if rate_scaling > 0: text = " battery_charge_power_curve:\n" @@ -1144,9 +1144,7 @@ def find_charge_curve(self): self.log("Charge curve can be entered into apps.yaml or set to auto:\n" + text) rate_scaling = round(rate_scaling, 2) if rate_scaling != self.base.battery_rate_max_scaling: - self.log( - "Consider setting in HA: input_number.battery_max_rate_scaling: {} - currently {}".format(rate_scaling, self.base.battery_rate_max_scaling) - ) + self.log("Consider setting in HA: input_number.battery_max_rate_scaling: {} - currently {}".format(rate_scaling, self.base.battery_rate_max_scaling)) return final_curve else: self.log("Note: Found incorrect battery charging curve, maybe try again when you have more data.") @@ -3144,17 +3142,7 @@ def minute_data_import_export(self, now_utc, key, scale=1.0, required_unit=None) if history: import_today = self.minute_data( - history[0], - self.max_days_previous, - now_utc, - "state", - "last_updated", - backwards=True, - smoothing=True, - scale=scale, - clean_increment=True, - accumulate=import_today, - required_unit=required_unit, + history[0], self.max_days_previous, now_utc, "state", "last_updated", backwards=True, smoothing=True, scale=scale, clean_increment=True, accumulate=import_today, required_unit=required_unit ) else: self.log("Error: Unable to fetch history for {}".format(entity_id)) @@ -3329,18 +3317,18 @@ def minute_data( continue # Find and converter units - if required_unit and ("attributes" in item): - if "unit_of_measurement" in item["attributes"]: - unit = item["attributes"]["unit_of_measurement"] + if required_unit and ('attributes' in item): + if 'unit_of_measurement' in item['attributes']: + unit = item['attributes']['unit_of_measurement'] if unit != required_unit: - if required_unit in ["kW", "kWh"] and unit in ["W", "Wh"]: + if required_unit in ['kW', 'kWh'] and unit in ['W', 'Wh']: state = state / 1000.0 - elif required_unit in ["W", "Wh"] and unit in ["kW", "kWh"]: + elif required_unit in ['W', 'Wh'] and unit in ['kW', 'kWh']: state = state * 1000.0 else: # Ignore data in wrong units if we can't converter - continue - + continue + # Divide down the state if required if divide_by: state /= divide_by @@ -5338,12 +5326,18 @@ def basic_rates(self, info, rtype, prev=None): for minute in range(start_minutes, end_minutes): minute_mod = minute % max_minute if (not date) or (minute >= 0 and minute < max_minute): - if rate_increment: - rates[minute_mod] = rates.get(minute % max_minute, 0.0) + rate - else: - rates[minute_mod] = rate - if load_scaling is not None: - self.load_scaling_dynamic[minute_mod] = load_scaling + minute_index = minute_mod + # For incremental adjustements we have to loop over 24-hour periods + while minute_index < max_minute: + if rate_increment: + rates[minute_index] = rates.get(minute_index, 0.0) + rate + else: + rates[minute_index] = rate + if load_scaling is not None: + self.load_scaling_dynamic[minute_index] = load_scaling + if date or not prev: + break + minute_index += 24*60 if not date and not prev: rates[minute_mod + max_minute] = rate if load_scaling is not None: @@ -8689,17 +8683,7 @@ def fetch_extra_load_forecast(self, now_utc): data = None load_forecast = self.minute_data( - data, - self.forecast_days, - self.midnight_utc, - "energy", - "last_updated", - backwards=False, - clean_increment=False, - smoothing=True, - divide_by=1.0, - scale=1.0, - required_unit="kWh", + data, self.forecast_days, self.midnight_utc, "energy", "last_updated", backwards=False, clean_increment=False, smoothing=True, divide_by=1.0, scale=1.0, required_unit="kWh" ) return load_forecast @@ -10390,7 +10374,7 @@ def fetch_config_options(self): self.battery_scaling = self.get_arg("battery_scaling", 1.0) # Charge curve - if self.args.get("battery_charge_power_curve", "") == "auto": + if self.args.get("battery_charge_power_curve", '') == 'auto': self.battery_charge_power_curve_auto = True else: self.battery_charge_power_curve_auto = False From a7efafaba63d022bbb473555134525b83fad76b8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Thu, 1 Feb 2024 19:34:27 +0000 Subject: [PATCH 2/3] [pre-commit.ci lite] apply automatic fixes --- apps/predbat/predbat.py | 48 ++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index cac29cec4..c1918a8e3 100644 --- a/apps/predbat/predbat.py +++ b/apps/predbat/predbat.py @@ -1129,7 +1129,7 @@ def find_charge_curve(self): if rate_scaling > 0: for index in final_curve: final_curve[index] = round(final_curve[index] / rate_scaling, 2) - + # If we have the correct data then output it if rate_scaling > 0: text = " battery_charge_power_curve:\n" @@ -1144,7 +1144,9 @@ def find_charge_curve(self): self.log("Charge curve can be entered into apps.yaml or set to auto:\n" + text) rate_scaling = round(rate_scaling, 2) if rate_scaling != self.base.battery_rate_max_scaling: - self.log("Consider setting in HA: input_number.battery_max_rate_scaling: {} - currently {}".format(rate_scaling, self.base.battery_rate_max_scaling)) + self.log( + "Consider setting in HA: input_number.battery_max_rate_scaling: {} - currently {}".format(rate_scaling, self.base.battery_rate_max_scaling) + ) return final_curve else: self.log("Note: Found incorrect battery charging curve, maybe try again when you have more data.") @@ -3142,7 +3144,17 @@ def minute_data_import_export(self, now_utc, key, scale=1.0, required_unit=None) if history: import_today = self.minute_data( - history[0], self.max_days_previous, now_utc, "state", "last_updated", backwards=True, smoothing=True, scale=scale, clean_increment=True, accumulate=import_today, required_unit=required_unit + history[0], + self.max_days_previous, + now_utc, + "state", + "last_updated", + backwards=True, + smoothing=True, + scale=scale, + clean_increment=True, + accumulate=import_today, + required_unit=required_unit, ) else: self.log("Error: Unable to fetch history for {}".format(entity_id)) @@ -3317,18 +3329,18 @@ def minute_data( continue # Find and converter units - if required_unit and ('attributes' in item): - if 'unit_of_measurement' in item['attributes']: - unit = item['attributes']['unit_of_measurement'] + if required_unit and ("attributes" in item): + if "unit_of_measurement" in item["attributes"]: + unit = item["attributes"]["unit_of_measurement"] if unit != required_unit: - if required_unit in ['kW', 'kWh'] and unit in ['W', 'Wh']: + if required_unit in ["kW", "kWh"] and unit in ["W", "Wh"]: state = state / 1000.0 - elif required_unit in ['W', 'Wh'] and unit in ['kW', 'kWh']: + elif required_unit in ["W", "Wh"] and unit in ["kW", "kWh"]: state = state * 1000.0 else: # Ignore data in wrong units if we can't converter - continue - + continue + # Divide down the state if required if divide_by: state /= divide_by @@ -5337,7 +5349,7 @@ def basic_rates(self, info, rtype, prev=None): self.load_scaling_dynamic[minute_index] = load_scaling if date or not prev: break - minute_index += 24*60 + minute_index += 24 * 60 if not date and not prev: rates[minute_mod + max_minute] = rate if load_scaling is not None: @@ -8683,7 +8695,17 @@ def fetch_extra_load_forecast(self, now_utc): data = None load_forecast = self.minute_data( - data, self.forecast_days, self.midnight_utc, "energy", "last_updated", backwards=False, clean_increment=False, smoothing=True, divide_by=1.0, scale=1.0, required_unit="kWh" + data, + self.forecast_days, + self.midnight_utc, + "energy", + "last_updated", + backwards=False, + clean_increment=False, + smoothing=True, + divide_by=1.0, + scale=1.0, + required_unit="kWh", ) return load_forecast @@ -10374,7 +10396,7 @@ def fetch_config_options(self): self.battery_scaling = self.get_arg("battery_scaling", 1.0) # Charge curve - if self.args.get("battery_charge_power_curve", '') == 'auto': + if self.args.get("battery_charge_power_curve", "") == "auto": self.battery_charge_power_curve_auto = True else: self.battery_charge_power_curve_auto = False From a9b308520f05fa276d536e364d61dd9938f65ac9 Mon Sep 17 00:00:00 2001 From: Trefor Southwell <48591903+springfall2008@users.noreply.github.com> Date: Thu, 1 Feb 2024 19:37:58 +0000 Subject: [PATCH 3/3] Update predbat.py - typo --- apps/predbat/predbat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index c1918a8e3..ed4ece46e 100644 --- a/apps/predbat/predbat.py +++ b/apps/predbat/predbat.py @@ -5339,7 +5339,7 @@ def basic_rates(self, info, rtype, prev=None): minute_mod = minute % max_minute if (not date) or (minute >= 0 and minute < max_minute): minute_index = minute_mod - # For incremental adjustements we have to loop over 24-hour periods + # For incremental adjustments we have to loop over 24-hour periods while minute_index < max_minute: if rate_increment: rates[minute_index] = rates.get(minute_index, 0.0) + rate