Help Needed: Alert for Prolonged Unlocked and Closed Door, Clearing Only When Locked and Closed

I’m using the alert integration in Home Assistant to notify me when any entrance door remains unlocked and closed for an extended period. However, I’m facing an issue with the alert’s deactivation behavior.

Current behavior:

  1. The alert activates when:
    • The door has been closed for 10 minutes AND
    • The door has been unlocked for 10 minutes
  2. The alert deactivates when:
    • The door is opened OR
    • The door is locked

Desired behavior:

  1. Activation remains the same
  2. The alert should deactivate ONLY when:
    • The door is locked AND
    • The door is closed

The purpose of the 10-minute delay is to avoid false triggers, such as when we’re actively using the door (e.g., bringing in items from the car) but it remains unlocked.

Here’s my current implementation:

# Binary Sensor (binary_sensor.front_door_unlocked)
{% set delay = 10 %}
{% set lock_open_duration = (now() - states.lock.front_door.last_changed) > timedelta(minutes = delay) %}
{% set lock_unlocked = is_state('lock.front_door', 'unlocked') %}
{% set door_closed_duration = (now() - states.binary_sensor.alarm_system_front_door.last_changed) > timedelta(minutes = delay) %}
{% set door_closed = is_state('binary_sensor.alarm_system_front_door', 'off') %}

{{ (lock_open_duration and lock_unlocked) and (door_closed_duration and door_closed) }}

# Alert
front_door_unlocked:
  name: Front Door Unlocked
  entity_id: binary_sensor.front_door_unlocked
  state: "on"
  repeat: 5
  can_acknowledge: true
  skip_first: false
  title: "🚨 Alert"
  message: "Front Door Unlocked"
  done_message: "Issue cleared"
  notifiers:
    - me
  data:
    push:
      sound:
        name: "default"
        critical: 1
        volume: 1.0
    tag: "alert-front-door-unlocked"
    actions:
      - action: ALERT_FRONT_DOOR_UNLOCKED_ACKNOWLEDGE
        title: "Silence alert"
      - action: ALERT_FRONT_DOOR_UNLOCKED_LOCK_DOOR
        title: "Lock door"

Is it possible to achieve this using templates? I know I can do this via an automation with an input_boolean sensor, but I’m curious if there’s a template-based solution. Any help from template experts would be greatly appreciated!

I really don’t understand what you want but if this is true:

Then use a triggered template binary sensor,

template:
  - trigger:
      - platform: state
        entity_id: etc...
        to: etc...
    binary_sensor:
      - name: Foobar
        state: etc...

https://www.home-assistant.io/integrations/template/#trigger-based-template-binary-sensors-buttons-images-numbers-selects-and-sensors

Thanks! This may help me but not sure. So I rewrote my post (Thanks Claude). Basically the way my template works today is:

It is ON when the door is closed for 10mins AND the door is unlocked for 10mins. The purpose for this is to avoid false triggers, which in my situation a false trigger is when 10mins have elapsed and a door is unlocked but we are actively opening and closing the door. This came about from the fact that sometimes we unlocked the door but maybe were actively opening and closing the door because maybe we were bringing in things from the car or some similar situation. Currently this template works well to activate. However, the deactivation is what doesn’t work to my ideal preferences. I want it to deactivate ONLY when the door is locked AND the door is closed.

After looking at this deeper i recalled i used these before once, and man this is exactly what I needed! Thanks for reminding me of these!

2 Likes