Triyng to create template sensor a countdown timer

Hey, I want a boiler that I turn on it by timer
I want to create a countdown timer, and I have a few questions:
This is the first try, but the seconds are stuck at 59 seconds, and only the minutes changed

 - platform: template
    sensors:
      countdown_timer:
        friendly_name: "Countdown Timer"
        value_template: >-
          {% set remaining_seconds = (as_timestamp(now().replace(hour=state_attr('input_datetime.countdown_timer', 'hour'), minute=state_attr('input_datetime.countdown_timer', 'minute'))) - as_timestamp(now()) | timestamp_custom("%-I:%H %p") ) | int %}
          {% set remaining_minutes = ((remaining_seconds // 60) % 60) %}
          {% set remaining_hours = ((remaining_seconds // 60) // 60) %}
          {% set remaining_days = (remaining_seconds // (60 * 60 * 24)) %}
          {% set remaining_seconds = remaining_seconds % 60 %}
          {% set remaining_time = "%02d:%02d:%02d" % (remaining_hours, remaining_minutes, remaining_seconds) %}
          {% if remaining_seconds <= 0 %}
            Expired
          {% else %}
            {{ remaining_time }}
          {% endif %}

And the second code is:

{% set remaining_seconds = (states.input_datetime.countdown_timer.attributes.timestamp - as_timestamp(now())) | int %}
{% set remaining_minutes = ((remaining_seconds // 60) % 60) %}
{% set remaining_hours = ((remaining_seconds // 60) // 60) %}
{% set remaining_days = (remaining_seconds // (60 * 60 * 24)) %}
{% set remaining_seconds = remaining_seconds % 60 %}
{% set remaining_time = "%02d:%02d:%02d" % (remaining_hours, remaining_minutes, remaining_seconds) %}
{% if remaining_seconds <= 0 %}
  Expired
{% else %}
  {{ remaining_time }}
{% endif %}

but here, the time still counts after the time that I set in input_datetime
think is needed to fix this function:

{% set remaining_seconds = (states.input_datetime.countdown_timer.attributes.timestamp - as_timestamp(now())) | int %}

These are the value of the input_datetime returns:

And I update the sensor every second:

  - alias: 'Update Countdown Timer'
    trigger:
      platform: time_pattern
      seconds: '/1'
    action:
      service: homeassistant.update_entity
      entity_id: sensor.countdown_timer

I would love to get help. Many thanks!

Your template is evaluated whenever a state-change occurs in one of the entities it references (input_datetime.countdown_timer) and every minute because it employs now(). In other words, the template is evaluated no faster than once a minute.

If you want to force it to update faster, consider using a Trigger-based Template Sensor (with a 1-second Time Pattern Trigger). However, your Template Sensor is configured in legacy format, which doesn’t support Trigger-based Template Sensors, so you’ll have to convert it to modern format.

A more efficient solution is to use a Timer (because forcing a Template Sensor to update every second is inefficient).

Did it like that:

automation.yaml:

  - alias: 'Update Countdown Timer'
    trigger:
      platform: time_pattern
      seconds: '/1'
    action:
      service: homeassistant.update_entity
      entity_id: sensor.boiler_off_timer

sensor.yaml

      boiler_off_timer: # timer.boiler_off_timer
        value_template: >-
          {% if is_state("switch.switcher_touch_f33f", "off") %}
              off
          {% else %}
            {% set f = state_attr("timer.boiler_off_timer","finishes_at") %}
            {% set val = '00:00:00' if f == None else (as_timestamp(f) - as_timestamp(now() + timedelta(hours=2))) | timestamp_custom("%H:%M:%S") %}
            {{ val }}
          {% endif %}

script.yaml

      - service: timer.start
        data_template:
          duration: "00:{{ minutes }}:00"
          entity_id: timer.boiler_off_timer

timer.yaml

timer:
  boiler_off_timer:
    icon: mdi:timer-outline

A timer entity’s countdown can already be displayed by an Entities card therefore there’s no need for the Template Sensor you’ve created or the automation.

If you don’t want the timer to start if switch.switcher_touch_f33f is off, just add that condition to the script.

I repeat, it’s inefficient to use a Time Pattern Trigger to force the evaluation of a Template Sensor every second. It’s a design decision that will come back at a later date to plague you with seemingly inexplicable performance problems.