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) }}
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.
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.
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).
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
Automation to have the fan be switched on/off based on the timer start, finish or cancel.
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.
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