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
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.
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
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.
You figured it out while I preparing to share my test automation with you … so you get to see it anyway.
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