Skip to content

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

@bengtj

Description

@bengtj

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions