Understanding the "for" time based condition

I’ve been helping Home Assistant users for over a year (feel free to check my profile) and it has been my experience, more often than not, that automations that use the Time Pattern Trigger have done so in error.

Here’s a simple example of what I mean. Imagine you wish to turn on a light at 08:00. The automation uses a time_pattern trigger, with a 1-minute interval, and a condition that tests if the current time is 08:00. It would work but it’s not the most efficient way to have Home Assistant turn on a light at a desired time. There’s a Time Trigger available for that purpose.

Optimally, you want Home Assistant to operate in an event-based manner as opposed to polling-based. In other words, let the desired events, whenever they occur, trigger the automation as opposed to having it repeatedly poll to check if they occur. Home Assistant’s architecture is well designed to handle event-based triggering.

Your automation, with a time_pattern trigger, fires on a regular basis regardless of the time of day or if the garage door is actually open. Effectively, Home Assistant evaluates this automation needlessly far more often than when the actual events occur (i.e. when the door is open for 30+ minutes during non-daylight hours).

The arrangement I recommended will allow the automation to trigger only when the desired events occur. Worst case, it will trigger during the day (if the door is open more than 30 minutes) but its condition (is it night time?) will prevent execution of the action. That’s far fewer needless triggers than a 3-minute time_pattern.

To be clear, there are good use-cases for the time_pattern trigger but this is not one of them.


Now it’s your turn to “provide some proof to why you feel you’re right”.

4 Likes

Agree w/ @123 on this. The trigger should really be WHEN DOOR OPEN FOR 30 and then use the conditions to nullify the close door action.

TRIGGER: Door Open FOR 30 minutes
CONDITION: Time < SUNRISE - 25 OR TIME > SUNSET + 25
ACTION: Close Door

1 Like

If I open the garage door at 2pm and sunset is it at 6pm, it won’t trigger because the on state is not exactly 30 minutes when sunset arrives.

If a double trigger is used, it might work. I’ll try this:

- id: auto_close_garage_south
  alias: Auto Close Garage South
  hide_entity: true
  trigger:
    - platform: state
      entity_id: switch.garage_south
      to: 'on'
      for:
        minutes: 30
    - platform: sun
      event: sunset
      offset: "+00:25:00"
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: switch.garage_south
        state: 'on'
        for:
          minutes: 30
      - condition: or
        conditions:
          - condition: sun
            after: sunset
            after_offset: "+00:25:00"
          - condition: sun
            before: sunrise
  # Close the south door
  action:
    service: homeassistant.turn_off
    entity_id: switch.garage_south
1 Like

Please note that automatically closing an unattended garage door is not without peril.

  • If you check your garage-door closer’s manual, somewhere it will state that the door should not be operated when unattended. So be aware that you will be using it outside its operational and safety ‘envelope’.

  • They have resistance sensors to prevent crushing obstructions. Be sure yours is calibrated properly (and recently).

  • Confirm the photo-electric obstruction detector is working correctly.

  • Property damage may still be possible. Any object taller than the obstruction sensor will go undetected and the door will contact it. For example, a vehicle’s open rear-hatch may be struck by the door itself or its handle.

  • Anyone in the garage at the time is likely to be startled by the door’s activity. In our home, I added a speaker in the garage and play a warning announcement prior to the door’s automatic operation (“Attention! Door will close in ten seconds”).

  • You may wish to add a failure notification. If the door attempts automatic closure but fails, it sends you a notification.

FWIW, for our home, we chose not to use a timed automatic closure but simply a notification that the door was left open. Basically, an automation checks how many exterior doors (front, garage, patio, yard, shed, etc) were left open and then reports them, house-wide, via the PA system.

2 Likes

Noted.

I’ve been doing auto-close on my doors for years via my own home rolled shell scripts. Everything is in working order, I’m just migrating the automation to HA.

I think you’re mis-understanding automations and how they work.

Might be worth re-reading this section: https://www.home-assistant.io/docs/automation/

Key is the TRIGGER, CONDITION and ACTION
image

TRIGGER: When this happens (ie. door has been open for 30 minutes)
CONDITION: Only proceed if this is evaluates to TRUE (ie. before sunrise or after sunset)
ACTION: DO this (ie. close door)

I’m pretty sure I understand how they work. I’m not going to repeat myself because the triggers and conditions I need to evaluate have already been explained, yet you seem to be skipping over them.

My last post where I show the rule having multiple triggers should work.

I’m pretty sure that you’re still unsure how they work and what the difference between a TRIGGER, CONDITION and ACTION really are.

Personally, and again, I suggest you go re-read the link I posted. Teach a man to fish vs. give him a fish. These are open forums, help is freely given, and it’s ALWAYS easier to get help once you’ve fully googled/searched the forums for your own solution.

Regardless, try this:

- id: auto_close_garage_south_when_dark
  alias: Auto Close Garage South
  hide_entity: true

  # Trigger AFTER door's been open for 30 minutes
  trigger:
  - entity_id: switch.garage_south
    for: '00:30:00'
    platform: state
    to: 'on'

  # The sun must be set for 25 minutes OR before sunsrise
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '00:25:00'
      - condition: sun
        before: sunrise

  # Close the south door
  action:
    service: switch.turn_off
    entity_id: switch.garage_south

You may find the following post to be of some use. I carried out an experiment to determine how the automation behaves if the time period handled by for: happens to span the time boundaries in the condition. In other words, in your example, what would happen if your garage door were opened 15 minutes before sunrise? Would the automation close the door 15 minutes later after sunrise?

Based on @123’s post above, you also then might want to change your before / after sunrise / sunset timeframe’s to ensure it’s executing how/when you want.

Ie. If door is opened 29 minutes before sunrise, the automation above will NOT close the door - b/c the 30 minutes FOR statement will ‘delay’ the condition test until AFTER sunrise - at which point the condition will evaluate to FALSE.

If the door is open for 15 minutes before sunrise, then it’s allowed to remain open until 25 minutes after sunset, at which point if it’s open for 30 minutes or more, it will then be closed.

I have an alert setup to fire if the door has been open for 60 minutes.

Yes, I’m aware of that. The main task is to close the door at night. If I want to auto close it during the day, I will add a condition to check the nest_away status.

Maybe we need to go all the way back to the beginning. Please clearly write out / articulate exactly what you want to happen when. The misunderstanding seems to be stemming from moving goal-posts at this point.

It seems like you have multiple reasons / times you want to evaluate / close the door - so best to clearly state your use-case / desires.

I did, in the first post.

Since I’m a new user and can’t reply more than 12 times in a day, I’ll paste my last reply here:

And it wouldn’t work if the door was open for more than 30 minutes prior to sunset + 25mins

Which is why I need 2 triggers.

Trigger 1 = if the door is open for 30 minutes - This will be the trigger between sunset and sunrise

Trigger 2 = sunset+25 - This will be triggered if door has been open for more than 30 minutes prior to sunset+25 minutes = true

My point was that the condition’s time boundaries are ‘fuzzy’ when the trigger employs a for with a substantial time period (like 30 minutes). Although my example used the sunrise boundary, the same effect will occur around the sunset+offset boundary. The door can be left open for 99% of the 30 minute allowance and then fail to be closed (given the boundary conditions I described).

The window of opportunity for this to happen is the time period used by for: so that’s 30 minutes in your case. I brought it to your attention only to make you aware of this behavior’s existence. You may decide (as I believe you already have) that it is non-critical for your application. For other applications, it may require mitigation.

TBH, you’re going to have to help yourself at some point.

You’ve started by saying:

I’m trying to create an automation that closes the garage door if it’s been open for at least 30 minutes and it’s 25 minutes after sunset or before sunrise.

My code snippet above does exactly this ^^

You then said this:

If the door is open for 15 minutes before sunrise, then it’s allowed to remain open until 25 minutes after sunset, at which point if it’s open for 30 minutes or more, it will then be closed.

This shows a slight misunderstanding of how the FOR condition is evaluated. Check this link: Automation Trigger - Home Assistant - For only evaluates ONE time.

Sounds, from the best I can translate, that you need TWO different automations:

  1. As I posted above, triggers when door’s been open for 30 minutes, between sunrise / sunset +/- whatever offset you want
  2. Trigger sunset + 25 minutes to close the door if it’s open (best to also have a homeassistant.turn_off for automation #1 to avoid overlapping

Anyhow, that’s the best I can do. There’s an unfortunate lack of clarity around EXACTLY what you’re trying to accomplish. At this point you should have all the info you now need above and in the links provided to suss this our on your own. Hopefully my time’s been spent helping you at least a bit.

I have everything working in a single automation using multiple triggers. When I first posted, I wasn’t aware that you could have multiple triggers, which is why I had the trigger set as a time pattern.

So @123 you were correct in that I wasn’t using the correct trigger.

The door can be open for more than 30 minutes prior to sunset +25 minutes and it will close once sunset +25 arrives, or it can be open for exactly 30 minutes between sunset+25 and sunrise and it will close.

Yes, I’m aware that if the door is opened 29 minutes before sunrise that it will remain open.

# Trigger on any of the following:
# - door open for 30 minutes
# - sunset +25 minutes
# Conditions:
# - door open for at least 30 minutes AND
#   any of the following:
#   - sunset +25 minutes
#   - before sunrise
- id: auto_close_garage_south
  alias: Auto Close Garage South
  hide_entity: true
  trigger:
    - platform: state
      entity_id: switch.garage_south
      to: "on"
      for:
        minutes: 30
    - platform: sun
      event: sunset
      offset: "+00:25:00"
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: switch.garage_south
        state: "on"
        for:
          minutes: 30
      - condition: or
        conditions:
          - condition: sun
            after: sunset
            after_offset: "+00:25:00"
          - condition: sun
            before: sunrise
  action:
    service: homeassistant.turn_off
    entity_id: switch.garage_south

@Markus99 your rule once again will not work if the door has been open for more than 30 minutes prior to the conditions. You kept stating I wasn’t clear in what I was looking to do, but I spelled it out in the very first post.

Thanks again to @sparkydave for quickly answering my initial question and to @123 for prodding me to find the right trigger.

1 Like

Whatever man, glad I wasted 30 minutes of my life trying to help you out this morning. Your overflowing appreciation for the free advice has been noted. Unbelievable.

3 Likes

And for completeness, here’s the working rule that includes a 3rd trigger for the nest away mode and the extra condition check.

# Trigger on any of the following:
# - door open for 30 minutes
# - sunset +25 minutes
# - nest away mode = on
# Conditions:
# - door open for at least 30 minutes AND
#   any of the following:
#   - sunset +25 minutes
#   - before sunrise
#   - nest away mode = on
- id: auto_close_garage_south
  alias: Auto Close Garage South
  hide_entity: true
  trigger:
    - platform: state
      entity_id: switch.garage_south
      to: "on"
      for:
        minutes: 30
    - platform: state
      entity_id: binary_sensor.home_away
      to: "on"
    - platform: sun
      event: sunset
      offset: "+00:25:00"
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: switch.garage_south
        state: "on"
        for:
          minutes: 30
      - condition: or
        conditions:
          - condition: sun
            after: sunset
            after_offset: "+00:25:00"
          - condition: sun
            before: sunrise
          - condition: state
            entity_id: binary_sensor.home_away
            state: "on"
  action:
    service: homeassistant.turn_off
    entity_id: switch.garage_south
1 Like

Glad to hear you successfully converted the automation to use multiple triggers.

I find it interesting that many Home Assistant users are aware of the opposite. They know an automation can have multiple triggers (it’s documented) but don’t know how a trigger using for: behaves when it spans a boundary condition (it’s not documented).

1 Like