Condition for a trigger

I have a camera by the front door that basically detects a person and outputs when someone arrives to my speakers. I want to add a condition if the front door was opened 2 mins or less then don’t trigger the automation. I’m using the following script but it’s not working. Can someone assist with what’s incorrect? Is there an easier visual way to do this with a not condition?


{{ 
( expand('binary_sensor.alarm_zone_2')
| sort(reverse=true, attribute='last_changed')
| map(attribute='last_changed')
| list )[0] > utcnow() - timedelta(minutes=2)
}}

Why not just use a state condition with a defined duration?

If you just want to use a template condition,… there’s no reason to use expand or map for a single entity. Your original template can be simplified to:

{{ states.binary_sensor.alarm_zone_2.last_changed > utcnow() - timedelta(minutes=2) }}

Though, I’m not sure that is exactly what you described… The condition represented by that template is “Has the state of the door sensor changed in the last 2 minutes?”

Thank you. Any condition change will work but I’m trying to detect if the front door was open then do not execute the notification that someone is at the front door. Appreciate the reply and I’m a newb when it comes to HA, so I’m learning along the way.

Maybe have a helper that is set if your door is opened setting it to true for 2 minutes. Then if your camera fires off you check to see if that helper is true or false, and go from there.

Just spitballing.

Thanks for the reply. I’m running into an issue now with sunlight triggering it so now I’m trying to use 2 cameras and detecting a person to trigger it. I’m going to try the trigger, but can some explain why this logic doesn’t work? So this is an entity of another camera out front I basically want to say if it detected a person in the last minute as a condition along with the main camera, then trigger the alert. So this entity either displays Cleared or Detected. Instead of last_changed should I use “Detected Occupancy”? Sorry newb learning here but keep trying things and getting stumped, but the helper is a good idea I’m going to explore today.

binary_sensor.front_center_person_occupancy.last_changed > 0 - timedelta(minutes=1) }}

I created a helper which is working to detect the person on the other camera and then I’m trying to use it as a value template in my automation to detect if it happened in the last minute. Still not working. I’m stumped.

"{{states.binary_sensor.front_person_binary.last_changed > now() - timedelta(minutes=1) }}"

I also plugged this into dev tools and it’s returning correctly

If it works in the Template editor, but not in an automation, post the whole automation.

here you go. the second value template is currently disabled, but does work for testing purposes.

alias: Alert - Person At Door
description: ""
triggers:
  - type: occupied
    device_id: 9d0ca0806756d6c1f8eb3ecdfdff41f4
    entity_id: ade9b2f067d88596c9b3497de85351ff
    domain: binary_sensor
    trigger: device
conditions:
  - condition: template
    value_template: >-
      "{{states.binary_sensor.front_center_person_occupancy.last_changed > now()
      - timedelta(minutes=1) }}"
  - condition: time
    after: "07:00:00"
    before: "22:00:00"
  - condition: template
    value_template: >-
      {{ states.binary_sensor.alarm_zone_2.last_changed > utcnow() -
      timedelta(minutes=2) }}
    enabled: false
actions:
  - action: script.text_to_speech_on_sonos
    metadata: {}
    data:
      entity_id: media_player.unnamed_room_5
      message: Ding Dong Ding Dong, Front Door
      min_wait: 1
      volume_level: 0.81
  - action: script.text_to_speech_on_sonos
    metadata: {}
    data:
      entity_id: media_player.family_room
      message: Ding Dong Ding Dong, Front Door
      min_wait: 1
      volume_level: 0.81
    enabled: false
  - action: script.text_to_speech_on_sonos
    metadata: {}
    data:
      entity_id: media_player.unnamed_room
      message: Ding Dong Ding Dong, Front Door
      min_wait: 1
      volume_level: 0.81
    enabled: false
mode: single

Just to add to this when I test it from the debug area it passes, but when I click test in the automation it does not pass. What is happening here? Bug or syntax issue?

Update; So I went in to the yaml editor and basically replaced the line with the syntax above and it appears to work now. I wonder if there is some sort of bug with the visual editor and a value template.

I know in the past there were issues with UI condition tester not working with Template conditions. The conditions work as they ought to when the automation runs, it just the tester that doesn’t work.

Appreciate the help none the less, got what I needed. The community here is awesome!