How to best represent timers/countdowns?

You just made me realize I could use jninja templates to generate these. Actually surprised I can’t find others doing that. Gonna give it a try later today

Yes, each time a movement is detected, the timer is restarted.
So the light will be stopped 5 minutes after the last detected movement in my example.

But, I don’t get what’s the difference between this and checking that the movement sensor has been ‘to: off’ ‘for: 5 minutes’?? I mean, if it hasn’t been off for that time this counting will also be somehow reset, won’t it?

in this case, we need the trigger to be On, and start doing things. like turning on a light or switch. And then, after a certain period has passed, do yet another set of things. hence the timer and trigger On in 1 automation

OK, in that case I see the need of timers, thank you. :ok_hand:

For some system, for example some 433MHz to MQTT sensor, their is not state, but only an impulse every time a movement is detected. So you need a trigger based automation and not a state based one.

Another thing timer gives you is flexibility, you can restart, change the duration or stop a timer from any other automation.
Also you can choose to activate deactivate part of the automation, for example I often don’t want the light to be turned off after 5 minutes, and for that, I deactivate the “turn-off” automation.
Selection_108

And lastly, you can display the remaining time of a timer in the frontend, which is much harder if you use state based timing (but surely somehow possible).

1 Like

hmm - how do you put automation + timer setup into seperate files ? I thought there was no way to include two top level things via home assistant include support ?

also - when I use your example I see the timer start correctly the first time and count down - great. But when I move again the timer still counts down it does not stop. When it reaches zero nothing happened and when I moved again instead of counting down it just switched to “active” - no info about countdown.

Any idea on what is going on?

There is a way, and it’s super convenient :slight_smile:

It’s call package:
Under homeassistant in your config, add a packages entry to configure the directory containing the config file:

homeassistant:
  packages: !include_dir_named packages

In my case the folder is called packages.
And in this folder I have for example a file called office_light_automation.yaml which contain the example I posted earlier with automation and timer top level element.
Now I can copy this file for other light, without any problem with having several automation and timer blocked declared in different files.

Regarding the last question, no I’m not sure what’s going on. You’re sure your trigger or actions are configured properly?

I actually rechecked after your last question, with my setup, the display of timer in the frontend is also wrong.
The timer is restarted (because I still receive the finished event in my automation and the light are turned off), but the frontend still shows the initial count down, and doesn’t reinitialise itself.

okey so I’m not going crazy :slight_smile: i’ll see if I can isolate it and open a bug on it.

Whats weird though is that as you say the timer does seem to fire at the right time but when I get the state of it it still says the full time is remaining.

okey, cool - I’m trying to make it so I can generate those packages with templates so I can at least avoid having to update 6 different files for each area with a motion sensor.

I’m seeing the same issue here.
While the countdown timer works nicely in the standard UI as a badge, it fails to countdown in Lovelace.

Setup is as follows:

configuration.yaml

timer:
  countdown:
    duration: '00:20:00'

ui-lovelace.yaml

      - type: entities
        title: Timer
        entities:
          - entity: timer.countdown
            name: "Remaining Time:"

im trying to get the packages !include_dir_named packages to work by creating a packages folder and put the content of what you pasted verbatim in afiled called motionlight.yaml.

The result is this error:
expected a dictionary for dictionary value @ data['packages']['motionlight']

any idea on what can cause this ?

maybe any chance you have your config shared somewhere ?

thanks again!

okey so I accidentally deleted the first line and that gave that error…

Here is what I actually have in my file:


automation:
  - alias: Turn on winter garden when there is movement
    trigger:
    - platform: state
      entity_id: binary_sensor.presence_winter_garden
      to: 'on'
    action:
    - service: timer.start
      entity_id: timer.timer_winter_garden
    - service: homeassistant.turn_on
      entity_id: group.winter_garden_lights
  - alias: Turn off winter garden lights at end of timer
    trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.timer_winter_garden
    action:
      service: homeassistant.turn_off
      entity_id: group.winter_garden_lights

timer:
  timer_winter_garden:
    duration: '00:05:00'

which gives me:

Invalid config for [automation]: required key not provided @ data['action']. Got None
required key not provided @ data['trigger']. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/automation/

afaics its exactly like yours…I even tried copying yours verbatim whichs gives me similar:

automation:
- alias: Turn on office lights when there is movement
  trigger:
  - platform: state
    entity_id: binary_sensor.office_presence_sensor
    to: 'on'
  action:
  - service: timer.start
    entity_id: timer.timer_office
  - service: switch.turn_on
    entity_id: group.office_light
- alias: Turn off office lights at end of timer
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.timer_office
  action:
    service: switch.turn_off
    entity_id: group.office_light

timer:
  timer_office:
    duration: '00:05:00'

Under action, 2 spaces are missing before “- service” and before “entity_id”
Same for the trigger entry.

i thuoght that too but even when I get things indented I keep getting same error ;/

in this I dropped the use of include_dir… (for now)

  packages: 
    pack1:
      automation:
        - alias: Turn on winter garden when there is movement
          trigger:
            - platform: state
              entity_id: binary_sensor.presence_winter_garden
              to: 'on'
          action:
            - service: timer.start
              entity_id: timer.timer_winter_garden
            - service: homeassistant.turn_on
              entity_id: group.winter_garden_lights
        - alias: Turn off winter garden lights at end of timer
          trigger:
            - platform: event
              event_type: timer.finished
              event_data:
                entity_id: timer.timer_winter_garden
          action:
            - service: homeassistant.turn_off
              entity_id: group.winter_garden_lights

      timer:
        timer_winter_garden:
          duration: '00:05:00'```

so via Invalid config for [automation]: required key not provided @ data['action'] I found that if I comment out automation: !include automations.yaml then the check passes.

no idea why that should matter since my automations.yaml is empty :slight_smile:

Your automation.yaml being empty might actually be an issue - I recall following a discussion here where linking to an empty automation file cause issues, so somebody created a dummy automation in the yaml-file:

1 Like

fyi I opened https://github.com/home-assistant/home-assistant/issues/18821 and after submitting found a bunch of other issues having related issues with timers that I linked in too.

Hopefully we can get that cleaned up :slight_smile:

1 Like