Tested the following code in Developer Tools and it works as expected. When setup as a sensor template it update very infrequently. The data is stale for long periods of time.
{{ (( as_timestamp(now()) - as_timestamp(states.sensor.accuweather_current_conditions.last_changed)) / 60) | round(0) }} min ago
{{ (( as_timestamp(now()) - as_timestamp(state_attr("automation.climate_update_accuweather_conditions", "last_triggered"))) / 60) | round(0) }} min ago
- platform: template
sensors:
accuweather_current_conditions_updated:
friendly_name: "Accuweather Current Conditions Last Updated"
value_template: "{{ (( as_timestamp(now()) - as_timestamp(states.sensor.accuweather_current_conditions.last_changed)) / 60) | round(0) }} min ago"
accuweather_current_conditions_updated_auto:
friendly_name: "Accuweather Current Conditions Last Updated"
value_template: '{{ (( as_timestamp(now()) - as_timestamp(state_attr("automation.climate_update_accuweather_conditions", "last_triggered"))) / 60) | round(0) }} min ago'
Result of the templates in the dashboard:
The last screenshot is a little misleading as it appears as if that data is fresher but itās been stuck with those values for 20+ minutes.
I would like to setup something that will be up to date whenever I refresh the page or at least every minute or so. Maybe Iām going about it the wrong way. Do the templates only get updated when the underlying sensor states get updated? I could add an automation to update it every minute or so but that seems like the wrong approach. I must be missing something.
Re-read the doc. Seems the automation route is the correct way to do it.
SENSOR STATE UPDATES
The template engine works out what entities are used to trigger an update of the sensor and recalculates the result when one of those entities change.
If you use a template that depends on the current time or some other non-deterministic result not sourced from entities, create an interval-based automation that calls the service homeassistant.update_entity for the sensor requiring updates. See the example below.
In this scenario though if I already have an automation thatās updated the underlying sensor itself on a schedule what is the best way to show a ālast updated X min agoā value on a dashboard for either the sensor itself or the automation?
When the time & date sensor updaters (which I imagine is at least every minute, maybe even more frequently) it will cause the template to get re-evaluated.
Thanks for confirming what I found. Hilarious timing as 0.117 is supposed to come out today (or maybe tomorrow my time). Oh well, at least I have it working for tonight and can clean it up tomorrow.
What you found is old news and is documented here:
Wasnāt claiming what I found was new. I just didnāt know about it just as I donāt know about a lot of things in HA being Iām new and there are a lot of features. An even better doc reference for this is probably here which gives deeper details and examples: Template - Home Assistant
I assume you are referring to the beta version (now at 0.117.0b1), because the release version isnāt due until next week.
I was watching this and he said it was released today but didnāt mention it was just the beta in the video. Iāll be waiting for the full release.
The link I provided contains a link to the same material. Effectively itās the ācover pageā, providing a brief explanation why a template containing only now(), wonāt update.
Releases typically occur every 3 weeks. Version 0.116 was released October 7. One more week to go for version 0.117. Then, if all goes according to plan, your template will update periodically with just now() and the linked documentation will be removed.