Calculate time left percentage of timer

I am trying to calculate the percentage of a timers time left. I have run into the problem, that my template sensor, just won’t update.

      - name: "Timer Completion Percentage"
        unit_of_measurement: "%"
        state: >
          {% set total_duration = state_attr('timer.my_timer', 'duration') | float %}
          {% set remaining_duration = state_attr('timer.my_timer', 'remaining') | float %}
          {% if remaining_duration is not none and total_duration > 0 %}
            {{ ((total_duration - remaining_duration) / total_duration * 100) | round(1) }}
          {% else %}
            0
          {% endif %}

If I understand it correctly, that is because the timers state does not change.
So how could I calculate the percentage updating for example once every 10 second? Using an automation, that will trigger the homeassistant.update_entitiy does not seem to do anything either.

alias: update percentage standing timer
description: ""
triggers:
  - seconds: "10"
    trigger: time_pattern
conditions: []
actions:
  - target:
      entity_id: sensor.timer_completion_percentage
    action: homeassistant.update_entity
    data:
      entity_id:
        - sensor.timer_completion_percentage
mode: single

Any tips or ideas would be greatly appreciated :smiley:

The Time Pattern Trigger you created is configured to trigger every minute at 10 seconds past the minute.

08:30:10
08:31:10
08:32:10
08:33:10

If you want every 10 seconds, like this:

08:30:00
08:30:10
08:30:20
08:30:30

the notation is this:

  - trigger: time_pattern 
    seconds: "/10"

However, I suspect this will not fix anything because a timer’s remaining attribute doesn’t change while it’s operating.


EDIT

You have to compute a timer’s remaining time.

Subtract the current time now() from the value of the finishes_at attribute (which exists only when the timer is operating).

1 Like