Help with timers

I’m embarrassed to ask this, but I can’t get this to work. I have a timer timer.office_lights and I want the lights to turn off when the timer finishes. When the lights go on the timer starts, but when the timer finishes nothing happens.

Help please (set to 10s for testing):

Thanks in advance for any help

timer:
  office_lights:
    duration: '00:00:10'

- alias: Lights - Office Lights Timer
  trigger:
        platform: state
        entity_id: light.office
        to: 'on'        
  action:
      - service: timer.start
        data:
            entity_id: timer.office_lights
        
- alias: Lights - Office Lights Off
  trigger: 
        platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.office_lights 
  action:
        service: light.turn_off
        entity_id: light.office

The problem with this use is people.
If they turn on the light in the toilet (for example) the timer is expected to turn the light off after (say) 15m. OK fine.
But I go in and turn the light off when I leave after 6 mins, you then come in 6 mins later and turn the light on.
The timer is already running and you only get 3mins before it all goes dark.
Instead, use a script and a delay.
The automation runs when the light turns on.
It Cancels the script (even if not running) and then starts the script
The script runs the delay (I usually set the delay via an input_number) and then it turns the light out.
I don’t have access to my examples right now but I’ll post a sample when I get back (about an hour)

Edit: be aware if you restart (mid timer OR delay ) - you will lose the turn off

Mega overkill.

- alias: Lights - Office Lights Timer
  trigger:
    platform: state
    entity_id: light.office
    to: 'on' 
    for:
      minutes: 10
  action:
    service: light.turn_off
    entity_id: light.office

This is from one of my packages so you ‘may’ have to adjust indentation to align with your configuration.
Entities shown are sample entities, edit to suit your system and entities.
Timer is in minute units, but you could do the same with hours or seconds.
Note: I have (and do) set minute timers to values of 720 (not had a problem yet)
The input number (without initial set) will retain your last set value accross restarts

input_number:
## delay off timer value
  in_light_sample_timer:
    name: Sample Light On Timer (mins)
    #initial: 2
    min: 1
    max: 900
    step: 1
    mode: box
    icon: mdi:alarm

automation:
##name: Light Sample Off Delay
  - alias: au_light_sample_offdelay
    trigger:
      - platform: state
        entity_id: light.sample
        from: 'off'
        to: 'on'
    action:
      - service: script.turn_off
        entity_id: script.sc_light_sample_timer
      - service: script.sc_light_sample_timer

## name: Switch Sample Off Cancels Timer
  - alias: au_switch_kitchenf_offcancelstimer
    trigger:
      - platform: state
        entity_id: light.sample
        to: 'off'
    action:
      - service: script.turn_off
        entity_id: script.sc_light_sample_timer

script:
## light off timer
  sc_light_sample_timer:
    alias: Light Sample Timer Script
    sequence:
    - delay: "00:{{ states('input_number.in_light_sample_timer') | int }}:00"
    - service: light.turn_off
      entity_id: light.sample

I can’t use that as my lights have the brightness changed according to time of day and whenever the adjustment happens I lose the timer (or it restarts - same difference)

Marc, can you template the time value in something like this ? (the for bit) ???

My full automation is a lot more complicated (motion, dark_sky,time triggers etc and more conditions) - I’ve just added the bit that isn’t working.

That seems a bit complicated for me. If I can get the timer off event working, I can finish my full automation i.e.

  • someone turns the light on manually, start the timer or reset the clock if already running
  • ditto with motion etc if clock not running start, else reset the timer

Yup

trigger:
  platform: state 
  entity_id: light.test
  to: 'on' 
  for:
    minutes: "{{ states('input_number.light_on_length')|int }}"
1 Like

solved. must have been something wrong with my indention.

Posting the corrected code would be helpful for other users.

I’ve learnt something new, thanks

It’s good you solved it but you have also marked your own post (with nothing relevant in it) as the solution.
Given where you started and where you ended up, I assume you meant to give the solution to Marc’s first post ???

Marked solved as I couldn’t see any differences to my initial post, but everything working now.

Sharing what my full automation if anyone interested.

This works as the tester:

timer:
  office_lights:
    duration: '00:00:10'

- alias: Lights - Office Lights Timer
  trigger:
        platform: state
        entity_id: light.office
        to: 'on'        
  action:
      - service: timer.start
        data:
            entity_id: timer.office_lights
        
- alias: Lights - Office Lights Off
  trigger: 
        platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.office_lights 
  action:
        service: light.turn_off
        entity_id: light.office

and here’s the real one

  • timer starts if someone turns light on manually
  • if no motion in 5 mins, then finished timer turns off lights. If there’s been motion, then waits for 5 mins of no motion

I could have added a condition for if the timer is running, don’t turn off lights but my thinking is if no-ones there then it’s ok to turn off the lights. In the future if I have some toilet/bathroom lights I might do this.


- alias: Lights - Office Lights Timer
  trigger:
        platform: state
        entity_id: light.office
        to: 'on'        
  action:
      - service: timer.start
        data:
            entity_id: timer.office_lights
        
- alias: Lights - Office Lights Off
  trigger: 
      - platform: state
        entity_id: binary_sensor.motion_office_motion
        to: 'off'
        for:
            minutes: 5
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.office_lights 
  condition:
        condition: state
        entity_id: binary_sensor.motion_office_motion
        state: 'off'
        for:
            minutes: 5
  action:
        service: light.turn_off
        entity_id: light.office