Automation testing condition with delay with respect to trigger

Hi,

use case for this is:

  • I have a door sensor at the entrance
  • I want this to fire a notification if door opened and no once at home

presence state is usually triggered few seconds (10-ish) after the door open, so every time I enter my house automation fires.
I would prefer to avoid adding a “for” statement to the trigger as this could prevent the automation to trigger when needed.

Is there a way to trigger the automation but test the condition after a certain amount of time?

Post the automation you are currently using.

Hi,

thanks for your answer.
This is the automation I am using

  alias: Allarme apertura porte
  initial_state: true
  id: "1912291115001"
  trigger:
    - entity_id: binary_sensor.porta_vetri_entrata
      for:
        hours: 0
        minutes: 0
        seconds: 5
      from: "off"
      platform: state
      to: "on"
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.someone_at_home
        state: "off"
      - condition: state
        entity_id: input_boolean.security_alarm_status
        state: "on"
      - condition: state
        entity_id: binary_sensor.xxx_at_home
        state: "off"
      - condition: state
        entity_id: binary_sensor.yyy_at_home
        state: "off"

You can try this:

Move the State Conditions that check if anyone is home to a wait_template in the automation’s action. Set its timeout value to slightly more than 10 seconds.

For example, if you set it to 12 the wait_template will wait for up to 12 seconds for its State Conditions to become true. If they don’t become true within 12 seconds, what the automation does next is your choice. Normally the automation will not continue because continue_on_timeout is set to false by default.

So if I correctly understand

WAIT TIMEOUT

With both types of waits it is possible to set a timeout after which the script will continue its execution if the condition/event is not satisfied. Timeout has the same syntax as delay, and like delay, also accepts templates.

this should become like this:

  alias: Allarme apertura porte
  initial_state: true
  id: "1912291115001"
  trigger:
    - entity_id: binary_sensor.porta_vetri_entrata
      for:
        hours: 0
        minutes: 0
        seconds: 5
      from: "off"
      platform: state
      to: "on"
- action:
    - wait_template: "{{ (is_state('binary_sensor.person1_at_home', 'on'))or(is_state('binary_sensor.person2_at_home_at_home', 'on')) }}"
      timeout: "00:00:15"
      continue_on_timeout: false
    - service: notify.mobile_app_myphone
      data:
        title: Allarme Sicurezza!
        message: Alarm
        data:
          push:
            sound:
              name: default
              critical: 1
              volume: 0.35

is that correct?

Maybe; this version doesn’t contain the first two State Conditions present in the original example. Do you now want to exclude them from the automation?

Ah yes, didn’t add all of them as it was just an example.

I will try, thanks for this

Here’s how your example will work with the wait_template.

When Porta Ventri Entrata is opened and left open for at least 5 seconds, the wait_template will begin to check for either person1 or person2 to be home. If neither is home, it will wait for up to 15 seconds for one of them to be home. If it happens within 15 seconds, the Automation sends a notification. If it doesn’t happen within 15 seconds, the automation ends (no notification is sent).

I think that behaves the way you wanted and demonstrates how a wait_template can be used to wait for something to happen.

Definetly what I needed: tomorrow I will update my automations!

so apparently I marked this as solved too early :frowning:
this is my automation now

- action:
    - wait_template: "{{ (is_state('binary_person1_at_home', 'off'))and(is_state('binary_sensor.person2_at_home', 'off'))and(is_state('binary_sensor.persone3_at_home', 'off')) }}"
      timeout: "00:00:35"
    - service: notify.mobile_app_iphone_xxx
      data:
        title: Alert
        message: Alert
        data:
          push:
            sound:
              name: default
              critical: 1
              volume: 0.35
  alias: Alarm
  initial_state: true
  trigger:
    - entity_id: binary_sensor.door
      for:
        hours: 0
        minutes: 0
        seconds: 5
      from: "off"
      platform: state
      to: "on"
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.someone_at_home
        state: "off"
      - condition: state
        entity_id: input_boolean.security_alarm_status
        state: "on"
      - condition: state
        entity_id: binary_sensor.elisa_at_home
        state: "off"
      - condition: state
        entity_id: binary_sensor.michele_at_home
        state: "off"

now issue is that sometimes the “person at home binary sensor” triggers to ON after the door is opened.
So I would like the wait template to check the condition for 35s and carry on only if the condition remains TRUE for 35s.
This is not happening instead and I can’t understand which syntax I need to use based on this

Can someone help?

    - wait_template: "{{ (is_state('binary_person1_at_home', 'off'))and(is_state('binary_sensor.person2_at_home', 'off'))and(is_state('binary_sensor.persone3_at_home', 'off')) }}"
                                    ~~~~~~~~~~~~~~~~~~~~~~
                                              ^
                                              |
                                        What is this?
                              It's not an entity_id or a variable

Are you saying that it never worked since a month ago? Or it only recently failed to work after you added all of the conditions that weren’t present in your original example?

so that binary sensor is a bayesian which I use to build a more robust “at home” sensor.

It never worked I am afraid: I was convinced it did and marked “solved” to early.
Honestly I am afraid wait_template can’t do what I supposed it could do.

As far as I understand it waits for the template to render true until the timeout expires and then continue the automation.
If it does not render true you can decide to continue or not after the timeout.

What I need instead is a sort of delayed condition such that it checks for 35s that the condition is satisfied, if not satisfied it stops executing the automation

If it’s a binary_sensor then you didn’t provide its entity_id correctly. Look closely at its spelling.

'binary_person1_at_home'

It should be:

'binary_sensor.person1_at_home'

If the wait_template evaluates to true before the timeout expires, it continues to the next action.

If the wait_template’s timeout expires, before it evaluates to true, it continues to the next action only if continue_on_timeout is set to true.

continue_on_timeout: false

sorry I edited the automation in order to post it here and remove personal data and doing this misspelled the binary sensor: I can confirm that in my automation that is spelled correctly

that is my understanding, but I need something that:

  • if the wait template is evaluate to true during the timeout duration stops the automation → I can’t see how to achieve this
  • if the timeout expires the automation continue → continue_on_timeout = true makes the job

I have a similar automation and had the same problem, the device_tracker state takes a few seconds after entering the appartment.
I solved with a simple delay and choose:

  alias: Allarme apertura porte
  initial_state: true
  id: "1912291115001"
  trigger:
    - entity_id: binary_sensor.porta_vetri_entrata
      from: "off"
      platform: state
      to: "on"
  action:
    - delay:
        seconds: 10
    - choose: 
      - conditions:
        - condition: state
          entity_id: binary_sensor.someone_at_home
          state: "off"
        - condition: state
          entity_id: input_boolean.security_alarm_status
          state: "on"
        - condition: state
          entity_id: binary_sensor.xxx_at_home
          state: "off"
        - condition: state
          entity_id: binary_sensor.yyy_at_home
          state: "off"
        sequence:
        ...

It will trigger when the door opens, waits the delay and then checks if the conditions are met. If yes it will continue (you have to add your actions under sequence), if not it will stop and do nothing.

You can use an if-then-else to test the wait.completed variable after the wait_template. If it’s true (meaning the wait_template evaluated to true during its timeout) you can stop the automation with stop. Otherwise, the else in the if-then-else proceeds to execute the remaining actions.

I am not very familiar with this, could you please provide an example?

Refer to the example in the link I provided for wait.completed.