Do something after a device has been in a certain state for a certain time then changed

Hello, First-ever post here.

I’m trying to create an automation as follows.

  • Utility Room Door has been left open for over 5 minutes, notify phone (done this one)
  • If the door is shut after the notification, notify phone (can’t work this one out)

Can anyone advise, please?

Maybe use a wait template after the first notify to wait for the door to close then send your second notification:

- wait_template: "{{ is_state('binary_sensor.utility_room_door','closed') }}"
  continue_on_timeout: false
1 Like

It’d be really helpful for you to have included the (properly-formatted) YAML code for your automation, as I’m going to need to do a lot of typing and placeholders (in capitals) now.

Two automations: first is probably similar to what you have, second triggers on the door closing and checks if the last notification is more recent than the last change of door state. This is a template shorthand condition.

alias: "Notify on open"
trigger:
  - platform: state
    entity_id: binary_sensor.DOOR
    to: 'on'
    for: '00:05:00'
action:
  - service: notify.YOU
    data:
      title: "Door open!"
      message: "{{ now().isoformat() }}"

alias: "Notify on close after notification"
trigger:
  - platform: state
    entity_id: binary_sensor.DOOR
    to: 'off'
condition:
  - "{{ state_attr('automation.notify_on_open', 'last_triggered')|as_timestamp(0) > trigger.from_state.last_changed|as_timestamp(0) }}"
action:
  - service: notify.YOU
    data:
      title: "Door closed!"
      message: "{{ now().isoformat() }}"
1 Like

Apologies if I asked the question wrong or without enough info. I thought I might get replies just telling me what to select in the automation GUI not YAML files etc. Super new to this.

I will try either of these solutions and try them. Thank you. :pray:

Some things are beyond the capability of the UI selections — but you can paste YAML in, either to each section or as a whole (under three dots top right):

Once you’ve put in the correct DOOR entity_id and YOU notify services, you can switch back to UI mode. It’ll not render the action in UI mode when there is a template in the message, but you can always turn that into plain text.

It’s so much easier and less ambiguous to work with YAML in forums rather than trying to describe steps or concoct screenshots for entities and devices that don’t exist on our systems.

1 Like

This worked great thanks! I feel it’s going to be a long time before YAML is easy or natural for me.

1 Like