Prevent zone automation from triggering when briefly entering zone

I have the following automation that sends a notification to my wife when I leave work:

- alias: "Notify Pieter Going Home from Work in XXX"
  trigger:
    platform: zone
    entity_id: person.pieter_rautenbach
    zone: zone.pieter_work_xxx
    event: leave
  condition:
    - condition: time
      after: "16:00:00"
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: script.turn_on
      entity_id: script.send_pieter_work_xxx_to_home_eta

This works well, except that my office location is right next to a highway (the one I normally take home) and there has been cases where I/we drive past the office on that same highway (e.g. on holiday and going to the city for the day), which will cause it to trigger.

What is my best resort? Creating a helper sensor that e.g. turns true if I’m at work for a certain time? But if this helper uses the same zone information, then that sensor will also change state at the same time the automation’s trigger will fire (which would be the same as incorrectly using a zone condition in the automation, because it will then never fire/be true), unless I use a delay in the helper to only change state a few seconds later.

The closest post I could find was this one: Using zone triggers with “for” - Configuration - Home Assistant Community (home-assistant.io).

2 Likes

I believe that if you switch to a state trigger, you can use pieter_work_xxx as the to: state and put a for: time to debounce the automation.

If that doesn’t work, you could put a delay followed by a condition in your automation action to check that you are still in the zone before proceeding with the rest of the actions.

Edit: the linked post indicates that my first proposal would work.

Would it work? If I understand it correctly the for will only check that the zone has changed from work to not home, and then has stayed as not home for a certain amount of time. The time check needs to be moved up to the from. As in, when the state changes from work to not home, check if the person was at work for more than a set amount of time.

1 Like

You’re right. I didn’t note that it’s for leaving, not entering.

How often is your wife with you during these false triggers? If you have location tracking on her phone, and you’re often together when these false triggers happen, you could add a template condition that checks that you are not together (that is, not closer than say a quarter mile of each other).

What about creating a History Stats sensor that logs how long you were at work that day…

sensor:
  - platform: history_stats
    name: Pieter hours at work today
    entity_id: person.pieter_rautenbach
    state: "pieter_work_xxx"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

Then use that as a condition for your automation:

- alias: "Notify Pieter Going Home from Work in XXX"
  trigger:
    platform: zone
    entity_id: person.pieter_rautenbach
    zone: zone.pieter_work_xxx
    event: leave
  condition:
    - condition: time
      after: "16:00:00"
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: numeric_state
      entity_id: sensor.pieter_hours_at_work_today
      above: 5
  action:
    - service: script.turn_on
      entity_id: script.send_pieter_work_xxx_to_home_eta
2 Likes

Something similar would probably work pretty well, I’m just unsure if this is the best execution. Unless tracking work hours is the goal, is there a point to making it a history stat? Also, tracking the last state change time instead and ensuring it wasnt too recent would be usable for any zone change, rather than just work.

Thank you for all the replies and ideas!

I’m going with @Didgeridrew’s suggestion. Tracking my time in a zone is useful to me in other ways too.