How can I adjust the time displayed in a timer?

So, a while back Hellis81 helped me set up a timer with the following code:

{% if states('timer.YOUR_TIMER_NAME') != "idle" %}
{{ (as_timestamp(state_attr('timer.YOUR_TIMER_NAME', 'finishes_at')) - now().timestamp()) | timestamp_custom('%M:%S') }}
{% endif %}

This timer displays up to 59 minutes. I wanted to set up a timer for two hours, so I changed (‘%M:%S’) to (%H:%M:%S’).

Now the timer would count down from more than 59 minutes, but for some reason it would add 4 hours to the displayed time. It would count down for the correct amount of time, but it would just add 4 hours to what is displayed.

For example, if I wanted a timer for 5 minutes, it would display 4:05:00 and start counting down until the five minutes pass at 4:00:00, then it would reset. If I wanted a timer for 1.5 hours, it would display 5:30:00 and count down from there until 1.5 hours pass and reach 4:00:00 and then reset.

Can some one help me so it displays the correct time?

Show your yaml file, all of it, in code tags.


# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes
  extra_module_url:
    - /local/community/custom-brand-icons/custom-brand-icons.js

http:
  use_x_forwarded_for: true
  trusted_proxies:
    - 127.0.0.1

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

template:
  - sensor:
      - name: AR charger timer SENS
        unique_id: 'AR charger timer SENS'
        state: > 
          {% if states('timer.ar_charger_timer_helper') != "idle" %}
          {{ (as_timestamp(state_attr('timer.ar_charger_timer_helper', 'finishes_at')) - now().timestamp()) | timestamp_custom('%H:%M:%S')}}
          {% elif states('timer.ar_charger_timer_helper') == "idle" %}
          {{ states.input_number.ar_charger_timer.state | int }}
          {% endif %}
        icon: >
          {% if states('timer.ar_charger_timer_helper') != "idle" %}
            mdi:timer-minus-outline
          {% elif states('timer.ar_charger_timer_helper') == "idle" %}
            mdi:timer
          {% endif %}
{% if states('timer.YOUR_TIMER_NAME') == 'active' %}
{% set ts = (state_attr('timer.YOUR_TIMER_NAME', 'finishes_at') | as_datetime - now()).total_seconds() %}
{{ '%02d:%02d:%02d' | format(ts / 3600, ts // 60 % 60, ts % 60) }}
{% endif %}
1 Like

Thanks, I’ll let you know :slight_smile:

Simply change this in your template:

timestamp_custom('%M:%S')

To this:

timestamp_custom('%H:%M:%S', false)

The 4 hour discrepancy you saw was your local timezone offset. The second parameter in timestamp_custom determines how it handles timezone (it’s true by default).

1 Like

This worked, thank you so much

1 Like

Sorry, didn’t get the chance to test this, 123’s solution worked. Thank you so much though :slight_smile:

1 Like