dan-r / HomeAssistant-NissanConnect

Unofficial Home Assistant integration for interacting with Nissan Connect vehicles 🚗
Apache License 2.0
32 stars 7 forks source link

Divide by zero impacting Monthly Efficiency and Daily Efficiency #12

Closed wralb closed 7 months ago

wralb commented 7 months ago

What happened? Monthly Efficiency and Daily Efficiency may be marked 'Unavailable' and a division by zero error logged when the car has not been driven during the day. This is generally notice when restarting Home Assistant before the vehicle has been used on the day.

To Reproduce

Expected behavior

Log Output

Mar 02 16:49:08 hass hass[57717]:     self.async_update_listeners()
Mar 02 16:49:08 hass hass[57717]:   File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/update_coordinator.py", line 183, in async_update_listeners
Mar 02 16:49:08 hass hass[57717]:     update_callback()
Mar 02 16:49:08 hass hass[57717]:   File "/home/homeassistant/.homeassistant/custom_components/nissan_connect/sensor.py", line 235, in _handle_coordinator_update
Mar 02 16:49:08 hass hass[57717]:     self._state = self._lambda(summary[0])
Mar 02 16:49:08 hass hass[57717]:                   ^^^^^^^^^^^^^^^^^^^^^^^^
Mar 02 16:49:08 hass hass[57717]:   File "/home/homeassistant/.homeassistant/custom_components/nissan_connect/sensor.py", line 48, in <lambda>
Mar 02 16:49:08 hass hass[57717]:     StatisticSensor(coordinator_stats, data[vehicle], 'daily', lambda x: x.total_distance / x.consumed_electricity, 'daily_efficiency', 'mdi:ev-station', SensorDeviceClass.DISTANCE, UnitOfLength.KILOMETERS, 2, imperial_distance),
Mar 02 16:49:08 hass hass[57717]:                                                                          ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Mar 02 16:49:08 hass hass[57717]: ZeroDivisionError: float division by zero

Home Assistant Version 2024.2.5 in python 3.12 venv on Ubuntu VM on Xen Orchestra

Integration Version 0.4.2

Additional context Vehicle is Nissan Ariya

dan-r commented 7 months ago

Thanks for reporting this, I've changed that method to catch errors in the calculation and default to returning 0 in v0.4.3. Please let me know if you see this sort of thing again!

wralb commented 7 months ago

Thanks! Tested: issue is gone after installing v0.4.3.