meichthys / uptime_kuma

Uptime Kuma HACS integration
110 stars 23 forks source link

Key error #57

Closed ceskyDJ closed 1 year ago

ceskyDJ commented 1 year ago

This error originated from a custom integration.

Logger: homeassistant Source: custom_components/uptime_kuma/sensor.py:73 Integration: Uptime Kuma (documentation, issues) First occurred: June 30, 2023 at 13:47:18 (182 occurrences) Last logged: June 30, 2023 at 14:18:17

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 229, in _handle_refresh_interval
    await self._async_refresh(log_failures=True, scheduled=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 374, in _async_refresh
    self.async_update_listeners()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 164, in async_update_listeners
    update_callback()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 431, in _handle_coordinator_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 590, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 654, in _async_write_ha_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 596, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 469, in state
    value = self.native_value
            ^^^^^^^^^^^^^^^^^
  File "/config/custom_components/uptime_kuma/sensor.py", line 73, in native_value
    return SENSORS_INFO[self.monitor.monitor_status]["value"]
           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 3.0
ceskyDJ commented 1 year ago

Hi, I found an error in my logs. It looks like everything is working but errors appear in logs. Could you look at it, please?

meichthys commented 1 year ago

Probably fine. It might have something to do with a sensor that was setup in homeassistant but later removed from uptimekuma or vice versa.

ceskyDJ commented 1 year ago

Yes, it sounds like a possible scenario. Thanks for the fix. I'm sorry, I only looked at my phone. So, could you fix that for cleaner logs, please?

meichthys commented 1 year ago

If the case is what I indicated, then you would either need to remove the extra entity from homeassistant or UptimeKuma

ceskyDJ commented 1 year ago

How can I find the entity causing those troubles? I don't know, what change could do that. I modify only Uptime Kuma entities. In HA I just moved some sensors to rooms.

emelarnz commented 10 months ago

@ceskyDJ, @meichthys - noting that the KeyError is triggered by a key value of 3.0, this is likely related to monitors being in maintenance mode - ref. uptime_kuma_api.MonitorStatus. I had the same error occur during my overnight maintenance window, and was able to fix it by adding another entry to SENSORS_INFO in custom_components/uptime_kuma/sensor.py i.e.:

SENSORS_INFO = {
    0.0: StatusValue(value="down", icon="mdi:television-off"),
    1.0: StatusValue(value="up", icon="mdi:television-shimmer"),
    2.0: StatusValue(value="pending", icon="mdi:sync"),
    3.0: StatusValue(value="maintenance", icon="mdi:wrench"),
}

Apologies I don't have time to create a PR today but will endeavour to come back to this when I get a chance.

meichthys commented 10 months ago

Thanks for this info. This sounds promising since I've never seen this issue but also never used maintenance mode. I'll take a look at it when I get some timešŸ‘

ceskyDJ commented 10 months ago

@ceskyDJ, @meichthys - noting that the KeyError is triggered by a key value of 3.0, this is likely related to monitors being in maintenance mode - ref. uptime_kuma_api.MonitorStatus. I had the same error occur during my overnight maintenance window, and was able to fix it by adding another entry to SENSORS_INFO in custom_components/uptime_kuma/sensor.py i.e.:

SENSORS_INFO = {
    0.0: StatusValue(value="down", icon="mdi:television-off"),
    1.0: StatusValue(value="up", icon="mdi:television-shimmer"),
    2.0: StatusValue(value="pending", icon="mdi:sync"),
    3.0: StatusValue(value="maintenance", icon="mdi:wrench"),
}

Apologies I don't have time to create a PR today but will endeavour to come back to this when I get a chance.

Nice work! Thanks a lot for finding the cause of my error.