Automation where the trigger was activated 30 seconds after

Good afternoon. I wanted to create an automation where the trigger was activated 30 seconds after I or my wife left homes, but I’m having difficulty because of the 30 seconds… I don’t know how to do it.
I currently have no 30 seconds and it looks like this:

alias: Alarme auto-on
description: ''
trigger:
  - platform: zone
    entity_id: device_tracker.a
    zone: zone.zona_alarme
    event: leave
  - platform: zone
    entity_id: device_tracker.b
    zone: zone.zona_alarme
    event: leave
condition:
  - condition: state
    entity_id: device_tracker.a
    state: not_home
  - condition: state
    entity_id: device_tracker.b
    state: not_home
action:
  - service: switch.turn_on
    target:
      entity_id: switch.alarme_418
mode: single

You will have to use state triggers instead of event triggers.

trigger:
  - platform: state
    entity_id: device_tracker.a
    to: 'not_home'
    for: 30
    event: leave
  - platform: state
    entity_id: device_tracker.b
    to: 'not_home'
    for: 30
1 Like

Or in action section add:

  - delay: '00:00:30'

before switch.turn_on…

Solution that @tom_l suggest will ensure, that if you return to home within 30 seconds action won’t be triggered. My proposed solution causes to trigger action after 30 seconds AND every time after leaving home, regrdless returing within 30 seconds or not. Your choice.

1 Like

Yeah but your solution allows for false triggering when the geolocation falsely wanders outside home momentarily. Mine does not.

When indoors and using cell tower triangulation instead of GPS, phone location is woefully inaccurate. I’ve seen it wander up to 500m.

Very true, but isn’t it general problem of low frequency of position updates from the app? Regardless of being home or driving I see updates being send sometimes every 1~2 minutes only. In this case problem with cell tower triangulation will persist also for your solution? Or am I missing something? I sit possible to force/configure high enough position update frequency?

Yes.

My solution requires the location state to be stable for a period of time. Yours just waits a period of time where the state could be wandering all over the place.

Thats my point with low frequency of updates. Even in your case if location suddenly changes based on false triangulation to be out of zone and next update comes in 2 minutes automation will be triggered as well. Indeed, if next update comes within 30 seconds, your method won’t trigger, but mine will.

We ain’t talking about a low update rate issue.

I want the trigger to be when I leave a specific zone (zone.zona_alarme) and not when the state is “not_home”

The solution I want is @tom_l, but the problem is that I want the trigger to be activated after the “device_tracker.a” or “device_tracker.b”, leave from a specific zone (“zone.zona_alarme”), and not when I leave the house

That’s fine. Change tom_l 's example to have from: zona_alarme instead of to: not_home and I get rid of the event: leave which I believe is a typo.

alias: Alarme auto-on
description: ''
trigger:
  - platform: state
    entity_id: device_tracker.iphone1
    for:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
    from: zone.zona_alarme
  - platform: state
    entity_id: device_tracker.iphone2
    for:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
    from: zone.zona_alarme
condition:
  - condition: state
    entity_id: device_tracker.iphone1
    state: not_home
  - condition: state
    entity_id: device_tracker.iphone2
    state: not_home
action:
  - service: switch.turn_on
    target:
      entity_id: switch.alarme_418
mode: single

I’ve tried it and it didn’t work.
Shouldn’t I give the “leave” indication somewhere?

You still have the not_home conditions, which you don’t want if they are not correct. You only need to tell Home Assistant what zone you are leaving (from), because this isn’t a zone trigger it’s a platform state trigger so just like every other state trigger, Home Assistant is only interested in what the state was ( from ) and what the new state is ( to ). Because we don’t know what the new state will be, and it doesn’t matter - only the state that it was previously in ( from ) is important.

I think that it should actually just be from: zona_alarme, not from: zone.zona_alarme. Look at the state history for your device tracker, the state will be the name of the zone without “zone.”

1 Like

Good spot! I overlooked that.
Yup Home Assistant is looking for actual text in the platform state trigger.

it is not working. the state of “device_tracker.iphone1” and “device_tracker.iphone2” is “home” or “not_home”. I want these entities to work with a zone that I created, so I started using “platform: zone”… but then I can’t use the time factor

Yes it is. Sorry.

This is a relatively simple problem but sometimes those are the toughest ones to get finished. :laughing:

Go back to your original automation version, with a zone trigger. Add to the action a delay of 30 seconds, and then another action that is a condition that you are not in zone.zona_alarme. This will do the “debouncing” in the way that you desire. For real this time!

action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - condition: not
    conditions:
      - condition: zone
        entity_id: device_tracker.a
        zone: zone.zona_alarme

That way, if I re-enter the zone during the 30 second period, is the action cancelled?

I found a post with something that I haven’t tested yet, but I think it might solve my problem… I thought there could be a simpler solution, but I think it’s going to have to be with a “script:”