Condition to execute an automation

Hi, I have a window sensor and I create this automation:

alias: Notify Bedroom Window Open
description: ''
trigger:
  - type: opened
    platform: device
    device_id: 3a7fd5a4045d93a27b5b635f079c70a3
    entity_id: binary_sensor.bedroom_window_sensor
    domain: binary_sensor
condition:
  - condition: numeric_state
    entity_id: sensor.ha_uptime
    above: '1'
action:
  - service: telegram_bot.send_message
    data:
      message: Bedroom window is open!
mode: single

But I want to get notification only if the window was closed previously for at least 5 seconds or was not opened in the last 5 seconds.
Thank you

Try using the state trigger instead of device trigger and then you can trigger based on state going from off to on (closed to open)

Unless someone is opening, closing and opening again in quick succession then you should not need anything else. It will also prevent the automation running from home assistant starting up or any other state change other than from off to on

1 Like

rossk is correct - state trigger is the better option here.
See: Automation Trigger - Home Assistant

Additionally I would recommend the queued mode instead of single.
This means multiple notifications if somebody open, close, open, close… but it will ensure you do not get mixed up / loose some actions.

I changed to state and it’s the same thing for now but how can I add the required condition?

Can I do this with a template?

This uses a State Trigger to detect when the binary_sensor changes state from off to on. It also uses a Template Condition to check if the the last time the binary_sensor changed state is more than 5 seconds ago.

alias: Notify Bedroom Window Open
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.bedroom_window_sensor
    from: 'off'
    to: 'on'
condition:
  - condition: numeric_state
    entity_id: sensor.ha_uptime
    above: '1'
  - condition: template
    value_template: "{{ now() - trigger.from_state.last_changed > timedelta(seconds=5) }}"
action:
  - service: telegram_bot.send_message
    data:
      message: Bedroom window is open!
mode: single
1 Like

Thank you so so much, works great and it’s just a line of code :))

1 Like

I’m not sure how that will work…?

If the same binary sensor is used in the trigger and the condition how can the trigger occur and at the same instant expect the trigger to have changed state over 5 seconds ago.

Am I missing something?

EDIT:

according to the OP it works so I really must be missing something.

EDIT 2:

NVM, it was the from_state… duh… :man_facepalming:

You figured it out while I preparing to share my test automation with you … so you get to see it anyway. :slight_smile:

Switch the input_boolean to on then off then on rapidly and it will only post a notification for the first off to on state-change. It needs at least 5 seconds of “dead” time (meaning the previous state needs to be off for at least 5 seconds) before it allows for the notification.

## Report only if last `off` state
## was more than 5 seconds ago.
## 
- alias: 'Test 999'
  trigger:
    - platform: state
      entity_id: input_boolean.test
      from: 'off'
      to: 'on'
  condition:
    - condition: template
      value_template: "{{ now() - trigger.from_state.last_changed > timedelta(seconds=5) }}"
  action:
    - service: notify.persistent_notification
      data:
        title: Test 999
        message: '{{ now().timestamp()|timestamp_custom()}}'
  mode: single
2 Likes