Time based automation to also start acting on devices set to On before?

I’ve been running an automation that turns off my lights after 15 mins between 00:00 and 07:00.
But obviously if a light is already On when it gets to 00:00 it doesn’t start tracking/running on this light.

Is there some way to make an automation run even on devices/entities that are already in the state the automation expects them to switch to?
Or maybe there’s a blueprint around for it, but I haven’t found it. Not sure what to search for tbh.

Current automation:

alias: Woonkamer Achter - Light Off after Midnight
description: Turn off lights after 15 mins between 00:00 and 07:00
trigger:
  - platform: device
    type: turned_on
    device_id: c68db30a55480377421bbfcace12bfa4
    entity_id: light.hue_white_filament_bulb_g93_e27_woonkamer_achter
    domain: light
    for:
      hours: 0
      minutes: 15
      seconds: 0
condition:
  - condition: time
    after: '00:00'
    before: '07:00'
action:
  - service: light.turn_off
    data:
      transition: 2
    target:
      device_id: c68db30a55480377421bbfcace12bfa4
mode: restart

That’s not true.

e.g. If the light gets turned on at 23:50, then at 00:05 the automation triggers and then the time condition is checked and it passes.

Ah, but then what if the light has been on since 23:00?
Currently it doesn’t turn off with the above automation.
But I do want it to be affected by the 15 min rule from the midnight point.

alias: Woonkamer Achter - Light Off after Midnight
description: Turn off lights after 15 mins between 00:00 and 07:00
trigger:
  - platform: state
    target:
      entity_id: light.hue_white_filament_bulb_g93_e27_woonkamer_achter
    from: 'off'
    to: 'on'
    for:
      minutes: 15
  - platform: time
    at: '00:00'
condition:
  - condition: time
    after: '00:00'
    before: '07:00'
  - condition: template
    value_template: >
      {{ trigger.platform == 'state' or 
        (is_state('light.hue_white_filament_bulb_g93_e27_woonkamer_achter', 'on') and
            (now() - states.light.hue_white_filament_bulb_g93_e27_woonkamer_achter.last_changed).total_seconds() > 900) }}
action:
  - service: light.turn_off
    data:
      transition: 2
    target:
      entity_id: light.hue_white_filament_bulb_g93_e27_woonkamer_achter
mode: restart

It triggers at 00:00 and the Template Condition checks if the light has already been on for at least 15 minutes (900 seconds). If it is then it’s turned off immediately.

Just add a time trigger at 00:00, then

Couldn’t you make this simpler by making the trigger do most of the work?

alias: Woonkamer Achter - Light Off after Midnight
description: Turn off lights after 15 mins between 00:00 and 07:00
trigger:
  - platform: template
    value_template: >-
      {{ is_state('light.hue_white_filament_bulb_g93_e27_woonkamer_achter', 'on') and 
        ( utcnow()| as_timestamp - 
        states.light.hue_white_filament_bulb_g93_e27_woonkamer_achter.last_changed | as_timestamp > 900 ) }}
condition:
  - condition: time
    after: '00:00'
    before: '07:00'
action:
  - service: switch.turn_off
    target:
      entity_id: light.hue_white_filament_bulb_g93_e27_woonkamer_achter
mode: single 

I’d say it’s a matter of preference regarding how the automation’s triggers are handled.

In the example I posted, the triggers are evaluated when one of two discrete events occurs:

  • At a specific time.
  • When the light is on for 15 minutes.

The condition is evaluated only when either of those two events occurs.

In your example, the inclusion of utcnow() mandates an evaluation of the Template Trigger every minute.

FWIW, wherever possible, I prefer to have the efficient, backend python code handle timed events.

1 Like

Thanks

Good to know.

FWIW, if the goal is to make the Template Trigger do all of the work, you can make it check the time-range, thereby eliminating the Time Condition.

alias: Woonkamer Achter - Light Off after Midnight
description: Turn off lights after 15 mins between 00:00 and 07:00
trigger:
  - platform: template
    value_template: >-
      {% set light = 'light.hue_white_filament_bulb_g93_e27_woonkamer_achter' %}
      {{ is_state(light, 'on') and 0 <= now().hour < 7 and  
        (now() - states[light].last_changed).total_seconds() > 900 }}
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: light.hue_white_filament_bulb_g93_e27_woonkamer_achter
mode: single 

EDIT

This might works as well but I haven’t had the time to test it (I rarely use trigger_variables).

alias: Woonkamer Achter - Light Off after Midnight
description: Turn off lights after 15 mins between 00:00 and 07:00
trigger_variables:
  light: 'light.hue_white_filament_bulb_g93_e27_woonkamer_achter'
trigger:
  - platform: template
    value_template: >-
      {{ is_state(light, 'on') and 0 <= now().hour < 7 and  
        (now() - states[light].last_changed).total_seconds() > 900 }}
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: '{{ light }}'
mode: single 

Thanks. I’ll be using a slightly altered version of the first post from Taras.
But this way it works just fine for my purpose.

I tried both ways, but I prefer it not running every minute.
And since either work I’ll take the Condition template way.

Here’s my resulting automation. It has some obsolete values, but that’s just because I prefer to edit via the GUI.

alias: Woonkamer Achter - Light Off after Midnight
description: Turn off lights after 15 mins between 00:00 and 07:00
trigger:
  - platform: state
    entity_id: light.hue_white_filament_bulb_g93_e27_woonkamer_achter
    to: 'on'
    for:
      hours: 0
      minutes: 15
      seconds: 0
  - platform: time
    at: '00:00:00'
condition:
  - condition: time
    after: '00:00'
    before: '07:00'
  - condition: template
    value_template: |-
      {{ trigger.platform == 'state' or 
        (is_state('light.hue_white_filament_bulb_g93_e27_woonkamer_achter', 'on') and
            (now() - states.light.hue_white_filament_bulb_g93_e27_woonkamer_achter.last_changed).total_seconds() > 900) }}
action:
  - service: light.turn_off
    data:
      transition: 2
    target:
      device_id: c68db30a55480377421bbfcace12bfa4
mode: single