Adding Delay between two conditions

If I want to create an automation according to this pseudocode, how do I configure it to add delay between the 2 conditions?

if garage opened and garage empty
… 3 minute delay
…if garage closed and now garage not empty
…send message

Thanks,
Rich

Hello Rich F,

I’m sure that can be done. Also having someone or something writing it for you means the second it misbehaves, you will be struggling back to find someone to fix it for you.
The best way to learn is to just try. Run thru the docs on getting started with the automation editor. If you get stuck, come back with what you have done.

and
The Home Assistant Cookbook - Index.

Thanks for your reply.

The way the Create automation GUI orders its functions are TRIGGERs, CONDITIONs, then ACTIONs.

I believe I need this order

TRIGGER - if door opened
CONDITION - garage is empty
ACTION - perform a 3 minute delay
CONDITION - if garage no loger empty
ACTION - then send message

My question is how to go from CONDITION to ACTION then back to CONDITION again?
Normally, if it was a procedural computer language like BASIC, it would be 2 nested IFs with the delay between the 2 IFs.

Or is my pseudo code wrong where I should think of it in a different way?

Thanks,
Rich

The GUI can probably not handle that cause, but YAML and Jinja can.
If you know basic, then YAML should not be that hard and Jinja is only a little step up from there.
Alternatively you can use Node Red, if you want GUI with more advanced automations.

You can use if statements in ACTIONS using the GUI

TRIGGER - if door opened
CONDITION - garage is empty
ACTION - perform a 3 minute delay
ACTION - if garage no longer empty
then send message

Did not know that. I will give it a try.

Thank you,
Rich

Alternatively, you could also use a condition as a build block in the actions section. If the condition is false, the automation will quit.

This is my 1st attempt but would rather remove delay and instead have it do this instead since 3 minutes may not be enough time. However, I have no clue as to how to code it.

“when door closes and car in garage send message”

alias: Car Arrival
description: ""
triggers:
  - trigger: state
    entity_id:
      - automation.right_garage_door_opened
conditions:
  - type: is_not_present
    condition: device
    device_id: cf404a8dc7a81c5aed9028a3e4ea7a84
    entity_id: 60cc785b9f803d8ead619a88ab5ba996
    domain: binary_sensor
actions:
  - action: timer.start
    metadata: {}
    target:
      entity_id: timer.cardelaycountdown
    data:
      duration: "000300"
  - if:
      - condition: state
        entity_id: automation.right_garage_door_closed
        state: []
      - condition: state
        entity_id: automation.right_garage_car_entered
        state: []
    then:
      - action: notify.alexa_media_everywhere
        metadata: {}
        data:
          message: Car has arrived in first garage
mode: single

you could try to add the building block wait for trigger after your 3 minut delay, or instead of it

Home Assistant is … “quirky” meaning that there are lot of things that you just have to run into before you understand them - sometimes the documentation helps sometimes it doesn’t.

Let me put together something that is “roughly right” to explain a few concepts:

mode: single
alias: Car Arrival
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.right_garage_door_is_open
    to:
      - "on"
conditions:
  - condition: state
    entity_id: binary_sensor.right_garage_car_present
    state:
      - "off"
actions:
  - wait_for_trigger:
      - trigger: state
        entity_id: binary_sensor.right_garage_door_closed
        to:
          - "on"
    timeout:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
    continue_on_timeout: false
  - condition: state
    entity_id: binary_sensor.right_garage_car_present
    state:
      - "on"
  - action: notify.alexa_media_everywhere
    data:
      message: Car has arrived in first garage

A significant chunk of inputs into home assistant are binary sensors, where binary means they only have two states: on/off detected/clear bright/dim

Most door sensors are also binary: open/closed however motorized doors (such as garage doors) may have two sensors:

  • One sensor detects if the door is FULLY open.
  • One sensor detects if the door is FULLY closed.

Hence while the door is moving the door is neither FULLY open or FULLY closed.

Even though sensors show different words in the UI typically in scripts they are just on or off, hence:

  • Presence sensors are typically on when they detect someone/something
  • Normal (unmotorized) doors are on when they are open.
  • Light sensors are on when they detect light (shows up as “bright” in the UI).

So lets work through the example:

Trigger

triggers:
  - trigger: state
    entity_id:
      - binary_sensor.right_garage_door_is_open
    to:
      - "on"

We want the automation to start running when the door is opened.
Typically this will be when the state changes from off to on however there are some corner cases if the sensor gets disconnected so we will just detect all changes to on.

Conditions

conditions:
  - condition: state
    entity_id: binary_sensor.right_garage_car_present
    state:
      - "off"

We still want to check if the car is present - someone might just open the door and close it again - we only want to be notified if the car wasn’t there and then arrives, so we need to make sure the car isn’t there at the start of the automation.

Actions

We don’t want to actually do anything until the door is closed so we will wait for that to happen:

  - wait_for_trigger:
      - trigger: state
        entity_id: binary_sensor.right_garage_door_closed
        to:
          - "on"
    timeout:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
    continue_on_timeout: false

Here I have assumed your garage door has two sensors so the “closed” sensor switches to on when the door is closed - you will have to adapt if your door only has one sensor or uses different on/off values.

I have set a 3 minute timeout and set the script not to continue if the door isn’t shut in 3 minutes - again I don’t know what is going on, but I assume you don’t want a notification.


Next is one of the home assistant quicks, you can just list conditions without an if, what that means is “if the following condition is false just stop the automation”

So we are only going to continue the automation if the car is present - remember we won’t check this until the previous trigger happens (when the door is closed).

  - condition: state
    entity_id: binary_sensor.right_garage_car_present
    state:
      - "on"

Finally if we get to the bottom of the script we can send the notification.

  - action: notify.alexa_media_everywhere
    data:
      message: Car has arrived in first garage

The main concept we are using here is that there is only one sequence of events that results in a notification being sent - hence we just abort the automation if something deviates from the expected sequence of events.

If you have two (or more) possible paths (sequences of events) then you need to use “if” or “choose” statements to cover those cases.

### Rich F [email protected] 10:18 AM (8 minutes ago)
to David

|

David,

Thanks for the input. Looking at your sample automation allows me to use parts and experiment. What I don’t like is that HA hides the various options in groups making it hard to know what is available. I wish the various items available for selection were not in groups but just alphabetized.

Thanks again,

Rich

TBF Over 75% of my automation triggers are “Entity State” triggers I occasionally need something else, but state changes are the bread and butter of automation triggers.

For conditions about 90% of them are one of three types:

  • Entity State - Again I just need to know if something is on or off.
  • Trigger ID - You can assign ID’s when you have multiple trigger statements.
  • Templates - Home Assistant includes the Jinja templating language it can be easier to string more complex logical statements together using that,

The menu that probably needs some cleanup is:
Settings → Devices & Services → Helpers → Create Helper

It’s worth knowing what most of the helpers do although you will probably find that most of the time you use a limited number of them, probably:

  • Binary sensor groups.
  • Light Groups.
  • Template Helpers
  • Generic Thermostat should also get a mention you will probably only use it once (although you may create one for each room) however it saves a lot of cursing trying to automate temperature control.