Display remaining seconds of timer in frontend

It does not work again!

I have to admit I’m a bit lost here.
How is this at all useful in a home automation context.?
It seems pointlessly processor intensive and I can’t think of a single use.
If you want to time something, to get the automation just right, use a stopwatch, there’s probably one on your phone.
I could also time the 100 metre dash at a school race meet using an IBM mainframe from the 1970’s but I don’t as there are more appropriate tools.
If you are spending time watching timers count down to zero, then you clearly are not getting any benefit from home automation.

Well. If this task is needlessly processor intensive, I am fine with it.

I use timers primarily for cooking. And I constantly need to check how much time the timer has left, for me to prepare other parts of the meal. I used to ask my Google Home but since I threw that out, I wanted to display the timer on my wall mounted tablet showing the HA interface (I use Snips now for voice which sends commands to Home Assistant)

I don’t think that use case is extremely far fetched. Granted, it has little to do with “automation”, but it makes my life a little easier not having to leave my phone in the middle of my kitchen surface for the timer alone.

Absolutely, it’s your choice, though I’m pretty hard pushed to think of any cooking action that needs precision to the second either. Still some people are really fussy about their boiled eggs :wink:

1 Like

I have been thinking about this, you could do full recipes.
You get a sounder of some sort. (I have a neo coolcam siren, not as intrusive as it sounds, it can be set to different tunes, 3 seconds or so)
You populate an input_select with your recipe steps, you populate another with the times between, then you start the recipe, it tells you what to do, then it alerts you for the next step, the next step etc.
Again I wouldn’t do it but, there is probably an ocd epicurean out there. :rofl:

Edit: that’s two selects per recipe, you might end up with quite a few and organising them would be ‘interesting’ but if done as packages you could share recipes. :rofl: :rofl:

1 Like

Hey guys,

Long time ago - but I assume, some are still interested in a solution!

Came across this post while looking for some more information, but couldn’t find anything that was easy to do and then decided to tweak around and actually just found a way! :smiley:

I use picture-entity combined with a state-badge to mimic a glance card, the timer is always up-to-date and only shows when it’s “active”:

21

Here’s my card:

        - elements:
          - conditions:
              - entity: timer.test1
                state: active
            elements:
              - entity: timer.test1
                hold_action:
                  action: null
                style:
                  color: transparent
                  font-size: 150%
                  left: 85%
                  top: 15%
                tap_action:
                  action: null
                type: state-badge
            hold_action:
              action: null
            tap_action:
              action: null
            type: conditional
          - conditions:
              - entity: light.test
                state_not: blabla
            elements:
              - entity: light.test
                hold_action:
                  action: toggle
                style:
                  left: 50%
                  top: 50%
                tap_action:
                  action: more-info
                type: state-icon
            hold_action:
              action: null
            tap_action:
              action: more-info
            type: conditional
        image: /local/test-30x100px-transparent.png
        type: picture-elements

(don’t mind the “state_not: blabla”, as this is only to get the light-icon permanent)

The background image is just a transparent 30x100px png which I created in GIMP.

Now, you have to add/change the following to/in your theme to hide the badge circles and background, otherwise it doesn’t look good aesthetically - this will also change other badges (tweak around with this one, but also remember what it was before, if you don’t want to lose anything)

label-badge-background-color: "var(--paper-card-background-color)"
label-badge-border-color: "transparent"
label-badge-red: "transparent"
label-badge-text-color: "darkgrey"
2 Likes

That looks like a much cleaner solution then the kludge I did.

I created an input number that would record the as_timestamp(now()) when the timer was started, and then another sensor that would calculate the active timer_duration - (input_number - as_timestamp(now())) to work out how long was left on the timer. It works perfectly but is impractical to do for all timers, I only do it where I need to know how long is left on a timer, to add or minus time from an existing running timer or I want something I can visually see on the GUI.

1 Like

this works for me.
how do i allow the card to be shown even if the timer is idle?

Don’t use a Conditional Card?

didn’t occur to me. haha thanks

Hey @callifo

Could you share the yaml of your solution? It is a little crude but basically what I am looking for. I only use it for one timer and it would allow me to query the remaining duration in other places

Configuration.yaml, sensors stored as both remaining seconds, and HH:MM:SS

sensor:
  - platform: mqtt
    state_topic: "home/timers/timer.aircon_run_timer/start"
    name: "Timer Start Aircon Run"
  - platform: mqtt
    state_topic: "home/timers/timer.aircon_run_timer/duration"
    name: "Timer Duration Aircon Run"
  - platform: template
    sensors:
      timer_remaining_sec_aircon_run:
        friendly_name: "Timer - Remaining Seconds - Aircon Run"
        value_template: >-
          {%- if (states['timer.aircon_run_timer'].attributes.duration == "none") or (states['timer.aircon_run_timer'].state == "idle") -%}
          {{ 0 | int }}
          {%- else -%}
          {{ ((((states['timer.aircon_run_timer'].attributes.duration).split(':')[0] |int * 60 * 60) + ((states['timer.aircon_run_timer'].attributes.duration).split(':')[1] |int * 60 ) + ((states['timer.aircon_run_timer'].attributes.duration).split(':')[2] |int)) - (as_timestamp(now()) | float - states['sensor.timer_start_aircon_run'].state | float) ) | int }}
          {%- endif -%}
        entity_id: 
          - timer.aircon_run_timer
      timer_remaining_hms_aircon_run:
        friendly_name: "Timer - Remaining HMS - Aircon Run"
        value_template: >-
          {%- if states['sensor.timer_remaining_sec_aircon_run'].state == 0 -%}
          00:00:00
          {%- else -%}
          {{ states['sensor.timer_remaining_sec_aircon_run'].state | int | timestamp_custom('%H:%M:%S', 0) }}
          {%- endif -%}
        entity_id: 
          - sensor.timer_remaining_sec_aircon_run

Automation to record initial start time (via MQTT, not input_number, my mistake), and then periodically update the sensor, as HA doesn’t seem to update it automatically, only runs when a timer is active.

Last automation restores active timer value on HA restart

- id: timer_start_all_timers
  alias: 'Store - Start - All timers'
  hide_entity: True
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: 
        - timer.aircon_run_timer
      to: 'active'
  action:
    - service: mqtt.publish
      alias: MQTT store timer timestamp
      data_template:
        payload_template: "{{ as_timestamp(now()) | int }}"
        topic: "home/timers/{{trigger.entity_id}}/start"
        retain: true
    - service: mqtt.publish
      alias: MQTT store timer duration
      data_template:
        payload_template: "{{ ((trigger.to_state.attributes.duration).split(':')[0] |int * 60 * 60) + ((trigger.to_state.attributes.duration).split(':')[1] |int * 60 ) + ((trigger.to_state.attributes.duration).split(':')[2] |int) }}"
        topic: "home/timers/{{trigger.entity_id}}/duration"
        retain: true
- id: timer_remaining_update
  alias: 'Timer - Remaining - Update all'
  hide_entity: True
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id:
        - timer.aircon_run_timer
      to: 'active'
    - platform: time_pattern
      seconds: "/5"
  condition:
    - condition: or
      conditions:
        - condition: state
          entity_id: timer.aircon_run_timer
          state: 'active'
  action:
    - service: homeassistant.update_entity
      entity_id:
        - sensor.timer_remaining_sec_aircon_run
- id: timer_restart_all_timers
  alias: 'Store - Restart at Boot - All timers'
  hide_entity: True
  initial_state: 'on'
  trigger:
    - platform: homeassistant
      event: start
  action:
    - service_template: >
        {% if (states.sensor.timer_start_aircon_run.state | int + states.sensor.timer_duration_aircon_run.state | int) > (as_timestamp(now()) | int) %}
          timer.start
        {% else %}
          script.do_nothing
        {% endif %}
      data_template:
        entity_id: timer.aircon_run_timer
        duration: "{{ ((states.sensor.timer_start_aircon_run.state | int + states.sensor.timer_duration_aircon_run.state | int) - as_timestamp(now()) | int) | timestamp_custom('%H:%M:%S', 0) }}"

Once you’ve got the initial automations in there, you can just add more timers to them (they use trigger entity_id so are generic).

6 Likes

Wow, that is some automation. Thank you for sharing!

late post, but can you share your config of sensor.timer_start_aircon_run? I see it referenced in the post but didn’t find the config for it.

That is the sensor that is created by the MQTT sensor (at the top of the first section),

sensor:
  - platform: mqtt
    state_topic: "home/timers/timer.aircon_run_timer/start"
    name: "Timer Start Aircon Run"

It’s possible to use it with input_number.
I have one irrigation system with input time and start button.
I want display count down of this input_time number.

Somebody can help me, give me some orientation?
Thanks a lot for script callifo :wink:

Same here… I Would like to have access to the timer’s remaining time. At least the minutes that are remaining. But it seems that there is no “clean” way to do it.

Agreed. I really hope there will be a ‘clean’ and simple way to do this soon.
@pnbruckner - do you have any ideas about this? Getting the current value (remaining time) of a timer?
For my desired use, I want to create a nice custom big timer view that I’ll cast.

Please don’t randomly tag people to bring them into foreign threads.

The best way to get a ‘countdown’ is to create a timestamp sensor when the timer end will occur. The UI will naturally count down to that timestamp but it will be in a long winded format. There may be other ways to handle this now that the main event loop is sub-second based as well. But these are uncharted waters in that regards.

This sensor should get the job done.

sensor:
  - platform: template
    sensors:
      timer_end:
        friendly_name: "Timer End"
        device_class: timestamp
        entity_id: timer.xxxxxxxxxxxxxxxx # <--- Your timer (and first line of template)
        value_template: >-
          {%- set duration = state_attr('timer.xxxxxxxxxxxxxxxx ', 'duration') %}
          {%- if duration in ['none', 'idle'] %}
            unavailable
          {%- else %}
            {% set h, m, s = duration.split(':') %}
            {% set n = now().timestamp() | list | map('int') %}
            {{ (n + h * 60 * 60 + m * 60 + s) | timestamp_custom('%Y-%m-%dT%H:%M:%S-00:00', False) }}
          {%- endif -%}

This will create a sensor that looks weird in the template section, but when you place it into an entity or entities card, it will update live.

2 Likes

Is there something missing in the timer_end Template Sensor because, based on a test, it doesn’t update live.

Before it can be tested, the template needs this minor adjustment because h, m, and s are strings:

(n + h|int * 60 * 60 + m|int * 60 + s|int)

When the associated timer begins its countdown, the Template Sensor displays the timestamp. However, during the countdown, it doesn’t change because its entity_id (the associated timer) maintains a constant state of active and none of its attributes change.

You can sort of see that from this screenshot where timer.my_timer is active (therefore in a countdown) but sensor.my_timer maintains the same value (5 minutes and 15 seconds) throughout the countdown.
Screenshot from 2020-07-24 09-28-56