Template sensor updates infrequently but Developer Tools shows updated data

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:
Screen Shot 2020-10-21 at 7.24.07 PM

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?

Ah, found the trick here: How to make template sensor with as_timestamp update regularly?

Instead of using

as_timestamp(now())

add the time & date platform and use this…

as_timestamp(states('sensor.date_time_iso'))

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.

Because the template uses now() which plays no part in periodically updating the template.

However, that will change in version 0.117. The now() function will cause the template to be evaluated every minute.

Until then, you can add this line to your original template:

{% set x = states('sensor.time') %}

Its presence within the template will cause the template to be evaluated every minute.

Be sure to configure sensor.time because it’s not included by default. It’s part of the Time & Date integration.

After upgrading to version 0.117, you can remove the line from your template.

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:

Templates without entities using now ()

I assume you are referring to the beta version (now at 0.117.0b1), because the release version isn’t due until next week.

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.

1 Like