diff --git a/homeassistant/components/nederlandse_spoorwegen/sensor.py b/homeassistant/components/nederlandse_spoorwegen/sensor.py index c814e3a97a2a4..39db6ad880f0a 100644 --- a/homeassistant/components/nederlandse_spoorwegen/sensor.py +++ b/homeassistant/components/nederlandse_spoorwegen/sensor.py @@ -135,7 +135,7 @@ def native_value(self): @property def extra_state_attributes(self): """Return the state attributes.""" - if not self._trips: + if not self._trips or self._first_trip is None: return None if self._first_trip.trip_parts: @@ -212,6 +212,8 @@ def extra_state_attributes(self): attributes["next"] = self._next_trip.departure_time_planned.strftime( "%H:%M:%S" ) + else: + attributes["next"] = None return attributes @@ -263,9 +265,13 @@ def update(self) -> None: > datetime.now().replace(tzinfo=dt_util.get_default_time_zone()) ] - sorted_times = sorted(filtered_times, key=lambda x: x[1]) - self._first_trip = self._trips[sorted_times[0][0]] - self._state = sorted_times[0][1].strftime("%H:%M:%S") + if len(filtered_times) > 0: + sorted_times = sorted(filtered_times, key=lambda x: x[1]) + self._first_trip = self._trips[sorted_times[0][0]] + self._state = sorted_times[0][1].strftime("%H:%M:%S") + else: + self._first_trip = None + self._state = None # Filter again to remove trains that leave at the exact same time. filtered_times = [ @@ -273,8 +279,12 @@ def update(self) -> None: for i, time in enumerate(all_times) if time > sorted_times[0][1] ] - sorted_times = sorted(filtered_times, key=lambda x: x[1]) - self._next_trip = self._trips[sorted_times[0][0]] + + if len(filtered_times) > 0: + sorted_times = sorted(filtered_times, key=lambda x: x[1]) + self._next_trip = self._trips[sorted_times[0][0]] + else: + self._next_trip = None except ( requests.exceptions.ConnectionError,