From 57fbb1c3d95a74bdef12ccb4465834a18522b3a2 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 1 Jul 2021 14:53:03 +0200 Subject: [PATCH] Fix sensor statistics collection with empty states (#52393) --- homeassistant/components/sensor/recorder.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index ac26e06e07d..c7f3e6e3ecd 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -153,13 +153,15 @@ def _normalize_states( entity_history: list[State], device_class: str, entity_id: str ) -> tuple[str | None, list[tuple[float, State]]]: """Normalize units.""" + unit = None if device_class not in UNIT_CONVERSIONS: # We're not normalizing this device class, return the state as they are fstates = [ (float(el.state), el) for el in entity_history if _is_number(el.state) ] - unit = fstates[0][1].attributes.get(ATTR_UNIT_OF_MEASUREMENT) + if fstates: + unit = fstates[0][1].attributes.get(ATTR_UNIT_OF_MEASUREMENT) return unit, fstates fstates = []