Display sensor last_reported in entities card

Trying to display state.last_reported on Entities card.

type: entities
entities:
  - entity: sensor.airvisual_carbon_dioxide
    secondary_info: last-changed
  - type: custom:template-entity-row
    icon: mdi:update
    name: last-reported
    state: "{{ relative_time(states.sensor.airvisual_carbon_dioxide.last_reported) }}"

That nearly works.
But the value displayed is updated only when the sensor state is updated. While sensor state remains the same, last-reported on the screen does not change (but changes if I refresh the page with F5).

What am I doing wrong?
How can I make last-reported displayed?

relative_time() seems to work differently than last-changed/last-updated shown on Entities card.
AFAIK relative_time is automatically updated every minute*.

*it was explained before in Docs - but then was removed due to plans to deprecate “relative_time()”; apparently it seems that this intention was cancelled, but Docs were not restored.

related issue

1 Like

Thanks for your answer.
Seems like I posted a working example. :rofl:

More complex one:

type: entities
entities:
  - entity: sensor.airvisual_pm2_5
    secondary_info: last-changed
  - type: custom:template-entity-row
    name: last-changed
    state: "{{ states.sensor.airvisual_pm2_5.last_changed | relative_time }}"
  - type: custom:template-entity-row
    name: last-reported
    state: "{{ relative_time(states.sensor.airvisual_pm2_5.last_reported) }}"
  - type: custom:template-entity-row
    name: last-changed
    state: "{{ states.sensor.airvisual_pm2_5.last_changed }}"
  - type: custom:template-entity-row
    name: last-reported
    state: "{{ states.sensor.airvisual_pm2_5.last_reported }}"

изображение

Line 1: common sensor value with last-changed secondary info.

Line 2: last_changed with relative_time from template.
Works, but not localized and updated not the same time as the first line, so sometimes values are different (on screenshot: 7 minutes in line 1 and 8 minutes in line 2).

Line 3: last_reported with relative_time from template.
Works, but not localized and updated not the same time as the first line.

Line 4: last_changed from template.
Updates only when sensor state is changed - OK.

Line 5: last_reported from template.
Updates only when sensor state is changed - NOT OK.

So, the questions.

  1. Is there any way to show relative_time the same way as standard secondary_info (localized, updating on the same time)?

  2. Why last_reported from template is not updated at all? Is that a bug?

I do not know about possibility to update relative_time() more frequently than once a minute (although you may press F5 more frequently which is not a solution).
That is why I am avoiding using relative_time() for showing last-changed/updated (means - template-entity-row) when I need to see constantly updating last-changed/updated.

As for “last-reported” - I missed a moment when this attribute became exposed.
It was added first to “states” but was not exposed for some period.
What is your use-case, why do you want this attribute to be shown?

Please advice any other approach so I can see the values updated (and localized).

I see my sensor value with comment “updated 3 hours ago”.
Is value the same for 3 hours?
Or is my sensor broken (connection lost)?
With “last-reported” shown near “last-changed” I do have an answer.

A standard entity row.
Actually, the only thing about “relative_time” is that it is updated once a minute.
Imho it is not a big deal - just press F5 to update it.

If last-updated is “3 hours ago” - then probably the last record in DB is from this moment of time.
Open a DB by something like DB browser for SQLite.
Also check logs; may be you also need to enable debug messages for this integration in Logger.

How can I display last_reported with standard entity row?

Not localized and updated not on the same schedule.
Technically “works”, but bad looking.

Go to SQL tables and check debug logs every time? Seriously?
What for do we need dashboards if we can check SQL and logs?

Seems to be a bug?

You cannot. This attribute is not supposed to be used by average users.

Well, you said you got issues with an integration - I suggested you to check logs and DB, you are asking “seriously?”.

There seems to be a little missunderstanding.
If I do have problems with integration, I have plenty of ways to see what is happening.

Speaking about normal day. I look on my dashboard and see:

  1. My temperature sensor value, changed 5 minutes ago - OK, everything works fine.

  2. My CO2 sensor value, changed 18 hours ago - ALSO OK, I am sure something is wrong and need to investigate (normally the value changes a little every several minutes).

  3. My PM2.5 sensor value, changed 6 hours ago - NOT OK.
    May be the value is really constant for several hours.
    May be there is a problem with integration - I do not know.
    I want to see if there is a problem with integration without checking DB and logs every time I doubt.
    One of the ways to see this: display last_reported value. May be, there is some better approach?

In general: if you doubt in something and trying to investigate - logs are first place to check. Next step - enabling debug messages for this integration and probably checking DB.
I assume all these sensors you are talking about are not template, they are provided by some integration (AirVisual?)?

Also, you may create a FR to allow displaying last-reported in Entities card and other places in UI (tiles, glance etc)

Last reported updates at a frequency that is different than last_changed. Both of those templates will update when the state (or any attribute in the state object) changes.

The template engine only listens to state changes and attribute changes. Properties on the state object do not cause the templates to update. last_changed, last_reported, etc are all properties, they are not attributes or the main state.

The only reason last_changed updates with a state update is because they come together at the same time.

1 Like

The 4 ways templates update:

  1. If there is an entity’s state is accessed in a template, it will update when attributes or the state updates.
    {{ states.sensor.abc }} 
    {{ states('sensor.abc') }}
    {{ state_attr(...) }}
    # etc any other function that access a entity_id or it's attributes.
    
  2. If a domain is accessed, the template will update when all of the domains state’s and attributes update. However it will update at most once per second.
    {{ states.sensor }}  # no entity_id provided, only this object is used
    
  3. If all states are accessed, the template will update when all states or attributes chage. However it will update at most once per minute.
    {{ states }}  # no domain provided, only this object is used
    
  4. If now(), relative_time(), or today_at() are used in the template, the template will also update on the minute mark.
    {{ now() }} 
    {{ relative_time(...) }} 
    {{ today_at(...) }} 
    

there are no other ways that templates currently update.

None of these “Update ways” react to properties on state objects.\

TLDR:

No this is not a bug, this is how template updates work.

1 Like

I always doubt. :slightly_smiling_face:
Yes, I am an engineer, and I can read database and debug logs.
But why not having this information on dashboard instead, so one can immediately see the sensor value and if the value can be trusted?
For example, display relative_time(last_reported).
Or more complex: check last_reported with some script and display alert if the state was not reported for more than an hour.

There is one already: Add last-reported to secondary_info. May be it will be implemented some day, but I need to do something now, not after several months or years.

Great thanks for the explanation how it works.
From user point of view I definitely call this a bug: template contains value (last_reported), the value is updated, the template is not updated.
Even if this behavior is the one expected and hard to change. :roll_eyes:

The value is updated though. It’s a moment in time. It’s static, it does not change. The frontend does this nifty thing where it calculates the difference between the timestamp (that hasn’t changed) and the current time.

When you use a template, the frontend loses that context and you’re held to updates provided by the template. Those were described above.

If that was an entity instead of a template, you’d see a correct relative time in the UI. You’d have to make this into a trigger based template sensor, with an update frequency that matches your reported at frequency (or less than that).

1 Like

It’s not hard to change and the eye roll is unnecessary.

Trying to sum up the discussion.

  1. Standard row secondary_info does not support last_reported. There is a FR for this support.

  2. Template with last_reported is not updated when last_reported changes. That is how templates work. Only sensor state change or F5 updates the value on screen. From my point of view this behavior is strange, but it is how it works.

entities:
  - type: custom:template-entity-row
    name: last-reported
    state: "{{ states.sensor.airvisual_pm2_5.last_reported }}"
  1. Template with relative_time function. Works (updated once a minute), but:
  • the value displayed is not localized, always in english;
  • update schedule is different from the one in standard rows;
  • so, the dashboard card as a whole looks inconsistent.
entities:
  - type: custom:template-entity-row
    name: last-reported
    state: "{{ relative_time(states.sensor.airvisual_pm2_5.last_reported) }}"
  1. Configure trigger based template sensor. Works, but:
  • still not localized and seems no way to update on the same schedule as standard rows;
  • tried to use state_reported event as trigger, but failed: as described here, an event_filter must be used, but I could not find any docs how to do it.
template:
  - trigger:
      - trigger: time_pattern
        seconds: "*"
    sensor:
      - name: airvisual_pm2_5_last_reported
        state: '{{ relative_time(states.sensor.airvisual_pm2_5.last_reported) }}'
entities:
  - entity: sensor.airvisual_pm2_5
    secondary_info: last-changed
  - entity: sensor.airvisual_pm2_5_last_reported
    icon: mdi:update
    name: Last reported

So, formally my task is solved. But no good solution.

You can use easy_time to generate localized relative_times assuming it supports your language.

1 Like

Thanks a lot!
I liked relative_time_plus even more. Localization issue is solved.

relative_time() is used in jinja-supporting cards like template-entity-row, is it possible to support higher frequency? For instance, if an optional “every_15_seconds” argument is defined - then a frequency is 15 sec (with no choice to select other frequencies).

There is a similar plugin from TheFes.