Automation Help with Time Conditions (Turn off lights for ##mintues between certain hours)

I have a Simple Automation that I’m trying to make it work.

- id: 'master_bedroom_lights_off'
  alias: Master Bedroom Lights Off
  trigger:
  - entity_id: group.all_master_bedroom
    for: 00:45:00
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - after: '22:30:00'
    before: '02:30:00'
    condition: time
  action:
  - data:
      entity_id: group.all_master_bedroom
    service: homeassistant.turn_off

basically I want all the master bedroom lights to turn off after 45 minutes between the hours of 10pm and 2:30am

Somehow Home Assistant ignores the time condition.

Thanks

The examples on the documentation page put the "for: " attribute value into single quotes. Try this.

Btw I forgot to mention that I made the automation using the automation tab in home assistant.

It generated the code above for me… I’m assuming that maybe it’s a bug in HA then?

I’ve modified the code below with no success:

- id: 'master_bedroom_lights_off'
  alias: Master Bedroom Lights Off
  trigger:
    entity_id: group.all_master_bedroom
    platform: state
    for: '00:45:00'
    from: 'off'
    to: 'on'
  condition:
    condition: time
    after: '02:30:00'
    before: '22:30:00'
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  action:
    - service: homeassistant.turn_off
      data:
        entity_id: group.all_master_bedroom

I think it’s just because the condition specifies after 2:30am, and before 10:30pm. That means it only evaluates to true in the day time, the exact opposite of what you want. Try this:

condition:
  - condition: or
    conditions:
      - condition: time
        before: '02:30:00'
      - condition: time
        after: '22:30:00'
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun

If the condition applies for every day of the week, it seems unnecessary to use the weekday: option and list all seven days.

The documentation for Time Condition indicates it can span across midnight. Therefore this should be valid (and very similar to the documentation’s example):

  condition:
    condition: time
    after: '22:30:00'
    before: '02:30:00'

Did you try your original version but with the 00:45:00 delimited by single-quotes?

- id: 'master_bedroom_lights_off'
  alias: Master Bedroom Lights Off
  trigger:
    entity_id: group.all_master_bedroom
    platform: state
    from: 'off'
    to: 'on'
    for: '00:45:00'
  condition:
    condition: time
    after: '22:30:00'
    before: '02:30:00'
  action:
    - service: homeassistant.turn_off
      data:
        entity_id: group.all_master_bedroom

I just experimented with the automation and discovered something interesting.

I set the time period to be from 20:30 to 21:30.
I set the duration (for:) to 10 minutes.
I turned the light on at 20:28.
The light failed to turn off at 20:38.

At 20:40 I manually turned the light off and then back on.
The light turned off at 20:50.

At 21:00 I turned the light on.
At 21:10 the light turned off.

At 21:25 I turned the light on.
It did not turn off at 21:35 (i.e. past the boundary of the time period).

It would appear that the automation expects to see a state-change from off to on during the time period before it begins counting the duration. If the light is already on before the time period begins, it won’t start counting the duration.

In light of this result, this automation, in its current form, is unlikely to work the way you want.

  1. If group.all_master_bedroom is already on before 22:30 then this automation will never turn it off.
  2. If group.all_master_bedroom is turned on at 02:00 it will not be turned off 45 minutes later, at 02:45, because that’s beyond the time period’s boundary (02:30).

I believe one solution might be to move the condition section into the action section. It doesn’t fix the second issue only the first one. However, it may do it a bit too aggressively. If the group is already on for 45+ minutes before 22:30, it will get immediately turned off at 22:30.

- id: 'master_bedroom_lights_off'
  alias: Master Bedroom Lights Off
  trigger:
    entity_id: group.all_master_bedroom
    platform: state
    from: 'off'
    to: 'on'
    for: '00:45:00'
  action:
    - condition:
        condition: time
        after: '22:30:00'
        before: '02:30:00'
    - service: homeassistant.turn_off
      data:
        entity_id: group.all_master_bedroom

Hi yeah I tried the ‘00:45:00’ before with no change in behavior.

Thank you for spending this much time running that experiment above.

Is there no other alternative to automate turning off lights after num of minutes between specific hours?

Try the condition as 2 conditions… one before 02:30 and the other after 22:30…

yeah that’s the latest I’m trying right now:

- id: 'master_bedroom_lights_off'
  alias: Master Bedroom Lights Off
  trigger:
    entity_id: group.all_master_bedroom
    platform: state
    for: '00:00:01'
    from: 'off'
    to: 'on'
  condition:
    - condition: or
      conditions:
        - condition: time
          before: '02:30:00'
        - condition: time
          after: '22:30:00'
  action:
    - service: homeassistant.turn_off
      data:
        entity_id: group.all_master_bedroom

I’ve set it to 1 second just so I can test it, but it is not turning off

  trigger:
    - platform: state
      entity_id: group.all_master_bedroom
      to: 'on'
      for: '00:00:01'

thanks for that suggestion, currently using this now:

- id: 'master_bedroom_lights_off'
  alias: Master Bedroom Lights Off
  trigger:
    - platform: state
      entity_id: group.all_master_bedroom
      to: 'on'
      for: '00:00:01'
  condition:
    - condition: or
      conditions:
        - condition: time
          before: '02:30:00'
        - condition: time
          after: '22:30:00'
  action:
    - service: homeassistant.turn_off
      data:
        entity_id: group.all_master_bedroom

reloaded automations, ran check config, no errors, but light is still not turning off after 1 second

So it’s not a light group… do the lights actually exist in that group?

yeah i have a button for group.all_master_bedroom that I can toggle on and off using homeassistant.toggle

it has 4 lifx, 3 tp link switch

all_master_bedroom:
  name: all master bedroom
  entities:
    - light.mab1_upstairs_master_bedroom
    - light.mab2_upstairs_master_bedroom
    - light.mab3_upstairs_master_bedroom
    - light.mab4_upstairs_master_bedroom
    - switch.pink_salt_lamps
    - switch.carousel_lamp
    - switch.diamond_lamp

So if you toggle the group it turns them all on or off?

that is correct

When the group is ON does the state for the group say On, on or True, true?

I’m not sure I get your question, I’m using apicture elements card that I built individual buttons for

but this works, I’ve tested it in services tab:

No if you go to dev tools and click on <> and type in the group name it will show the state of the group… when it is on and off… I’m just confirming you have the right state in your automation.

oh i see what you mean, yeah I went to the states tab and looked for my automation. It says on. I toggled it on and off… I’m running out of ideas might just pick back tomorrow :confused: