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
- 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.
- Checking Timer State:
- The code checks if the timer is either
active
orpaused
. - If true, it splits the
time_left
string into hours, minutes, and seconds.
- Calculating Total Seconds:
- It calculates the total number of seconds remaining by converting hours to seconds, minutes to seconds, and adding them all together.
- 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).
- 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