Turn off lights that has been on for hours

I am trying to set a automation to turn the lights that hass been on for more than 3 hours after 9pm until 8am.

This is because sometime the light in the living room stay on all night.

i have this but its not coded right

- alias: Auto Turn off Light at night
  initial_state: True
  trigger:
    platform: time_pattern
    hour: "/1"
  condition:
  - condition: time
    after: '21:00:00'
    before: '08:00:00'
  - condition: state
    entity_id: 'switch.luz_cuarto_entrada_nj'
    state: 'on'
    for:
      minutes: 30
  action:
    - service: homeassistant.turn_off
      entity_id: "switch.luz_cuarto_entrada_nj"

This code runs every hour and check if a switch has been on for 30 minutes, then turn it off.

The code works but the problem is that there are many light and switches to check, so i dont want to make an automation for every one of them.

Any ideas how to make this work?

  action:
    - service: homeassistant.turn_off
      entity_id: group.your_group_of_switches_and_lights

How to create a group:

The other option is to just list the entities in the automation:

  action:
    - service: homeassistant.turn_off
      entity_id: 
        - switch.luz_cuarto_entrada_nj
        - switch.another_switch
        - switch.another_switch_2
        - switch.another_switch_3
        - light.one_of_your_lights
        - etc...

This:

does not do what you said you wanted to:

Would you like the trigger to do what you originally wanted?

This would be it:

- alias: Auto Turn off Light at night
  initial_state: True
  trigger:
    platform: time_pattern
    hour: "/1"
  condition:
  - condition: time
    after: '21:00:00'
    before: '08:00:00'
  - condition: state
    entity_id: group.your_group_of_switches_and_lights
    state: 'on'
    for:
      hours: 3
  action:
    - service: homeassistant.turn_off
      entity_id: group.your_group_of_switches_and_lights

This could work as an alternate solution,
But, it only checks if the group has been on for more than 3 hours and then turns off all light in that group.

What i really wanted, is to turn off the light that has been on for more than 3 hours (not all light in the group)

Then list all the lights and switches you want in the trigger and use a template for the action, e.g:

- alias: Auto Turn off Light at night
  initial_state: True
  trigger:
    platform: state
    entity_id:
      - switch.luz_cuarto_entrada_nj  # any of these can trigger individually.
      - switch.another_switch
      - switch.another_switch_2
      - switch.another_switch_3
      - light.one_of_your_lights
    to: "on"
    for:
      hours: 3
  condition:
    condition: time
    after: '21:00:00'
    before: '08:00:00'
  action:
    service: homeassistant.turn_off
    data_template:
      entity_id: "{{ trigger.to_state.entity_id }}"

There is still an edge case that this will not catch. If a light has been on for 3 hours or more before 9pm it will not get turned off after 9pm. Adding a time_pattern trigger will stuff up the action template. So I’m still thinking about how to catch this.

3 Likes

Yes, you are right, that also happened to me. It stayed on all night because it was turned on before 9 so the automation was triggered but the condition didn’t matched.

I’m having trouble trying to create an automation to turn off lights at night based upon the previous post, but it isn’t working. Any recommendations?

- id: '0000000000000'
  alias: 'REMEDIATE Turn off Flood Lights after 1h at night'
  description: ''
  initial_state: true
  trigger:
  - entity_id:
    - switch.jasco_products_46201_in_wall_smart_switch_switch_3
    - switch.jasco_products_unknown_type_4952_id_3135_switch_19
    for:
      hours: 1
    platform: state
    to: 'on'
  condition:
  - after: sunset
    before: sunrise
    condition: sun
  action:
    data_template:
      entity_id: '{{trigger.to_state.entity_id}}'
    service: homeassistant.turn_off
  mode: single

I believe the problem is due to the condition. When using the sun condition, if you want to cover the time span between sunset and sunrise, you cannot combine them into a single sun condition.

This scenario is explained in the documentation and is due to midnight acting as a boundary. The documentation concludes with:

… to cover time between sunset and sunrise one need to use after: sunset and before: sunrise as 2 separate conditions and combine them using or

- id: '0000000000000'
  alias: 'REMEDIATE Turn off Flood Lights after 1h at night'
  description: ''
  initial_state: true
  trigger:
  - entity_id:
    - switch.jasco_products_46201_in_wall_smart_switch_switch_3
    - switch.jasco_products_unknown_type_4952_id_3135_switch_19
    for:
      hours: 1
    platform: state
    to: 'on'
  condition:
    condition: or
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
  action:
    data_template:
      entity_id: '{{trigger.to_state.entity_id}}'
    service: homeassistant.turn_off
  mode: single
4 Likes

Thanks that was it. Still learning.