appdaemon icon indicating copy to clipboard operation
appdaemon copied to clipboard

AppDaemon sometimes looses Home Assistant services after HA restart

Open matejdro opened this issue 3 years ago • 10 comments

  1. Create an app that uses home assistant services (for example default/notify)
  2. Start an app and ensure it works
  3. Restart Home Assistant
  4. Attempt to trigger the app again

BUG: Services will not trigger and WARNING AppDaemon: Unknown domain (default/notify) in call_service from my_app will be printed into the console.

This does not happen 100% of the time (some restarts seems to not cause it). It happens more often when updating HA to new version.

Related to the https://github.com/AppDaemon/appdaemon/issues/977

Workaround is to also restart AppDaemon after restarting Home Assistant. This is with AppDaemon 4.0.8 running via Docker.

matejdro avatar Apr 30 '21 06:04 matejdro

setting a delay in the plugin section from appdaemon.yaml is in some cases the only possible option. but its also just good practise to restart AD when you restart HA. that always was and always will be. like its good practise to restart dashboards when you did restart AD.

ReneTode avatar Apr 30 '21 10:04 ReneTode

From what I see, this delay is only for the AD startup? It does not apply when AD is already started up and HA is restarted.

matejdro avatar Apr 30 '21 11:04 matejdro

no its for the startup from the plugin.

https://appdaemon.readthedocs.io/en/latest/CONFIGURE.html#hass-plugin-startup-conditions

if your apps depend on the plugin (default) they are reinitialised when the plugin restarts. the plugin restarts after it has lost connection to HA, and waits untill it gets a signal from HA that its available again. HA gives that signal before all services are available, and the apps in that case will reinitialise before AD can know about the services. by adding a delay to the plugin startup conditions AD waits after HA has given the signal untill the delay is over.

you can also wait for an event. so for example you could create an event in HA after the notify is setup in HA and let AD wait untill it gets that event.

ReneTode avatar Apr 30 '21 12:04 ReneTode

Plugin delay doesn't work because AD loads services before evaluating conditions. It was discussed in https://github.com/AppDaemon/appdaemon/issues/977.

definitio avatar May 01 '21 07:05 definitio

What if, instead of having arbitrary delay, similar option to the state would be added, but for services? So you would list all services that you require and plugin would wait until all those services are available before continuing with startup.

matejdro avatar May 01 '21 10:05 matejdro

@definitio did you try creating an event in HA, that fires at the moment you think it should be finished?

@matejdro the problem is that AD has no option to listen to added services as far as i know. if there was an event in HA like service_added, AD could listen to that and add that service and the problem would be gone.

there will always be problems, no matter how hard the devs try, if people restart HA without restarting AD. and as soon as they fix it HA changes something and other problems rise.

the best thing to do is just always restart AD after HA is restarted.

ReneTode avatar May 01 '21 15:05 ReneTode

Maybe it could poll every second until services are available?

Here is why I think "just restart AD" is not a good solution:

  1. Lots of HA configuration still requires restart of the HA to apply, so HA restarts are not that rare
  2. HA can be easily restarted from its GUI
  3. There is no easy way to restart AD from its GUI. I have to ssh into the server, enter root password and restart the entire docker container which is a bit of a hassle.

matejdro avatar May 01 '21 15:05 matejdro

there are a lot of easy ways to make sure that AD restarts when HA restarts. the easiest way is, just to restart your device completely. but as in the topic mentioned here, it can also be done with cron, with a bat file or anything like that.

i even know of people who did create switches in HA to restart AD

AD depends on its plugins. so its always best to make sure that what is depending is restarted. (like with depending apps)

the problem is known, but there is no easy solution, and the dev that could take a deeper look at it is busy IRL, so there will not be a fast solution. so the best thing to do for now is to look at other solutions.

ReneTode avatar May 01 '21 18:05 ReneTode

All of those hinge of HA having access to docker on the server machine which I don't really want (I don't want HA to be able to restart other services willy nilly for security purposes).

matejdro avatar May 02 '21 07:05 matejdro

then i guess youll need to try out workarounds (like creating an event in HA), or do it the hard way untill someone able to find the trouble has the time and this is corrected.

maybe your lucky and odia finds the trouble now that he installed latest HA.

ReneTode avatar May 02 '21 11:05 ReneTode

This shouldn't;t be an issue any longer - AD dynamically adds services that are created up to 10 minutes after a HASS restart

acockburn avatar Mar 04 '23 01:03 acockburn