Template to not trigger an event if device enters a zone within X minutes

I am trying to create a template that will prevent my motion detection camera alert from firing off when I enter zone.home in the past X minutes so I don’t get a “motion detected” alert when I get home, because if I don’t know that someone is at the door when I am at the door, something is terribly wrong. I already setup the following with my door sensor to prevent alerts for when I’m leaving the house:

{{(as_timestamp(now()))-(as_timestamp(states.binary_sensor.front_door.last_changed)) < 600}}

A similar thing won’t work for for a few reasons, mainly because I don’t want a delay after someone leaves.

{{(as_timestamp(now()))-(as_timestamp(states.device_tracker.device.last_changed)) < 1200}}

I don’t know how/what to use to call “zone.home.enter = true” The specific device doesn’t matter (unless it does for template purposes). If any device linked to HA enters the zone, I don’t want to get a motion alert. Wife, kids, wife’s boyfriend. Whatever. I just don’t want a bunch of popups when someone I know (or at least someone with their phone) comes home.

I suppose I could do an if then delay or something, but a this seems perfect for a template in my mind. I’m just smart enough to do it.

Appreciate the help!

Edit: I think I figured it out, but I’m not sure if it’s the best way. Template is as follows:

{#check to see if front door sensor state has changed in 10 minutes#}
{{(as_timestamp(now()))-(as_timestamp(states.binary_sensor.front_door.last_changed)) < 600}}

{#check all device_tracker to see if any are home and they changed within the past 20 minutes#}
{%for state in states.device_tracker %}
  {{ state.entity_id }} = {{ state.state }}
    {% if is_state(state.entity_id, 'home') and ((as_timestamp(now()))-(as_timestamp(state.last_changed)) < 1200) %}
      {{ true }}
    {% endif %}
{% endfor %}

If anyone has a better way please let me know!

I would set a condition in the automation instead. This will prevent the automation from running within the first 10 minutes of entering the zone.

condition: or
conditions:
  - condition: state
    entity_id: zone.home
    state: "0"
  - condition: state
    entity_id: zone.home
    state: "1"
    for:
      hours: 0
      minutes: 10
      seconds: 0

The problem I am having with this is that the state becomes “2” (or “x”) as more people come home. State “1” worked fine until my wife got home, then the condition was never met for her because she entered the zone it changed to “state zone.home=2”

Oh, I did not know that zone.home state had the number of person currently within the zone. I live alone, so… I assumed it was an on-off state… Makes sense :slight_smile:

You could create a helper toggle switch and an automation that turns it off-on whenever someone enters the zone

alias: Enter zone
description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - person.person_1
    to: home
  - platform: state
    entity_id:
      - person.person_2
    to: home
condition: []
action:
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.zone_enter
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.zone_enter

This will reset the “on” time of the helper switch every times someone enters the zone.

Then in your camera motion automation condition :

condition: or
conditions:
  - condition: state
    entity_id: zone.home
    state: "0"
  - condition: state
    entity_id: input_boolean.zone_enter
    state: "on"
    for:
      minutes: 10
1 Like

Thanks so much for the help and for all the effort you put in to it. I hadn’t considered helpers, but am glad you brought that up as an option. Good to know for future needs.

I actually stumbled across an old post by @Bluenight talking about namespaces and that did the trick! I couldn’t figure out how to create and use a variable, but namespace was the answer. I created a variable with a namespace that I could set to true if my conditions were met. Then I output the variable as the last line of the template so the automation executes or not based on the value in that variable. Since it loops through all of the device_trackers I can add more and wont have to mess with the automation.

{% set no_motion = namespace(home = false) %}
{%for state in states.device_tracker %}
  {{ state.entity_id }} = {{ state.state }}
    {% if is_state(state.entity_id, 'home') and ((as_timestamp(now()))-(as_timestamp(state.last_changed)) > 1200) %}
      {% set no_motion.home = true %}
    {% endif %}
{% endfor %}
{{ no_motion.home }}

Hopefully this will help someone in the future.

Bluenight’s post

1 Like