Understanding the "for" time based condition

When using something like

        entity_id: 'switch.garage_south'
        state: 'on'
        for:
          minutes: 30 

Does minutes: 30 mean exactly 30 minutes, or does it mean 30 minutes or more?

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.

What I’m trying to figure out is if this will work if the door was open for more than 30 minutes prior to the sun conditions being met.

Here’s the full rule:

- id: auto_close_garage_south_when_dark
  alias: Auto Close Garage South
  hide_entity: true
  # Trigger this every 3 minutes
  trigger:
    platform: time_pattern
    minutes: "/3"
  # Garage door must be open for 30 minutes 
  # AND
  # The sun must be set for 25 minutes OR before sunsrise
  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

When using it in a condition as you have, it’s 30 mins or more.

If it was part of a trigger, the trigger would happen when the time hit 30 mins

1 Like

Thanks, @sparkydave

I’ll be sure to test it out. I was preparing for the opposite answer, so I came up with a condition that should also work as well.

This will return True if the on state hasn’t changed for 1800 or more seconds ( 30 minutes )

  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: 'switch.garage_south'
        state: 'on'
      - condition: template
        value_template: "{{ as_timestamp(now()) - as_timestamp(states.switch.garage_south.last_changed) >= 1800 }}"
      - condition: or
        conditions:
          - condition: sun
            after: sunset
            after_offset: "+00:25:00"
          - condition: sun
            before: sunrise

Then why are you using a 3-minute Time Pattern Trigger?

Because it needs to be time based.

Which you can do by triggering on sunrise and sunset (with 25 minute offsets).

I don’t want to trigger just once per sunset/sunrise, I want to trigger anytime during the period of sunset to sunrise, ensuring that the door is closed if it’s open for 30 minutes. I also want to make sure that once sunset arrives, if the door has already been opened for 30 or more minutes to also close it.

Then you picked the wrong time-based trigger.

The triggers ought to be duplicates of the conditions. So a State Trigger (with for: ‘00:30:00’) and Sun Triggers for sunset and sunrise (with offsets).

The way I described it will do that.

What you’re describing would require multiple triggers and multiple conditions

eg:

  trigger:
    - platform: state
      entity_id: switch.garage_south
      state: 'on'
      for:
        minutes: 30
    - platform: sun
      event: sunset
      after_offset: "+00:25:00"

I would still need to include multiple conditions.

Please reply with more than “you’re wrong, I’m right” provide some proof to why you feel you’re right.

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”.

5 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.