So, you’ve received many suggestions, which is great. Lots of things to learn with HA!
I’ve been using HA for years and have actually worked on some of the core code (automations & scripts, as well as the Life360 integration.) That doesn’t make my suggestions any better, but I do have some unique insights. Anyway…
For a newbie, I suggest using the most “HA way” of doing things, at least to start. In this particular scenario, I’d recommend simplifying. You should be able to do this strictly via the UI. (There’s nothing wrong with editing YAML files directly. Most experienced/advanced users do. But “baby steps”… lol)
If I understand correctly, you already have two person
entities created, each associated with a corresponding device_tracker
entity. That’s all you should need.
For the automation, create one “Numeric state” trigger. For “Entity”, pick “Home/zone.home”. Then put 1 in the “Below” box. Once you’ve done that, if you were to select the “Edit in YAML” option for the trigger, it should look like this:
Then, put whatever you want in the Actions section.
This automation should run whenever the numeric interpretation of the state of the Home Zone entity (which is representation of the number of person
entities located within that zone) changes from a value of 1 or more to a value below 1 (i.e., 0. To state that simply, when the last person leaves the Home Zone.)
It’s important to understand that automation triggers “fire” only when something changes. E.g., when the automation is first created (or if it gets reloaded, e.g., due to being changed via the UI automation editor), it won’t fire if the current numeric value of zone.home
is already below 1. The state of zone.home
has to change from a value of 1 or more to a value below 1.
Also, remember “not home
” is not the same as “not_home
”. For device_tracker
, person
, and group
entities made up of these, the states “home
” and “not_home
” have specific meanings. The former usually shows as “Home” in the UI, whereas the latter usually shows as “Away” (or the equivalent words for other languages.)
Hope this helps.