Skip to content

Commit

Permalink
Divide by zero fix (#1581)
Browse files Browse the repository at this point in the history
* Divide by zero fix

* Fix yesterday alignment

* [pre-commit.ci lite] apply automatic fixes

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
springfall2008 and pre-commit-ci-lite[bot] authored Nov 2, 2024
1 parent 6033b08 commit 6ea68c0
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions apps/predbat/predbat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4577,6 +4577,19 @@ def today_cost(self, import_today, export_today, car_today):
day_cost_time_export[stamp] = self.dp2(day_cost_export)
day_carbon_time[stamp] = self.dp2(carbon_g)

day_pkwh = 0
day_car_pkwh = 0
day_import_pkwh = 0
day_export_pkwh = 0
if day_energy_total > 0:
day_pkwh = day_cost / day_energy_total
if day_car > 0:
day_car_pkwh = day_cost_car / day_car
if day_import > 0:
day_import_pkwh = day_cost_import / day_import
if day_export > 0:
day_export_pkwh = day_cost_export / day_export

self.dashboard_item(
self.prefix + ".cost_today",
state=self.dp2(day_cost),
Expand All @@ -4587,7 +4600,7 @@ def today_cost(self, import_today, export_today, car_today):
"unit_of_measurement": self.currency_symbols[1],
"icon": "mdi:currency-usd",
"energy": self.dp2(day_energy_total),
"p/kWh": self.dp2(day_cost / day_energy_total),
"p/kWh": self.dp2(day_pkwh),
},
)
if self.num_cars > 0:
Expand All @@ -4601,7 +4614,7 @@ def today_cost(self, import_today, export_today, car_today):
"unit_of_measurement": self.currency_symbols[1],
"icon": "mdi:currency-usd",
"energy": self.dp2(day_car),
"p/kWh": self.dp2(day_cost_car / day_car),
"p/kWh": self.dp2(day_car_pkwh),
},
)
if self.carbon_enable:
Expand All @@ -4626,7 +4639,7 @@ def today_cost(self, import_today, export_today, car_today):
"unit_of_measurement": self.currency_symbols[1],
"icon": "mdi:currency-usd",
"energy": self.dp2(day_import),
"p/kWh": self.dp2(day_cost_import / day_import),
"p/kWh": self.dp2(day_import_pkwh),
},
)
self.dashboard_item(
Expand All @@ -4639,7 +4652,7 @@ def today_cost(self, import_today, export_today, car_today):
"unit_of_measurement": self.currency_symbols[1],
"icon": "mdi:currency-usd",
"energy": self.dp2(day_export),
"p/kWh": self.dp2(day_cost_export / day_export),
"p/kWh": self.dp2(day_export_pkwh),
},
)
self.log(
Expand Down Expand Up @@ -7561,7 +7574,7 @@ def calculate_yesterday(self):
yesterday_load_step = self.step_data_history(self.load_minutes, 0, forward=False, scale_today=1.0, scale_fixed=1.0, base_offset=24 * 60 + self.minutes_now)
yesterday_pv_step = self.step_data_history(self.pv_today, 0, forward=False, scale_today=1.0, scale_fixed=1.0, base_offset=24 * 60 + self.minutes_now)
yesterday_pv_step_zero = self.step_data_history(None, 0, forward=False, scale_today=1.0, scale_fixed=1.0, base_offset=24 * 60 + self.minutes_now)
minutes_back = (self.now_utc - self.midnight_utc).total_seconds() / 60
minutes_back = (self.now_utc_real - self.midnight_utc).total_seconds() / 60

# Get SoC history to find yesterday SoC
soc_kwh_data = self.get_history_wrapper(entity_id=self.prefix + ".soc_kw_h0", days=2)
Expand Down

0 comments on commit 6ea68c0

Please sign in to comment.