When the timer is running, the finshes_at attribute exists. However, when paused or finished, the attribute no longer exists. The template must be able to gracefully handle the case where the attribute isn’t available.
Try this:EDIT Does not work as desired; see explanation several posts below.
alias: 'example 1'
trigger:
- platform: template
value_template: >
{% set f = state_attr('timer.your_timer', 'finishes_at') %}
{{ f != none and (as_timestamp(f) - now().timestamp()) | int in [10, 30, 60] }}
action:
- service: notify.persistent_notification
data:
title: Attention
message: >
There are {{ (as_timestamp(state_attr('timer.your_timer', 'finishes_at')) - now().timestamp()) | int }} seconds remaining.
Im getting an error atm with it. Saying the format tag is undefined.
Logger: homeassistant.helpers.template
Source: helpers/template.py:1346
First occurred: 12:10:23 PM (4 occurrences)
Last logged: 12:10:35 PM
Template variable error: 'f' is undefined when rendering '{{ f != none and (as_timestamp(f) - now().timestamp()) | int in [10, 30, 60] }}'
I just realized that the example will not work for the desired time intervals (all are under a minute).
A listener will be assigned to the one identifiable entity in this template, namely the timer:
{% set f = state_attr('timer.your_timer', 'finishes_at') %}
{{ f != none and (as_timestamp(f) - now().timestamp()) | int in [10, 30, 60] }}
That means the template is evaluated only when the timer’s state changes which, of course, won’t occur while its counting down because its state simply remains active.
At best, the template’s inclusion of now() means the template will be evaluated once a minute (on the minute). However, that’s a 1-minute time resolution which is too coarse for the desired purpose.
You would have to include something in the template that causes it to be evaluated every second. The problem is, if you do that, the template will continue to be evaluated every second even when the timer is not active.
I can’t think of an elegant way to meet your requirements. One could use a cascading series of timers (each with a duration set to your desired intervals) with automations to make them work in sequence and fire off notifications … but that seems like a terrible kludge.
Hopefully some else can come up with something more clever.
Ah okay… Well I appreciate the effort you put into it so far. Why would this be such a chore anyways? Not sure why there can’t be ab attribute that counts in real-time.
This automation triggers when the timer starts then loops every second (only while the timer is active). In each loop, it calculates the remaining time and checks if it matches 10, 30, or 60 seconds. If it does it sends a notification.
What’s important here is that this repeated looping every second only occurs while the timer is active. It stops when the timer is idle or paused. In other words, it’s reasonably efficient and uses no resources while the timer is not active.
Man…Thanks so much for your time and work. I’m working on trying this out right now and hoping one day I can be much proficient with templates. Taking python this semester in school and it’s been helping me understand the syntax better in HA.
If the automation I posted resolves your original problem, please consider marking my post (above) with the Solution tag.
It will automatically place a check-mark next to the topic’s title which signals to others that your problem has been resolved. It will also place a link below your first post that leads to the Solution post.
All of this helps other users find answers to similar questions.
Hey, sorry iv been so busy with school with my last week and exams I got around to trying it but it still does not show the notification on the TV. I edited your code to fit my entities and it seems all correct, so not sure whats going. My notification is using the android tv notification app. The initial message when it starts works.
I’m unfamiliar with the AndroidTV notification integration. Have you confirmed it works correctly by testing it in Developer Tools > Services? Try something simple like: