Understanding the "for" time based condition

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