Add time to an already running timer

There’s already a thread for that matter, but every solution proposed in that thread does not seem to work.
So, I am sharing my approach for anyone else that might want to do something similar.

The timer entity provides several attributes, such as the duration and the remaining time.
However, in my case, when a timer is running, the remaining time attribute does NOT change.

Thus, i’ve used the finishes_at attribute as you can see in the following script.
The idea is to calculate the seconds between now and the time that the timer is supposed to finish.
After that, we can add as many seconds as we want to, in order to start a new timer.
Have a look at the script:

alias: timer_plus_one_minute
mode: single
sequence:
  - service: timer.start
    data_template:
      duration: |
        {% if states.timer.countdown.state == 'active' %}
          {% set t = states.timer.countdown.attributes.finishes_at %} {% set f =
          as_timestamp(strptime(t, '%H:%M:%S')) %} {% set s = f-now().timestamp() %}
          00:00:{{ s | int + 60 }}
        {% else %}
          00:01:00
        {% endif %}
      entity_id: timer.countdown


2 Likes

I had some issues getting this to work, as strptime considered the finishes_at property an invalid input. It seems like that function overcomplicates things anyway, so I have resolved it like this:

- service: timer.start
  data_template:
    duration: |
      {% if states.timer.extractionfan.state == 'active' %}
        {% set FinishDate = states.timer.extractionfan.attributes.finishes_at %}
        {% set RemainingRuntimeSeconds = as_timestamp(FinishDate) - now().timestamp() %}
        00:00:{{ RemainingRuntimeSeconds | int + states('input_number.extractionfan_runtime') | int }}
      {% else %}
        00:00:{{ states('input_number.extractionfan_runtime') | int }}
      {% endif %}
    entity_id: timer.extractionfan

Thankyou, I used this code as my base to create and modify a script that I’m very happy with - but lost my way back to this thread! I just stumbled across it again so here is my code for my timer increment script. This method to increment was what got me out of the blocks so thanks again.

Some key points: I changed states.timer.countdown.attributes.finishes_at to state_attr('timer.ac_off_countdown', 'finishes_at') as recommended in HA documentation to avoid errors.

This will start at 30 minutes and increase until 12 hours before resetting to off.

This allows me to use a simply chip in lovelace to display the time remaining as needed and call this script on press.

I wrote a longer post about it here including the timer, script and lovelace examples.

alias: AC sleep increment or reset
sequence:
  - choose:
      - conditions:
          - condition: not
            conditions:
              - condition: state
                entity_id: timer.ac_off_countdown
                state: active
          - condition: not
            conditions:
              - condition: state
                entity_id: climate.izone_controller
                state: "off"
        sequence:
          - service: timer.start
            data:
              duration: "00:30:00"
            target:
              entity_id: timer.ac_off_countdown
      - conditions:
          - condition: not
            conditions:
              - condition: state
                entity_id: climate.izone_controller
                state: "off"
          - condition: template
            value_template: >-
              {{ (as_timestamp(state_attr('timer.ac_off_countdown',
              'finishes_at')) - now().timestamp()) < 39600 }}
        sequence:
          - service: timer.start
            data:
              duration: >
                {% set s = as_timestamp(state_attr('timer.ac_off_countdown',
                'finishes_at')) - now().timestamp() %} 00:00:{{ s | int + 1800
                }}
              entity_id: timer.ac_off_countdown
    default:
      - service: timer.cancel
        data: {}
        target:
          entity_id: timer.ac_off_countdown
mode: restart
icon: mdi:sleep

For future reference, it’s not obligatory to format the value of duration in HH:MM:SS but can be represented purely as seconds. Therefore the template can be reduced to:

- service: timer.start
  data:
    entity_id: timer.extractionfan
    duration: |
      {% set fa = state_attr('timer.extractionfan', 'finishes_at') %}
      {{ 0 if fa == none else (fa | as_datetime - now()).total_seconds() | int(0) + states('input_number.extractionfan_runtime') | int(0) }}
2 Likes

This is a great solution. Thank you.