JaccoR / hass-entso-e

Integration for Home Assistant to fetch day ahead energy prices from European countries via ENTSO-e Transparency Platform
Apache License 2.0
173 stars 28 forks source link

Error handling when the ENTSO-E platform is down #86

Closed MrOzzOnline closed 1 year ago

MrOzzOnline commented 1 year ago

When the ENTSO-E transparancy platform is down for some reason all data becomes unavailable even if there is still old data available. The following error is triggered:

2022-12-07 14:54:29.903 ERROR (MainThread) [custom_components.entsoe.coordinator] Unexpected error fetching ENTSO-e coordinator data: 'NoneType' object has no attribute 'items'Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 205, in _async_refresh self.data = await self._async_update_data() File "/config/custom_components/entsoe/coordinator.py", line 101, in _async_update_data parsed_data = self.parse_hourprices(data) File "/config/custom_components/entsoe/coordinator.py", line 85, in parse_hourprices for hour, price in hourprices.items(): AttributeError: 'NoneType' object has no attribute 'items'

Suggestion:

  1. catch the error
  2. use old data as long as possible
  3. add a sensor for the ENTSO-E availability (optional, but would be very nice to handle follow up errors)
michaelpiron commented 1 year ago

I support this suggestion, would be very value-adding

lindsand commented 1 year ago

And root cause is here:

Entsoe_down_20221207

MrOzzOnline commented 1 year ago

And root cause is here:

  • server down for maintenance
  • I guess this happens now and then

Entsoe_down_20221207

Yes, but the integration doesn't handle the downtime properly. From the moment the server becomes unavailable, all entities become unavailable while the data has been downloaded earlier.

JaccoR commented 1 year ago

I'm not sure what the home assistant way of handling a no data api request. The errors are catched with:

        except NoMatchingDataError as exc:
            raise UpdateFailed("ENTSO-e prices are unavailable at the moment.") from exc
        except (HTTPError) as exc:
            if exc.response.status_code == 401:
                raise UpdateFailed("Unauthorized: Please check your API-key.") from exc
        except Exception as exc:
            raise UpdateFailed(f"Unexcpected error when fetching ENTSO-e prices: {exc}") from exc

Home assistant sees there is an error and shuts down all sensors. Maybe, before requesting the prices from the entsoe api there should be a check if it is available, if not, do not update the prices.

MrOzzOnline commented 1 year ago

Home assistant sees there is an error and shuts down all sensors. Maybe, before requesting the prices from the entsoe api there should be a check if it is available, if not, do not update the prices.

That would be great. In theory you only need to successfully pull the data once every 24h.

Adding a binary with the ENTSO-e connection state would be a great bonus.

lindsand commented 1 year ago

What about comparing with the logic in the Nordpool interface?

https://github.com/custom-components/nordpool

And nordpool is using a status flag (I think it's set after midnight if new data is not available, didn't check that but could make sense)

Tomorrow valid true / false

sfstar commented 1 year ago

I'm not sure what the home assistant way of handling a no data api request. The errors are catched with:

        except NoMatchingDataError as exc:
            raise UpdateFailed("ENTSO-e prices are unavailable at the moment.") from exc
        except (HTTPError) as exc:
            if exc.response.status_code == 401:
                raise UpdateFailed("Unauthorized: Please check your API-key.") from exc
        except Exception as exc:
            raise UpdateFailed(f"Unexcpected error when fetching ENTSO-e prices: {exc}") from exc

Home assistant sees there is an error and shuts down all sensors. Maybe, before requesting the prices from the entsoe api there should be a check if it is available, if not, do not update the prices.

Perhaps we should check the datas presence and age before raising the updatefailed. In case the data is still valid/usefull log a warning that the update failed. Else raise the updatefailed.

This combined with https://developers.home-assistant.io/docs/core/entity/sensor?_highlight=restore#restoring-sensor-states would make the integration more resilient (even if you restart during the api outage)

MrOzzOnline commented 1 year ago

I'm not sure what the home assistant way of handling a no data api request. The errors are catched with:

        except NoMatchingDataError as exc:
            raise UpdateFailed("ENTSO-e prices are unavailable at the moment.") from exc
        except (HTTPError) as exc:
            if exc.response.status_code == 401:
                raise UpdateFailed("Unauthorized: Please check your API-key.") from exc
        except Exception as exc:
            raise UpdateFailed(f"Unexcpected error when fetching ENTSO-e prices: {exc}") from exc

Home assistant sees there is an error and shuts down all sensors. Maybe, before requesting the prices from the entsoe api there should be a check if it is available, if not, do not update the prices.

Perhaps we should check the datas presence and age before raising the updatefailed. In case the data is still valid/usefull log a warning that the update failed. Else raise the updatefailed.

This combined with https://developers.home-assistant.io/docs/core/entity/sensor?_highlight=restore#restoring-sensor-states would make the integration more resilient (even if you restart during the api outage)

Since this integration is often the core of many automations it's important it is robust.

sfstar commented 1 year ago

Not to raise any hopes but i'm currently testing the restore change I suggested. Getting real tired of my automations not working πŸ˜‰πŸ˜‚