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