How to best represent timers/countdowns?

I would like to visualize my timers/countdowns I virtually created for my motion controlled lights. Wondering how to do it best.

I know I could use appdeamon to update an entity every second with the number of seconds left, but it just feels wrong having 5 motion sensors updating every second and generate lots of noise in the state changes/traffic.

Alternatively I’ve created an entity with state on|off and a timestamp as attribute for when it will fire/time out.

This at least reduces the state updates to a minimum - but how would you go about showing in the UI how many seconds before it triggers ?

I tried using templates based on time/date but the highest resolution here seem to be every minute which would be to low.

1 Like

Did you simply try to show the timer itself? (I also didn’t know it was possible) ^^

To do so, add the timer to a group or a view:

group:
  zigbee_group:
    view: no
    control: hidden
    name: Zigbee2mqtt
    entities:
      - timer.time_remaining

Selection_315

And here is the declaration of the timer used in this example:

timer:
  time_remaining:
    name: Time remaining

The time is refreshed every seconds, and when the timer is not active/finished, there is simply no time displayed:
Selection_316

nice tip - now would be nice to know how to create/integrate such timers from appdemon apps… don’t think its timers are the same thing.

Ah sorry, didn’t notice it was other type of timer.

Maybe it would be easier to migrate to the standard timer/automation? I use them with motion sensor and light too.

So i tried using them but as soon as I call the timer.start with amount of seconds the visualizations of countdown stops and it just says activated.

How have you done yours ? All my attempts outside of appdeamon results in slot of duplication I feel ( I have 6 motion sensors :slight_smile:

Would love to see how someone done it outside appdeamon.

Here is how I control my lights:

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'

Yeah so I would need 6x of that :confused:

I guess I should just bite the bullet

It’s that or appdaemon + plenty of time to figure out how to make it work :sweat_smile:

I always put it in separate files, it also have the advantage that if you decommission one lamp, you can remove it with all associated sensor/timer. Same for adding one, just copy/paste the file and modify the entities id.

1 Like

never having used a timer yet, but I think this accomplishes the same? What, if any, would be the difference in end-result here? Does the timer restart when motion is detected, or does it simply count down and switch off the light. My binary_sensor timed waits for no movement and until that moment doesn’t act. Which is what I need. Can I achieve that with the timer also?

  - alias: 'Switch Masterbed outlet when movement'
    id: 'Switch Masterbed outlet when movement'
    trigger:
      platform: state
      entity_id: sensor.master_bedroom_motion_sensor
      to: 'on'
    condition:
      - condition: template
        value_template: >
          {{ is_state ('sensor.activity_selection', 'Slapen')}}
      - condition: template
        value_template: >
          {{is_state('switch.master_bed_outlet', 'off')}}
    action:
      - service: switch.turn_on
        entity_id: switch.master_bed_outlet
      - wait_template: >
          {{ is_state('binary_sensor.master_bedroom_motion_sensor_timed','off')}}
      - service: switch.turn_off
        entity_id: switch.master_bed_outlet

binary_sensor:
  - platform: template
    sensors:
      master_bedroom_motion_sensor_timed:
        friendly_name: 'Master bedroom motion sensor timed'
        value_template: >
          {{ is_state('sensor.master_bedroom_motion_sensor','on')}}
        delay_off:
          minutes: 1
        device_class: motion

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.