How to calculate time_left in a timer

I tried to determine the light status using the jinja2 template used AI, as follows:

{% set timer_state = states('timer.lamp') %}  
{% set time_left = state_attr('timer.lamp', 'remaining') | default('00:00:00') %}  

{% if timer_state in ['active', 'paused'] %}  
  {% set time_parts = time_left.split(':') %}  
  {% set total_seconds = (time_parts[0] | int * 3600) + (time_parts[1] | int * 60) + (time_parts[2] | int) %}  

  {% if total_seconds > 240 and total_seconds <= 300 %}  
    1 minute on-a  
  {% elif total_seconds > 180 and total_seconds <= 239 %}  
    1 minute on-b  
  {% elif total_seconds > 120 and total_seconds <= 179 %}  
    1 minute on-c  
  {% elif total_seconds > 0 and total_seconds <= 119 %}  
    1 minute on-d  
  {% else %}  
    Off  
  {% endif %}  
{% else %}  
  Off  
{% endif %}

Code Explanation

  1. Getting Timer Status:
  • timer_state retrieves the current state of the timer for the lamp.
  • time_left gets the remaining time attribute from the timer, defaulting to '00:00:00' if no time is left.
  1. Checking Timer State:
  • The code checks if the timer is either active or paused.
  • If true, it splits the time_left string into hours, minutes, and seconds.
  1. Calculating Total Seconds:
  • It calculates the total number of seconds remaining by converting hours to seconds, minutes to seconds, and adding them all together.
  1. Determining Lamp Status:
  • Based on the total seconds, it determines the status of the lamp:
    • 1 minute on-a: Between 4 minutes and 5 minutes (240 to 300 seconds).
    • 1 minute on-b: Between 3 minutes and 4 minutes (180 to 239 seconds).
    • 1 minute on-c: Between 2 minutes and 3 minutes (120 to 179 seconds).
    • 1 minute on-d: Between 0 seconds and 2 minutes (0 to 119 seconds).
    • Off: If none of the above conditions are met (e.g., if the timer has finished).
  1. Off Status:
  • If the timer is not active or paused, it defaults to displaying Off.

but the results of the template above only read the status of 1 minute on-a and off, while 1 minute on-b and 1 minute on-c never appear.I suspect the timer_value is not updated in real time, or maybe there are suggestions for improving the template above. Please help so that the template above can function properly, thank you

This whole post looks like AI guessing. If you use AI, please state that you used AI.\

The only way to get a template that updates the timer duration is to use a template with now() in it. Otherwise the template will never update because state changes are not occuring. The remaining attribute does not update frequently. So you have to subtract the finishes_at time from the current time now() to get a number of seconds. But you’ll have to understand that the template will only calculate once a minute on the minute. So you likely will never have an exact secondly count for this template to work the way you want it to work.

If you attach a trigger that updates once a second to the template entity, it will properly calculate but keep in mind that it will always be running in the background. It could impact performance if you have cheap hardware running home assistant.

What are you wanting this info for? To use in an automation? Or just to display somewhere?

If the later, a custom timer-bar-card will do it. It can also be shown in a custom button-card. I am sure other cards will too. These are just the two I have used.

ok problem solved