Help me understand if, and, or in automations and some presence questions

Im fine with copying and pasting automations and such, but I feel I dont know what Im doing.

At the moment I working on presence detection and notifications.
I figured out how to get notification when a user arrives home, but i would like it to have a condition, that none of the other two phones are at home. I would also like to setup this up regardless of whom comes home, without copying and pasting the same automation over and over, and this is were I feel I dont understand what Im doing. So if anybody would be so kind to give me an simple example of the following scenario:

  1. If X arrives home while Y and Z is not home, notify with pushbullet that X is home. The notification should user Xā€™s custom name, not the phone-name.

  2. If anybody is home and someone arrives, have Sonos TTL announce whos coming home (entering a zone)

I can read the documentation and guess how to use it, but Im also uncertain about the formating/order of things. For example, when do I use a " - " before something, like ā€œidā€, ā€œentityidā€ etc? Where do I place a condition, after for example trigger, platform, from, to? The coin hasnā€™t dropped yetā€¦

And the presence questions: Which is the fastest and most reliable way to get presence? I use router presence today, but its quite slow. Should I enable more device trackers, like nmap, owntracks and so forth and have them working together? I only use IOS devices, is the app able to update location somehow?

Hope this makes senseā€¦

Cheers!

You can only use the entity ID in an automation, not the friendly name. For things like formatting see here, and the samples in the docs.

In terms of speed of detection, Iā€™ve generally found that nmap and bluetooth are faster than my router. My personal take is to use those, and an app (like the iOS app) for GPS location tracking, then put them all in a group. As long as any member of the group is home then the group is home.

So, at this point, Iā€™d suggest the following approach

  • One group per person (group.person_X), combining all the device trackers for each person.
  • One group that combines those groups to track if any person is home (group.all_people)
  • One group for person Y and Z (group.all_adults)
automation:
  - alias: 'X made it home'
    trigger:
      - platform: state
        entity_id: group.person_X
        to: 'home'
    condition:
      - condition: state
        entity_id: group.all_adults
        state: 'not_home'
    action:
      - service: notify.yournotifier
        data:
          message: "X made it home"

  - alias: 'Somebody else is home'
    trigger:
      - platform: state
        entity_id: group.person_X, group.person_Y, group.person_Z
        to: 'home'
    condition:
      - condition: state
        entity_id: group.all_adults
        state: 'home'
        for:
          minutes: 5
        # This requires that somebody else has been home for at least 5 minutes
    action:
      - service: tts.say
        data:
          message: "{{ trigger.to_state.attributes.friendly_name }} just arrived"

You could also use the proximity component and notify when people are heading home and within your chosen distance.

Iā€™d suggest that next time you ask one question per topic, itā€™ll be easier to respond to your questions that way :wink:

Thank you so much, this will get me a long way. Much appreciated!

As a slight aside, this article shows a great way to combine a number or presence sensors via bayesian probability to give greater accuracy:

Pretty good answers so far. Iā€™ll give you a slightly different take.

First, I use Life360 and an integration I created. (See Life360 and Life360 Device Tracker Platform.) Iā€™ve been using Life360 for years, long before using HA, and I find that it works very well and suits our need for HA presence detection perfectly. Iā€™ve also started looking at Google Maps Location Sharing for people who are often at our house but that donā€™t use Life360. So, basically, in my case, we only have one device_tracker per person and thatā€™s all weā€™ve needed. However, it does depend on a cloud service, so if youā€™re looking for something else, the suggestions above are excellent.

Next, as suggested, you should have a group that contains all the entities that ultimately track the individual people. E.g., if you just use a single device_tracker per person, then thereā€™s already a group you can use that is automatically maintained - group.all_devices. Thatā€™s what I use. Or, if you create other groups for each person then you should create a group that contains all of those (one per person.)

Next, as mentioned, this ā€œall_devicesā€ or ā€œall_peopleā€ group will be ā€˜not_homeā€™ when nobody is home, and ā€˜homeā€™ whenever at least one person is home. So itā€™s pretty easy to create a trigger for the first person coming home. Itā€™s just the group changing to ā€˜homeā€™.

The ā€œsomeone else just got homeā€ trigger, however, is a bit trickier. The easiest way to do this is to use a state to: 'home' trigger and list out the individual device_trackerā€™s or groupā€™s (depending on what youā€™re using, one per person.) This will trigger whenever anyone gets home, regardless of how many people are already home. But you donā€™t want this to trigger for the first person. Thatā€™s the harder part. You might think ā€œIā€™ll just use a condition that all_people is ā€˜not_homeā€™ā€, but that probably wonā€™t work because as soon as the first person comes home, that group will also transition to ā€˜homeā€™. Granted it might take a few milliseconds, but I wouldnā€™t count on it still being ā€˜not_homeā€™ when the automationā€™s condition is evaluated.

So, what I do is I have a template sensor whose value is the number of people home. It just counts the number of entities in the all_devices group whose state is ā€˜homeā€™. You can then use that in a numeric_state trigger with above: 1. (And contrary to what the docs say, this will trigger whenever the count changes and itā€™s value is above 1, not just when it ā€œcrosses the thresholdā€ from 0 to 1.) The nice part is if you add people trackers, you only have to update the group (or it will update automatically if youā€™re using group.all_devices), but you donā€™t have to remember to update the automation triggers, too. But weā€™re not out of the woods yet! :wink: This is because in an automation triggered this way, we donā€™t have direct access to the entity that came home.

So, what Iā€™d suggest is the automation runs a script (if it isnā€™t already running) that waits a few seconds, then announces the names of anyone who just came home during that wait period. I actually worked with someone else to implement this technique. The other advantage is if two or more people get home at about the same time there will only be one announcement with all their names.

Anyway, hopefully this gives you some other ideas. If you like any of this and would like to try it and need some help doing so, just let me know. :slight_smile:

1 Like

Trying to set the record straight. It seems I came to this conclusion incorrectly. The numeric_state trigger does seem to work as documented.