Timer examples

Does anyone have any real world examples of using the new timer control ?

Every example I can think of myself can already be done with platform: state, for example:

- alias: Turn switch off after 1 hour
  trigger:
    platform: state
    entity_id: switch.my_switch
    to: 'on'
    for:
      minutes: 60
  action:
    - service: switch.turn_off
      entity_id: switch.my_switch

or this: (from here)

- alias: Turn off kitchen light 10 minutes after last movement
  trigger:
    platform: state
    entity_id: sensor.motion_sensor
    to: 'off'
    for:
      minutes: 10
  action:
    service: homeassistant.turn_off
    entity_id: light.kitchen_light

Itā€™s hard to pause or cancel a timer when using state change duration.
You could also get some cumulative time with the timer (For example time at home, or duration for which a lamp was on).

Iā€™m sure everything was possible with some hack, it is meant to simplify things.

As usual, the ha documentation is not sufficient to use the component without a lot of guessing and experimentation.
However, examples are often the most useful source of information.
Here is an example that works but may or may not be the best way to use this component:

timer:
  garage_door_lights:
  duration: '00:08:00'

automation:
  - id: timergaragelightsoff
    alias: atoff
    trigger:
      platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.garage_door_lights
    action:
      service: homeassistant.turn_off
      #entity_id: switch.garage_interior_switch
      entity_id: switch.tester_switch_switch
  - id: garagedooropen
    alias: "garage door opened"
    trigger:
      - platform: state
        entity_id: sensor.garage_door_west_alarm_level
        from: '255'
        to: '0'
    action:
      - service: homeassistant.turn_on
        entity_id: switch.garage_interior_switch
      - service: timer.start
        entity_id: timer.garage_door_lights

The key is the ā€˜event_dataā€™ entry that (i think) will associate the event with that specific timer. Leaving off the ā€˜event_dataā€™ tag works too but I suspect that the automation will then respond to any timer that fires.
This is the use case that I immediately thought of when I saw this component. It replaces a series of scripts I was using to accomplish the same goal with a more concise syntax.

YMMV

3 Likes

Was it not even simpler with the delay function:

automation:
  alias: Garage door open
  trigger:
    - platform: state
      entity_id: sensor.garage_door_west_alarm_level
      from: '255'
      to: '0'
  action:
    service: homeassistant.turn_on
    entity_id: script.timed_garage_lamp

script:
  timed_garage_lamp:
    sequence:
      - service: switch.turn_on
        data_template:
          entity_id: switch.garage_interior_switch
      - service: script.turn_on
        data:
          entity_id: script.timer_garage_off

  timer_garage_off:
    sequence:
      - delay: '00:08:00'
      - service: switch.turn_off
        data:
          entity_id: switch.garage_interior_switch

Thanks for your replies, I think the light bulb above my head is slowly sparking to life :slight_smile: My first thought was that the above solution would be much more readable/elegant if written as below:

automation:
  - id: timergaragelightsoff
    alias: atoff
    trigger:
      platform: state
      entity_id: switch.garage_interior_switch
      to: 'on'
      for:
        minutes: 8
    action:
      - service: homeassistant.turn_off
        entity_id: switch.garage_interior_switch        
  - id: garagedooropen
    alias: "garage door opened"
    trigger:
      - platform: state
        entity_id: sensor.garage_door_west_alarm_level
        from: '255'
        to: '0'
    action:
      - service: homeassistant.turn_on
        entity_id: switch.garage_interior_switch

But that would give a problem if I manually switched on the light via say a wall switch. With your example I could cancel the timer to leave the lights on after switching them manually.

1 Like

Yes, my total use case is more complex as there are three doors and a physical switch. When is opening or closing a second door just before the previous automation turns off the light the previous action will still turn off the light on its own timeline. It is necessary to cancel the previous script (if it is running) and restart it to reset the delay. This required a minimum of two scripts to decouple the turn off delay from the sensor events being fired by three doors. The timer simply extend the delay window automatically when it receives the fresh signal from one of the sensors which simplifies the code significantly.

I like the way a timer can be used to perpetuate an action that has been started ā€“ turn the lights on for four minutes after motion detection ā€“ when the motion sensor fires a second time within the four minutes.

The advantage of using delay is that the delay duration can be configurable from the front end using an input_number and the value of the input_number is fetched at run-time, so changes in the input_number value can be made any old time. Is it possible to do the same with a timer, which is declared in configuration.yaml (i.e. static at startup)? However, i guess the value of the input slider would only be applied as the timer duration as the result of a homeassistant restart.

timer:
some_timer:
duration: ā€˜00:{{ states.input_number.timer_duration_minutes.state | int }}:00ā€™

This is exactly what Iā€™m trying to do - use an input slider to change the duration for future timer countdowns. Iā€™ve messed around with templates and scripts, but canā€™t get it to work. Has anyone found a solution?

auseco. Maybe there is a wack-on-the-side-of-the-head approach to this.

I wonder, since timers are statically defined in the config, maybe the approach is to create a bunch of timers, one for each of your desired duration, create an appropriate input_thingy that allows you to ā€œindicateā€ a timer that has been defined in the config, and then, based upon the value of the input_thingy, dynamically select the pre-configured timer to use in your automation/script.

wonder if that would work?

Good idea. Itā€™s brute force, and not as elegant as Iā€™d hoped, but it should work. Thanks for the suggestion. Iā€™m going to give it a try. If anyone else gets this approach working before me, please upload a code snippet.

auseco,

Ya know what. i have read again the Timer component page and have come to realize that the duration of the timer that is defined in configuration.yaml is the default duration.

Have a look at the component details and, while it is subtle, you will see that it indicates that the instantiation of a timer can include a duration that will override the default.

I have not tried it yet, but will soon.

Hope this helps.

Hello.
I have an rgb led strip, that indicates events for me, and also server as a night light when motion detected.
When motion detected I turn it on, and start a timer.
But when the led color is overridden by an event, and I had to change it.
Eg security alarm - someone came into the house - i turn it to red. I donā€™t want it to change back to white when the person reaches the motion sensor.
So here is my example:

    - id: 'kitchenlightupatnight'
      alias: "Light - Kitchen auto on at night"
      initial_state: 'on'
      trigger:
        platform: state
        entity_id: binary_sensor.kitchen_motion
        from: 'off'
        to: 'on'
      condition:
        condition: and
        conditions:
          - condition: state
            state: 'below_horizon'
            entity_id: sun.sun
          - condition: or
            conditions:
              - condition: state
                state: 'off'
                entity_id: light.kitchen_rgb
              - condition: state
                state: 'active'
                entity_id: timer.kitchen_rgb
      action:
      - data:
          entity_id: light.kitchen_rgb
          color_name: white
          brightness: 255
        service: light.turn_on
      - service: timer.start
        entity_id: timer.kitchen_rgb

It turns on only at night, and only if already turned on by a prev motion (to restart the timer), or nothing using it (state off)

When something has to override it i do this:

    - id: '1340538301118'
      alias: Led alert - armed but door opened
      trigger:
        platform: state
        entity_id: binary_sensor.front_door
        from: 'off'
        to: 'on'
        for:
          seconds: 1
      condition: 
        condition: state
        entity_id: alarm_control_panel.ha_alarm
        state: 'armed_home'
      action:
      - service: timer.cancel
        entity_id: timer.kitchen_rgb
      - data:
          entity_id: light.kitchen_rgb
          color_name: red
          brightness: 180
          effect: flash
        service: light.turn_on

So simply cancel the timer, and set the colour. So none of the 2 condition will be true, so nothing will override it. When the alarm goes off, the led state will be off, so the next motion will turn it on.

1 Like