Trigger automation only if other trigger fired within timeframe

I have two zones set up in order to definitively determine if someone is leaving or returning home. I’ll explain why later but let’s say the zones are Home and NearHome.

How do I set up a condition that the NearHome zone was entered/left by the same person within say 15 seconds before entering the Home zone?

Goal:
I’m trying to automate the garage door. Simply using the Home zone with basic enter/leave event is insufficient because there’s another zone, RearNeighbor, behind the home that the person goes too often so enter/leave triggers when they are not actually leaving/returning. I looked at the Zone and Template documentation and several other posts but couldn’t find anything similar enough.

I did see suggestions for somewhat similar things that effectively said to make the NearHome zone set/clear a helper variable, which would work, but I want to do similar things with RearNeighbor and both with multiple people so that gets to be a lot of distinct helper variables. I want to avoid the extra overhead and maintenance and just use proper templating to check if the person who triggered the home zone also triggered the NearHome zone within the timeframe.

I’d make a trigger based template sensor that just stores a timestamp, then use a template condition that compares the times. Not necessarily easy to do, but it would always work.

template:
- triggers:
  - trigger: zone
    <All your settings here for trigger>
  sensor:
  - name: Last Trigger
    unique_id: last_trigger
    state: "{{ now() }}"
    device_class: timestamp

then in your automation

- condition: template
  value_template: "{{ now() - states('sensor.last_trigger') | as_datetime <= timedelta(seconds=15) }}"

or you could make a template binary sensor with auto_off

template:
- triggers:
  - trigger: zone
    <All your settings here for trigger>
  binary_sensor:
  - name: near home
    unique_id: near home
    state: "{{ 'on' }}"
    auto_off:
      seconds: 15

then in automation

- condition: state
  entity_id: binary_sensor.near_home
  state: 'on'

the auto-off would be nice. I’m not familiar with template binary sensors so how would you set this up? I’m looking at it now and don’t see an option for YAML so not sure how to utilize the YAML you provided.

I would need 3 of these template binary sensors per person to determine inbound/outbound to both locations, so I’d be starting with 6 of them. :sweat_smile:

You put the yaml in your configuration.yaml file. If you’re on HAOS, you’ll need the file editor app or vscode app to edit the file.

I’m getting this error when adding to configuration.yaml. I don’t see “triggers” anywhere else.

Those warnings aren’t real, the syntax checking extension is significantly out of date.

alright, I kept what I had and reloaded the YAML and now I see it in the list of helpers. I tried to use Developer tools → States to simulate the person entering the zone. it didn’t do anything but I maybe I didn’t set it up properly. How can I simulate the trigger for a person entering a zone so I can test it?

Zone triggers don’t work that way, they require GPS coordinates of the device_tracker to change… that’s why only GPS-based trackers can be used with them:

You can’t use set state to simulate a zone trigger.

if I’m understanding correctly, you’re saying I need to simulate the GPS coordinates for the device of the person I want to simulate, which will in turn trigger the zone. I’ll try that.

Truthfully, I’ve never tried it… or if I have, it was quite a while back.

You might be able to do it using the device_tracker.see action with defined gps data.


Presumably, you want to open the garage door for the car, so have you considered adding a device tracker for the car instead of trying to proxy it off a person?

that was my first idea but I couldn’t find a tracker for that purpose. I even looked for long-range proximity sensors but didn’t find anything. do you have an idea of what device could be used as a tracker for a car?

I just keep a BLE beacon in the glove box.

1 Like

that’s actually a great idea. I’m having issues with Bluetooth on my HAOS and can’t add any Bluetooth devices so that’s going to be my next post.

setting the device lat/log worked, as did the binary sensor helper.

this solution is more complicated than I was hoping for and requires a bit of sprawl in comparison to everything being together in a single automation with a template condition checking recent zone triggers but I appreciate all the help, @petro and @Didgeridrew, and am happy to get something that at least works.

To recap the solution presented so far in this thread:
Follow petro’s suggestion by putting the template directly into configurations.yaml and ignore any warnings about DisallowedExtraPropWarning.
Then reload the YAML configurations (Settings > System > Power > Quick reload) to pick up the changes.

To test the new helper or automations using it:
Go to Developer tools > States and copy the lat/log coordinates from the target zone and paste it as the lat/long in the device tracker tied to the target person.

Another solution for the specific problem of automating a garage door opening/closing when a person tracker won’t suffice due to entering/leaving the home zone but not needing the garage door opened, i.e. visiting a neighbor, is a BLE beacon placed in the vehicle(s) which would be a much simpler solution since it could reliably be tied directly to the basic home zone events.