Better Presence detection automation with multiple triggers

Hi there,

i recently did an automation that has the house speak to us when my wife or myself connect to the wifi after 2 minutes. (typically enough time to wait for us to walk in the front door)

what i’m trying to do is still use the device trackers as the trigger, but also wait if the front door opens after the device tracker trigger or the front door motion trigger happens.

is there a way to have an AND in the trigger? it doesn’t seem so without some type of automation template.

the first code block below was my original try, but wouldn’t work because the conditions werent met before the wifi connected

alias: People Greeting
  trigger:
    - platform: state
      entity_id:
        - device_tracker.champ
        - device_tracker.mari
        #- group.household
      from: 'away'
      to: 'home'
      for: '00:02:00'
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.foyer_4_in_1_sensor_209
        state: 'on'
      - condition: state
        entity_id: binary_sensor.front_door_215
        state: 'on'
  action:
      - service: script.speech_engine
        data_template:
          person_arriving: >
            {% set person = trigger.to_state.attributes.friendly_name %}
            {%- macro greeting_sentence(person) -%}

i’m looking to do something like this (this works, but the speech engine uses the motion or the door name vs the device tracker condition. would there be a way to use a variable that can grab the condition that was met?

  trigger:
    - platform: state
      entity_id:
        - binary_sensor.front_door_215
        - binary_sensor.foyer_4_in_1_sensor_209
        #- group.household
      from: 'off'
      to: 'on'
      #for: '00:02:00'
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: device_tracker.champ
        state: 'home'
        #for:
        #  hours: 0
        #  minutes: 0
        #  second: 10
      - condition: state
        entity_id: device_tracker.mari
        state: 'home'
        #for:
        #  hours: 0
        #  minutes: 0
        #  seconds: 10
  action:
      - service: script.speech_engine
        data_template:
          person_arriving: >
            {% set person = trigger.to_state.attributes.friendly_name %}
            {%- macro greeting_sentence(person) -%}

Just a thought, You could maybe two automations running. have the fIrst trigger/automation(phone connects to WiFi) start a timer countdown for two minutes(global variable somehow?). If the second trigger triggers(door opens) AND the condition of the timer being >0 , then run your text-to-speech. I would be very interested in using the code if you can make that work :slight_smile:

is there a way in the action to pull what condition was met? similarly to the

trigger.to_state.attributes.friendly_name

that seems like the simplest thing to do. then i could use the front door sensor, front door motion and or the door unlocking as the trigger and define which condition allowed the action to happen (device_tracker.champ has been connected for the past 2 minutes)

the main reason i want to stay away from the timer is that the timer does not persist a HA restart. granted, the system has only typically restarted when i force it, i would like to stick with something stateful

Configure a Bayesian sensor that combines the two actual sensors?

that is a good idea. i’ll look more into it. i’ve been reading about that here and there, but wasn’t sure how i should implement it

A wait_template probably does what you want:

alias: People Greeting
  trigger:
    - platform: state
      entity_id:
        - device_tracker.champ
        - device_tracker.mari
      from: 'away'
      to: 'home'
  action:
      - wait_template: >-
          {{ is_state('binary_sensor.foyer_4_in_1_sensor_209,'on') or is_state('binary_sensor.front_door_215', 'on') }}
        timeout: "00:02:00"
        continue_on_timeout: false
      - service: script.speech_engine
        data_template:
          person_arriving: >
            {% set person = trigger.to_state.attributes.friendly_name %}
            {%- macro greeting_sentence(person) -%}

I’ve removed the “for” portion of your trigger. So, what happens is, when either device tracker comes “home” it will start the action. The action waits for the template to be true (either the door opens or the motion sensor is activated). If 2 minutes go by and neither of those things happen, the action ends. If one of those things does happen, though, the script proceeds and performs the speech output.

1 Like

Oh, as a side note, I think the proper from: state is “not_home” as opposed to “away”.

ah! i keep forgetting about the wait template.

that looks like it just might do the trick. i will give this a go after work today.

and yes, it is indeed not_home. made that mistake early on :slight_smile:

will continue to also explore the bayesian sensor in general. looks insanely powerful, but i haven’t been able to invest the thought equity there yet.

still trying to knock off the ‘low hanging fruit’ to make my wife happy so i can dig deeper in the more complex stuff

I already have the same automation, and I have thought about this.
I use my gps and my wifi tracker to trigger my presence. If either of them trigger “home”, then it means I am at the house.
Now keep in mind that the trackers can be late detecting you… Guess what… you can walk through the door, and the trackers may still see you as away. There goes a bad automation…
I think the best approach for this, is to leave it as it is.
Just wait 2-3 minutes after one of the trackers show you as “home” and then do the actions.

Bayesian sensors appear very useful. I’ve yet to actually use one though. I start to, fiddle with it for a while, and then end up doing it another way.

One note for this particular use case, the Bayesian sensor will be “on” or “off”. So… if you’re wanting the name of the person who came home to be announced, it won’t help you much there. You could use it, perhaps, to detect “hey, someone just came home” based on device trackers, motion sensors, and door sensors. And if you have two of them you could have “Champ just came home” and “Mari just came home”. But, in all of these cases, your going to want to use a time period to determine it, which means you’ll also need binary_sensor templates with on_delays and such and it gets really complicated. Especially when all you need is “the door opened within 2 minutes of me coming home”.

The trick is, to make it “really cool” you want it to happen right when you open the door. Because then it feels like it really knows that you came home exactly when you came home. So I agree with you, just use the triggers. And then the wait_template gives it that extra touch. Also, keeps it from announcing “Champ is home” just because your device tracker goofed up and reported you as away and then home again, since it also requires the door to open in that window of time, you get no false reports.

For that I have made a boolean that turns on when I am away for more than 10 minutes. The “greeting automation” only triggers with the condition the boolean is OFF. :wink:

Edit: I just reviewed my automation…
When I leave home for more than 10 minutes the boolean turns ON.
When one of the trackers report as “home”, the boolean turns OFF and THAT triggers the “greeting automation” (with a 3 minute delay)

Very smart. I love that there are so many ways to tackle the same problem.

1 Like

thanks for all the input. these are all very valid and helpful points. i’ve also been working on implementing boolean inputs for conditions to also act as overrides. has been super helpful.

just a quick update. i decided to dive in now, and the wait template seems to be doing the trick quite nicely.

will need to now add some logic for these two device trackers to account for little network blips. will look at implementing that 10 minute timeout.

thanks again for the help!

note: in your code sample, i did notice there was a missing single quote after the first device entity. (after _209)

another thing i thought was interesting was the syntax coloring in Atom editor… for some reason it doesn’t shade the keys for the timeout and continue on timeout properly but it works perfectly.

Ah yes… I did miss a quote. Thank you.

Atom, for me too, seems to have issues with inline jinja templates. Not sure why… but it colors them incorrectly every time I use a multi-line value_template or wait_template.

quick question, is there a way to get two actions to happen? what i mean is i have myself and my wife as device trackers. if we both arrive, only one of us seems to get picked up by the automation and the speech engine trigger. there doesn’t seem to be any rhyme or reason as to which one happens first based on watching it happening in realtime.

is there a way to catch both then add each or both people in of each individual trigger?

Reviving this thread as it’s the closest I’ve seen to my situation.

I want to use two GPS sources (GeoFency and OwnTracks) to trigger my arrival back home. Both must agree I’m home (think: two keys to fire the missles) to cut down on false positives.

The part I’m missing from the above thread is how to code “AND” vs “OR” into the wait_template - ie, within one minute both must agree or abort the action.

Thanks all.