How can I debug how often a template sensor is updated?

Hi there,

I’m using a template sensor with this state template. The template sensor state and attribute should only be rendered once per hour.

   - name: "0 - Technik - Tibber Stunde"
      unique_id: "0_technik_tibber_stunde"
      attributes:
        timestamp: >
          {{ today_at(now().hour | string + ":00") | as_timestamp | int }}
      state: >
        {{ today_at(now().hour | string + ":00") }}

If I try this in the template development tab, HA tells me that this template is being updated once every minute.

However, if I check the sensor states development tab, it appears that the template sensor using this template is updated once per hour (which would be the desired behaviour).

So what’s correct?

How can I find out myself how often a template sensor’s template is actually being rendered to a value? Is there a log entry or some other information on this?

Please note that the code above is just an example for one template sensor I’m using. I have other template sensors with more complex behaviour.

To avoid unnecessary CPU cycles, I want to make sure these are only updated once per hour or once per day and now want to find out if I can rely on HA’s automatic detection of input state changes. Or if I need to use a time_pattern trigger to force the update on specific times of the day.

Thanks!

One simple way is to use an entities card with the “last updated” option:

image

If you have a number of these templates you could monitor them all in one place on a single card.

But is this the “last update” or the “last change” of the value?

If the template sensor above is called every minute, it will return the same, unchanged state value for 60 times.

You can have either.

Okay, these infos are also shown in the states development tab, see the column to the right:

So it looks like I’m getting the desired behaviour and the template is only rendered once per hour.

However, why does the template development tab update the template…

{{ today_at(now().hour | string + ":00") }}

…once per minute when this isn’t necessary? Is this just special development behaviour? See the note below the result block:

Any template that contains now() will update once per minute.

There’s an easier way. Template sensors support triggers. Use a time pattern trigger, e.g.

template:
  - trigger:
      - platform: time_pattern
        hours: "/1"
        minutes: 0
        seconds: 0
    sensor:
      - name: "0 - Technik - Tibber Stunde"
        unique_id: "0_technik_tibber_stunde"
        attributes:
          timestamp: >
            {{ today_at(now().hour | string + ":00") | as_timestamp | int }}
        state: >
          {{ today_at(now().hour | string + ":00") }}

Thanks. Then why does the state development tab tell me it updated only once per hour? Am I reading this wrong?

That’s not what it says for me:

The template in the development tab is updated every minute.

When I use the same template as a template sensor, the states development tab tells me the sensor is updated once per hour.

See my posting above with (German language) screenshots.

Unfortunately I can’t read German.

Either way, did you miss this?

Do that if you only want hourly updates.