Coming home notification

I cant find a way to notify if the person was away at least for 10 minutes.
Can someone help?

alias: Meldung Jemand kommt nach Hause
description: ""
triggers:
  - entity_id:
      - person.frederic
      - person.jessica
      - person.romeo
      - person.alessia
    to: home
    from: not_home **//////for 10 Minutes///////**
    trigger: state
conditions:
  - condition: state
    entity_id: input_boolean.meldungs_switch
    state: "on"
actions:
  - action: notify.send_message
    metadata: {}
    data:
      message: "{{ trigger.to_state.attributes.friendly_name }} kommt nach Hause."
    target:
      entity_id:
        - notify.homeassistant_durchsagen
mode: parallel
max: 10

nope, this only describes the time for the TO state.

i need from nothome for 10 minutes.
means, the person was away at least 10 minutes.

our phones sometimes jump to away positions for some seconds.
i dont know why, but i can see the moving in the map. maybe a phone GPS problem.

Add a 10 minute delay to the end of the automation and put the automation into single mode.
Parallel mode means it will re-trigger over and over constantly when running.

does not solve my problem.

phone is home, thene changes to not home for 5 seconds and right back at home.
automation is triggered and should not.

OK, then maybe stating the problem instead of what you think is the answer might have been a better move.

If you look at the entity zone.home, it will count the number of people in the zone. If it reaches 0, no one is home or they have their phones off.

What you have there if one of them goes not home, the thing triggers and would be very noisy.

I still suggest single mode.

my question is if there is a from(time conditiion) methode in triggers or someone knows a workaround.

without a nice workaround i have to create 4 separate imput.boolean and my automation will get very complicated.

EDIT: Ignore all this and see Drew’s answer below.

It is a tricky one. Home Assistant does not have any way of accessing the previous to last state change time Only the last state change time. So you would have to track this yourself. One way would be with four triggered template binary sensors. e.g.

# configuration.yaml
template:
  - triggers:
      - id: "on"
        trigger: platform: state
        entity_id: person.frederic
        to: "not_home"
        for:
          minutes: 10
      - id: "off"
        trigger: platform: state
        entity_id: person.frederic
        to: "home"
        for: 5 # seconds, prevents race condition with automation
    binary_sensor:
      - name: Frederic Away for 10 min
        state: "{{ trigger_id | bool }}"

  - triggers:
      - id: "on"
        trigger: platform: state
        entity_id: person.jessica
        to: "not_home"
        for:
          minutes: 10
      - id: "off"
        trigger: platform: state
        entity_id: person.jessica
        to: "home"
        for: 5 # seconds, prevents race condition with automation
    binary_sensor:
      - name: Jessica Away for 10 min
        state: "{{ trigger_id | bool }}"

  - triggers:
      - id: "on"
        trigger: platform: state
        entity_id: person.romeo
        to: "not_home"
        for:
          minutes: 10
      - id: "off"
        trigger: platform: state
        entity_id: person.romeo
        to: "home"
        for: 5 # seconds, prevents race condition with automation
    binary_sensor:
      - name: Romeo Away for 10 min
        state: "{{ trigger_id | bool }}"

  - triggers:
      - id: "on"
        trigger: platform: state
        entity_id: person.alessia
        to: "not_home"
        for:
          minutes: 10
      - id: "off"
        trigger: platform: state
        entity_id: person.alessia
        to: "home"
        for: 5 # seconds, prevents race condition with automation
    binary_sensor:
      - name: Alessia Away for 10 min
        state: "{{ trigger_id | bool }}"

You can then add just this one extra condition to your automation.

conditions:
  - condition: state
    entity_id: input_boolean.meldungs_switch
    state: "on"
  - condition: template
    value_template: "{{ states('binary_sensor.' ~ trigger.to_state.object_id ~ '_away_for_10_min') | bool }}"

The advantages of using triggered template sensors are that:

  1. They are restored after a restart
  2. You can use them elsewhere in your config.
2 Likes

You have to do it in a condition.

4 Likes

Goddammit Drew! :rofl:

I thought I was being so clever.

3 Likes

thank you very much
i implemented trigger.from_state.last_changed and hope it works and will report back. ////edit it works, thank you all
it is difficult to test but one of the most important automations i have.
(it warns you on all alexa speakers if someone enters the home radius.)

alias: Meldung Jemand kommt nach Hause
description: ""
triggers:
  - entity_id:
      - person.frederic
      - person.jessica
      - person.romeo
      - person.alessia
    to: home
    from: not_home
    trigger: state
conditions:
  - condition: state
    entity_id: input_boolean.meldungs_switch
    state: "on"
  - alias: The person was in its previous state ('not_home') for at least 2 minutes
    condition: template
    value_template: |
      {{ now() - trigger.from_state.last_changed >= timedelta(minutes=2)}} 
actions:
  - action: notify.send_message
    metadata: {}
    data:
      message: "{{ trigger.to_state.attributes.friendly_name }} kommt nach Hause."
    target:
      entity_id:
        - notify.homeassistant_durchsagen
mode: parallel
max: 10

While you were busy with all that, I came up with this automation. It should also work if multiple people arrive at the same time:

alias: Welcome message
description: ""
triggers:
  - trigger: state
    entity_id: zone.home
    not_from:
      - unavailable
      - unknown
    not_to:
      - unavailable
      - unknown
    variables:
      arrived: |
        {{ difference(trigger.to_state.attributes.persons,
              trigger.from_state.attributes.persons)
         | map('replace','person.jack_jones','Hi Jack')
         | map('replace','person.jill_jones','Welcome Jill')
         | join(', ')
         }}
conditions:
  - condition: template
    value_template: |
      {{ arrived != ''  and 
          now() - trigger.from_state.last_changed >= timedelta(minutes = 10) }} 
actions:
  - action: tts.speak
    metadata: {}
    data:
      cache: true
      media_player_entity_id: media_player.frontdoor
      message: "{{ arrived }}. Nice to see you!"
mode: single

Note that if people’s states drift from home too often, you’ll not get any notifications. Downside of this version is that anyone drifting will prevent notifications for 10 minutes.

I’dd try to add multiple (types of) device trackers to each person to prevent ti. Also I believe the companion app has options to list your home wifi to help it determine home more accurately.