Setting the "turned on by" informtion

Tags: #<Tag:0x00007fc3f75d44a0>

Sometime relatively recently home assistants got to know who changed the state of a device.
For example, a light that is turned on via the Lovelace interface is “turned on by user”. But if it’s turned on by automation then the name of the automation is there. If it was appdaemon turned on then it says “turned on by Hass.io” I am not sure what to call this, but its what I am looking for!

I want to be able to have app daemon set this “by value” and also check its last value so that an action can be conditional. For example, if the light turned on via appdaemon due to motion then turn it off after a delay. But if it was turned on by the switch then leave it alone (or have a longer timeout).

You can create an extra user in HA for AppDaemon, like “appdaemon” and can set a long live access token from this user in the AppDaemon config as described here:
https://appdaemon.readthedocs.io/en/latest/CONFIGURE.html#hass-authentication
You can then see in the HA logs if something was done “by appdaemon”. In AppDaemon when you register to an event or service call, you can see which user is responsible for the event in the event data, which is passed to the callback method. So, you can check if it is AppDaemon and do something or if not do something else.

Thanks for the documentation link and explanation. I will give this a try, it will definitely help.

I guess I was really hoping for calls to services like turn_on to accept an argument that populated this data with something user provided, so rather than “appdaemon” I could write in something about the class instance, maybe even the code branch which triggered it. I get this from logs today but unifying the information into the logbook is attractive.

I wonder how the automations work, they put their automation name in the log book