Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the price is 0 at boot nordpool seems to get disabled due to division by zero #428

Open
bengtj opened this issue Oct 27, 2024 · 2 comments

Comments

@bengtj
Copy link

bengtj commented Oct 27, 2024

Version of the custom_component

0.0.15

Homeassistant version

2024.10.4

Configuration

- platform: nordpool
  VAT: True
  currency: "SEK"
  price_in_cents: false
  low_price_cutoff: 0.95
  region: "SE3"
  precision: 1
  price_type: kWh
  additional_costs: "{{0.0|float}}"

Describe the bug

As can be seen in the the attached home assistant log early after boot there is a division by zero if the self.average is 0. After that the sensor says "Unavailable".

I just tried adding the bold text below to sensor.py and rebooted and it seems to have work to keep the sensor alive during reboot:
self.current_price / self._average
if isinstance(self.current_price, (int, float))
and isinstance(self._average, (float, int))
and self._average>0
else None

Debug log


2024-10-27 11:36:43.222 ERROR (MainThread) [homeassistant.util.logging] Exception in handle_new_hr when dispatching 'nordpool_update_hour': ()
Traceback (most recent call last):
  File "/config/custom_components/nordpool/sensor.py", line 493, in handle_new_hr
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1005, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1130, in _async_write_ha_state
    self.__async_calculate_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1071, in __async_calculate_state
    if extra_state_attributes := self.extra_state_attributes:
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/nordpool/sensor.py", line 412, in extra_state_attributes
    "price_percent_to_average": self.price_percent_to_average,
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/nordpool/sensor.py", line 269, in price_percent_to_average
    self.current_price / self._average
    ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
ZeroDivisionError: float division by zero

@Hellowlol
Copy link
Collaborator

Having average costs of 0 seems unlikely. However it should not stop the integration from booting. Thanks for the config examples this will let me recreate it. The easiest solution is just tp catch the exception and return None

@bengtj
Copy link
Author

bengtj commented Oct 27, 2024

The price is currently "-0.0 SEK/kWh" so if the average cost is calculated from boot I guess the average will be zero until that changes.

So far adding "and self._average>0" seems to be working for me. Catching the exception should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants