How to get notified when an integration fails to start or trigger a "reload" automatically

I have an HACS integration that sometimes fails to run on start and has no retry

When I click “Reload Integration” it works as expected. Is there a way to get notified when the integration fails or to retry the load after some seconds?

I was checking in the developer menu but I can not find anything about it.

Thanks!

If it occasionally fails to load on startup, you can create an automation that is triggered on startup, waits 1 minute, then checks if one of the integration’s entities has a valid value (is not unavailable).

If it is unavailable, the automation reloads the integration.

Hi, how do I find out the state of an integration? I just see core integrations on the automation screen.

I didn’t suggest finding an integration’s state. I suggested you check the state of one of the entities that are produced by the integration.

Go to Settings → Devices & Services → Integrations.

Click on the Xplora Watch integration and it will list the number of devices and entities it produces. You want to use one of those entities, specifically an entity whose state is unavailable, or doesn’t exist, when the Xplora Watch integration fails to load.

Ah I understand, sorry.

Reload an automation can also be done automatically?

You can check the state of a config entry (i.e. an integration) with the config_entry_attr(config_entry_id, 'state') function. I can’t find a list of expected return values, but when your integration fails like this it will probably return something like ConfigEntryState.SETUP_ERROR. Should be easily verifiable in Developer Tools > Template.

I would try making an automation with a template sensor that detects when this happens, then use the homeassistant.config_entry_reload action.

Yes, using an automation to detect when it’s necessary to reload it (using either of the techniques suggested by me and Mayhem_SWE) and then proceed to reload the integration (using the action described in Mayhem_SWE’s post).

When I check the state I get this

I want to check that the value is different but I do not find how to do it, comparing it to loaded or 'ConfigEntryState.Loaded always return false there.

Here, try this. I use it to reload the bring integration whenever it has been unavailable for 15 mins.

You will obviously need to change the entity Id you’re tracking in the trigger, as well as the entry ID in the action:

alias: Bring Integration Watchdog
description: Reloads the Bring integration when it goes to Unavailable
mode: single
triggers:
  - entity_id:
      - todo.generic_shopping_list
    to: unavailable
    for:
      hours: 0
      minutes: 15
      seconds: 0
    trigger: state
conditions: []
actions:
  - data:
      entry_id: 3080e6df9e7ec8a9bdc9dfc966158e03
    action: homeassistant.reload_config_entry
2 Likes

Yeah this is a bit weird. config_entry_attr(config_entry_id, 'state') is string results in false, so it must be some other type of object that gets stringified to ConfigEntryState.LOADED etc. at the time the template is rendered.

What kind of object? I really have no idea how to figure that out to be honest. Kind of annoying that there is no type filter similar to how it would work in native Python.

What I did figure out though, is that if you force stringification earlier it with a string filter, then the comparison does work:

{{ config_entry_attr(config_entry_id, 'state') | string == 'ConfigEntryState.LOADED' }}
1 Like

Thanks! this did the trick together with @ShadowFist action

1 Like