Zone exit for x minutes automation trigger

When I exit a certain place at a certain time, I’d like to turn on one of my switches in my HA setup
The thing is, I sometimes exit the zone for 1-2 minutes and I’d like to set a trigger for ‘exit zone for the past 10 minutes’

Can anyone point me in the right direction ?

There’s two ways to trigger an automation when a device leaves a zone. (BTW, I assume you’re referring to a device_tracker entity.) The first is with a zone trigger, but AFAIK, it doesn’t have a “for” option. So you’ll probably want to use the other type of trigger, which is a “standard” state trigger. A device_tracker’s state will be set to the name of the zone while it’s in the zone, and will change to something else (probably not_home) when you leave the zone. To use the “for” option you have to use “to”, so something like this might work for you (assuming when you leave the zone you don’t go into another zone within the 10 minute period):

automation:
  - alias: Leaving a zone
    trigger:
      platform: state
      entity_id: device_tracker.XXX
      from: 'ZONE NAME'
      to: not_home
      for:
        minutes: 10
    action:
      service: switch.turn_on
      entity_id: switch.YYY

If you want this to only happen during a particular time period then you can add a condition for that.

It seems like a fair idea, how did I not think about this before :wink:
I should just use it without the ‘to’ statement because I don’t know the new state of the tracker

EDIT:

I’ve tried it and it seems that the trigger doesn’t allow for only ‘from’ statement
So we’re still with the original question

That is why I said, ‘To use the “for” option you have to use “to”’. The trigger can be used with just “from”, but not if you’re also using “for” – it’s the use of “for” that requires “to”. That’s basically because “for” (as currently implemented) means trigger only if the entity that changed stays in the “to” state for the specified amount of time. I think it would be nice if “for” could be used with “from”, which to me would mean the entity changed from the specified state and didn’t go back to that state in the specified period. But that’s a feature request. :wink:

I would think that in most cases, when you leave a zone you probably don’t enter another one in less than ten minutes, in which case what I suggested would work. But if you really don’t want to count on the fact that the device stays out of all zones during that ten minutes, then another way might be something like this:

automation:
  - alias: Leaving ZONE NAME
    trigger:
      platform: state
      entity_id: device_tracker.XXX
      from: 'ZONE NAME'
    action:
      service: script.turn_on_10_min
  - alias: Entering ZONE NAME
      platform: state
      entity_id: device_tracker.XXX
      to: 'ZONE NAME'
    action:
      service: script.turn_off
      entity_id: script.turn_on_10_min
script:
  turn_on_10_min:
    sequence:
      - delay:
          minutes: 10
      - service: switch.turn_on
        entity_id: switch.YYY

The idea is, whenever you leave the zone it starts a script. The script waits 10 minutes, then turns on the switch. But when you enter the zone, it cancels the script if it happened to be running. Which means, if you leave a zone, but re-enter it within 10 minutes, the script will get started, but then be canceled, so it will never get to the point of turning on the switch.

3 Likes

Thanks for that, this actually might work