How to use timer to turn off light? Seems too simple but having trouble

Running the latest HA.

I’d like to use a timer to turn off the light but its not working. A delay works perfectly fine. It seems that simply having the timer present prevent the light from turning on at all. The timer itself seems to be working perfectly fine and turns off the light as expected.

Any tips as to what I might be overlooking or how I might troubleshoot?

Here’s my yaml -

alias: Garage light
description: ""
triggers:
  - device_id: ad9b0a8204387f5a7266a0517bd6f26b
    domain: cover
    entity_id: 9ea8c25e9994d702bd4da9897dca2702
    type: opened
    trigger: device
  - type: opened
    device_id: 47323f58bf65857d02a9cc8c5cf492be
    entity_id: a00ef9f8a28d29069713b06689356fec
    domain: binary_sensor
    trigger: device
  - device_id: f8151e650b322c01329dba054ef6a95e
    domain: lock
    entity_id: 6d93303052f543765276bf7f298b3d2f
    type: unlocked
    trigger: device
conditions: []
actions:
  - action: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: light.garage
  - action: timer.start
    metadata: {}
    data: {}
    target:
      entity_id: timer.garage_light_off_timer
    enabled: true
mode: restart

To turn the light off -

alias: Garage light off
description: ""
triggers:
  - trigger: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_light_off_timer
conditions: []
actions:
  - action: light.turn_off
    metadata: {}
    target:
      entity_id: light.garage
    data: {}
mode: single

What does the debug trace show?

If, as it appears, you have a second automation controlling the light, it might be helpful to share that as well.

I wager that you have to change your automation mode which is accessible on the top right corner menu so that instead of being in the default Single mode, it changes to Restart mode. If you don’t restart the automation by having it re-trigger when the conditions change, then Home Assistant will not execute the automation again because it’s still running because you set a timer.

Whenever possible, prefer not to leave an automation running for a long time, especially for times where you have to do something critical after that period of time has passed, because the automation will reset to zero and will stop running if you restart home assistant in the middle of it running. I would strongly prefer if Home Assistant restored the state of the automation at the time it was running when you restart Home Assistant. But that just isn’t the case today, and I am not sure this is ever going to be implemented.

Ignore my comment. I am an idiot and I did not read that your automation is already on restart mode. You will have to look at the traces in the automation (top right corner for the traces menu) to see what’s going on. Another useful tool that allows you to see what might be going on is the activity log, especially when filtered by the light itself, because that will tell you what exactly triggered the state change of the light.

Single automation to turn light on and off.

alias: Garage light
description: ""
triggers:
  - id: 'on'
    trigger: state
    entity_id: cover.your_cover
    to: 'open'
  - id: 'on'
    trigger: state
    entity_id: binary_sensor.your_binary_sensor
    to: 'on'
  - id: 'on'
    trigger: state
    entity_id: lock.your_lock
    to: 'unlocked'
  - id: 'off'
    trigger: event
    event_type: timer.finished
    event_data:
      entity_id: timer.your_timer
conditions: []
actions:
  - action: light.turn_{{ trigger.id }}
    target:
      entity_id: light.garage
  - if: "{{ trigger.id == 'on' }}"
    then:
      - action: timer.start
        target:
          entity_id: timer.garage_light_off_timer
mode: restart

This automation to me seems to turn on the light and start a timer. It doesn’t seem like it is responsible for what happens when the timer ends.

Updated my main post to include that info

Does it seem like your light off automation is running when your light on automation runs?

  • The first three State Triggers serve the same purpose as the ones in your example and are responsible for turning the light on and for starting the timer.

  • The fourth trigger is an Event Trigger that detects when the timer.finished event occurs.

  • When it detects the event, it passes the string value off via the trigger.id variable.

  • The first action uses the value of trigger.id to create the action light.turn_off.