Add time to running timer

Here we go. This is what I have for a script now and it’s working exactly like I want. Thanks a ton for the help!

add_screentime_calli_script:
alias: Calli Screentime - Add 10 minutes
sequence:

  • data:
    entity_id: timer.screentime_calli
    service: timer.pause
  • data_template:
    duration: >-
    {% set t = state_attr(‘timer.screentime_calli’, ‘remaining’).split(’:’) %}
    {% set ts = (t[0]|int * 3600) + (t[1]|int * 60) + t[2]|int + 600 %}
    {{ ‘{:02d}:{:02d}:{:02d}’.format(ts // 3600, (ts % 3600) // 60, (ts % 3600) % 60) }}
    entity_id: timer.screentime_calli
    service: timer.start

I don’t think that matters as long as you ensure the template will only be evaluated when the timer is paused and it will then only have fractions included.

Here is the new working template:

{% set x = state_attr('timer.garage_lights_timer', 'remaining') ~ '-0000' %}
{% set y = strptime(x, '%H:%M:%S.%f%z') %}
{{ (as_timestamp(y)|int + 15) | timestamp_custom('%H:%M:%S', false) }}

Glad to hear it.

Perhaps not quite the way you envisioned it would work (need to pause the timer first then do some gymnastics with the remaining attribute). The end-result isn’t super-pretty but, most importantly, gets the job done.

Not trying to be negative but we might as well get this out of the way here…

In the future you really need to post your code in the correct format.

It helps us see if you have syntax errors.

Here is one way to do it:

Just trying to be helpful (again…:grinning:) because someone will tell you that next time if you don’t do it correctly.

Here’s the script but formatted and using the third offered solution for doing the time arithmetic:

add_screentime_calli_script:
  alias: 'Calli Screentime - Add 10 minutes'
  sequence:
  - service: timer.pause
    entity_id: timer.screentime_calli
  - service: timer.start
    data_template:
      entity_id: timer.screentime_calli
      duration: >-
        {% set r = state_attr('timer.screentime_calli', 'remaining') ~ '-0000' %}
        {% set t = strptime(r, '%H:%M:%S.%f%z') %}
        {{ (as_timestamp(t) + 600) | timestamp_custom('%H:%M:%S', false) }}

I tested it using one of my own timers and it works well.

2 Likes

Hi Taras, I know this is a bit old, but I appreciate what you’ve done here. I’m trying to do the same thing. I have it set up, but when I run the script I get the following error:

[140520736539168] Error rendering data template: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 389, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1358, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

My code is:

  increment_goodnight_timer_up:
    sequence:
      - service: timer.pause
        entity_id: timer.goodnight_countdown

      - service: timer.start
        data_template:
          entity_id: timer.goodnight_countdown
          duration: >-
            {% set r = state_attr('timer.goodnight_countdown', 'remaining') ~ '-0000' %}
            {% set t = strptime(r, '%H:%M:%S.%f%z') %}
            {{ (as_timestamp(t) + 60) | timestamp_custom('%H:%M:%S', false) }}

Any idea what I’m missing? My guess is it’s related to the float issue, but I will admit I don’t know where to start with resolving it.

I know this is old, but I came across this while searching and thought I’d update with how I got this done (without having to pause the timer).

 - if:
  - condition: state
    entity_id: timer.pb3_timer
    state: active
then:
  - service: timer.start
    data_template:
      entity_id: timer.pb3_timer
      duration: >-
        {% set f = state_attr('timer.pb3_timer', 'finishes_at') %}
        {% set x = (as_datetime(f) - now()).total_seconds() + (30*60) %} 
        {{ x  | timestamp_custom('%H:%M:%S', false)}}
else:
  - service: timer.start
    data:
      duration: "00:30:00"
    target:
      entity_id: timer.pb3_timer
6 Likes

Thanks for this, worked a treat :slight_smile:

Hi everyone,

Again, I know this is an old thread but though I’d share my “UI only” solution, that didn’t require any templating. My use case is a bathroom fan, and I want to have it turn on when you press a button, running for 10 minutes, and add 10 minutes every time you press the button (up to a maximum of 1 hour).

  1. Timer helper. Nothing special here - it doesn’t actually matter what I set the count to as I will change it in the script. My entity ID is timer.bathroom_fan_timer

  2. Automation to have the fan be switched on/off based on the timer start, finish or cancel.

  3. Script to run when the button is pressed (it’s set to Queue mode). The key here is in the conditional, it first starts the timer with the max of 1 hour then immediately reduces it by 50 minutes, to get to 10 minutes - but allowing subsequent presses to add 10 minutes up to the max of 1 hour.

The only thing that isn’t quite how I would like it is that the script leaves the timer default set to 1 hour. This means if you activate the timer from the UI, it will leave the fan on for an hour (i’d prefer it to default to 10 minutes). I could probably get around this by doing something like upon finish/cancel, start the timer briefly at 10minutes then cancel it, but I decided not to bother.

Screenshots below.

Hope this helps others!

James

Image of Automation step 2:

image of Script step 3:

Expanded image of the first conditional step in the Script

1 Like

Hmm. I can’t extend a timer beyond it’s current “duration”. So I can’t take a 1 minute timer and add 3 minutes to it. I’d have to stop and restart.

That’s right, when you first start the timer you specify the duration - you can’t adjut it to higher than that initial value in subsequent calls. That’s why I start it first at 60 minutes then reduce it by 50 minutes: that gives mea timer set to 10 minutes that i can increase up to 60