travisghansen / hass-pfsense

pfSense integration with Home Assistant
193 stars 27 forks source link

ZeroDivisionError: division by zero #77

Closed boulwarek closed 2 years ago

boulwarek commented 2 years ago

PfSense 21.05 running on NetGate SG-3100 Was working fine for a few days and then started throwing this error message. Once this happens all entities become unavailable.

Unexpected error fetching rt01.home pfSense state data: division by zero
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 191, in _async_refresh
    self.data = await self._async_update_data()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 150, in _async_update_data
    return await self.update_method()
  File "/config/custom_components/pfsense/__init__.py", line 95, in async_update_data
    await hass.async_add_executor_job(lambda: data.update())
  File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/pfsense/__init__.py", line 95, in <lambda>
    await hass.async_add_executor_job(lambda: data.update())
  File "/config/custom_components/pfsense/__init__.py", line 367, in update
    ((total_change - idle_change) / total_change) * 100
ZeroDivisionError: division by zero

I temporarily modified line 367 to get the actual values of total_change and idle_change individually within the CPU Usage entity value total_change = 0 idle_change = 7451

The total_change value of 0 explains the error, but I am not sure if this is expected. Any tips on further troubleshooting?

boulwarek commented 2 years ago

Played around some more and set the cpu_usage to the following values to retrieve them as well: dict_get(previous_cpu, "ticks.total") = 2,147,483,647 dict_get(current_cpu, "ticks.total") = 2,147,483,647

These readings were taken a couple of minutes apart and they were both the same. Possibly these values are not updating correctly.

I then modified the following: if previous_cpu is not None: current_cpu = dict_get(self._state, "telemetry.cpu") if ( dict_get(previous_cpu, "ticks.total") < dict_get(current_cpu, "ticks.total") ) and ( dict_get(previous_cpu, "ticks.idle") < dict_get(current_cpu, "ticks.idle") Note that I changed the "<=" to "<" as this will prevent a zero value from being used. Now the CPU Usage entity is unavailable and not updating. Probably because these numbers are stagnant.

I rebooted my router at this point and the CPU Usage statistic re-populated correctly. It might be worth putting some logic in there to prevent division by zero even if these values are hung and not updating. At least the remainder of the entities will continue to work.

travisghansen commented 2 years ago

Thanks for bringing this up. I've got a fix queued up.

travisghansen commented 2 years ago

Fixed here: https://github.com/travisghansen/hass-pfsense/commit/c557539b2cbce63d91df805bd7e12cdbc47afa23 released in v0.5.0.