Template for Companion App Widget - sensor last update does not work when changing sensor entity?

Hi,
I managed to make a nice template to visualize a temp/humidity sensor and get the time since last change as the 4th row (hand writing in picutre) as a widget in my app


<center>
<strong>HB153</strong><br>
<span style="color:#3fbaf1;">
<big><big><strong>{{states('sensor.ute_trh_temperature')|float|round(1)}}°C</strong>°</big></big><br>
<big><strong>{{states('sensor.ute_trh_humidity')|float|round(1)}}%RH</strong></big><br>
<small>{{ relative_time( states.sensor.ute_trh_temperature.last_changed ) }}</small>
</center>
</span>

image

The sensor names are from my RTXtrx, that unfortunately have shown to have a really crappy receiver, so I have just sent it for warranty exchange.
Now I’m back on the same sensors, but from integration of my Telldus Tellstick Z-Net lite v2.
Sensors have other entity names and when I update entity names, like this:

<strong>HB153</strong><br>
<span style="color:#3fbaf1;">
<big><big><strong>{{states('sensor.5_hb153_temperature')|float|round(1)}}°C</strong>°</big></big><br>
<big><strong>{{states('sensor.5_hb153_humidity')|float|round(1)}}%RH</strong></big><br>
<small>{{ relative_time( states.sensor.5_hb153_temperature.last_changed ) }}</small>
</span>

Both temperature and humidity works with those sensor entity names, but not time since last change…and I can’t figure out why. In the widget template, the update button goes greyed out when I change entity name for last_changed

When I paste it the code without formatting into template in dev tools it looks like this:


and I can’t see I made any typo error while updating code…

If I remove last line, sensor entities works in HA dev template as well as in app:

I’m a beginner in coding - please help

Look at the states tab in the developer tools and then look up your sensor to see what attributes that are available.

Aha…so different sensors have different attributes

It looks like this:

Then I thougt it should be time_last_updated instead of last_change

But that doesn’t work either:

Try …


{{ states.sensor.ute_trh_temperature.last_changed | relative_time }}

it has to be sensor.5_hb153_temperature (sensor.ute_trh_temperature is the entity for the RFXtrx I’m replacing during it’s repair/replacement)

Tried

{{ states.sensor.5_hb153_temperature.last_changed | relative_time }}

{{ states.sensor.5_hb153_temperature.time_last_updated | relative_time }} 

It took me…just a few hours…to google and make it work with the entity for the RFXtrx…how can it be that difficult for the Telldus integration…?

Edit:
And info on both last change and last update looks to be available…somewhere:

{{states('sensor.5_hb153_temperature.time_last_updated') | relative_time}}

gives:


Still not right, but no syntax error and it says it’s listens for what I found in the dev tools states tab…

I think it could be to do with the digit (5) at the start of your entity id. Can’t see what else would be wrong. Does the template work with a different entity?

Its not a state, but a state_attr

{{ state_attr('sensor.5_hb153_temperature','time_last_updated') }}

Doesn’t work either…

but what is state_attr?

If you want to use the entity’s built in attribute, as above would work. But all entities can use last_changed or last_updated in the format;

{{ states.sun.sun.last_changed }}

When OP tried this it resulted in unknown- perhaps because you actually want last_changed instead of last_updated. But the template was valid.

Also note the is it last_updated, not time_last_updated which is a specific attribute of the entity in question.

Put a space after the ,

state_attr is for referencing a state attribute.

I have one sensor entity without a digit in start of the name for the Telldus (use digits to sort them as telldus app has no sort function)

Tried these:

{{ state_attr(‘sensor.viking_light_temperature’,‘time_last_updated’) }}
``` -> TemplateSyntaxError: unexpected char '‘' at 14

{{states(‘sensor.viking_light_temperature.time_last_updated’)


```{{states('viking_light_temperature.time_last_updated') | relative_time}} 
```-> Result type: string unknown
{{ state_attr(‘sensor.5_hb153_temperature’ , ‘time_last_updated’) }}

tried space before, after and before&after the comma, but still doesn’t work ->TemplateSyntaxError: unexpected char ‘‘’ at 14

Try removing and readding your quotes. Looks like something is up with them given the error. Replace with freshly typed single quotes or double quotes. Both should work.

Also - note that for the other entity you tried, this would only work if that entity as a specific ‘time_last_changed’ attribute. states.sensor.name.last_changed works with all entities.

{{ state_attr('sensor.5_hb153_temperature' , 'last_changed') }}

→ Result type: dict
null
This template listens for the following state changed events:

  • Entity: sensor.5_hb153_temperature

tried both time_last_updated and time_last_changed and without time_ for both

All entities used is the same type of sensor and they can show last_update and last_chage in lovelace cards.

I did not remember to mark the text as preformatted text, so your browser changed some of the characters.
Try this on.

{{ state_attr('sensor.5_hb153_temperature','time_last_updated') }}

Now you nailed it!!!

Result type: string

2023-02-08 10:18:24

This template listens for the following state changed events:

  • Entity: sensor.5_hb153_temperature

Then it would be nice to get rid of the date…or have just time since update…

Nice!
Data+time fits!
image

@WallyR Thanks! You’re always here helping me out!

Fenty178 tried to explain the difference between state and state_attr “state_attr is for referencing a state attribute”, but I don’t understand.
For the same sensor connected via RFXtrx-integration, one code worked with state, but with Telldus integration it became state_attr.

Dev menu States tab says:
state_class: measurement
battery_level: 100
time_last_updated: 2023-02-08 11:06:03
unit_of_measurement: °C
assumed_state: true
device_class: temperature
friendly_name: HB153 Light/Temp

regarding this sensor. As the RFXtrx is for service/replayce and that integration is inactivated, I can’t see what it looks like for those sensors right now.

Not needed for function…but would be good for learning and understanding!

Glad you got it working!

I suggest you read this - https://www.home-assistant.io/docs/configuration/templating/

If you’ve already read it, read it again… and again. I have read many times in order to get my head around it from a starting point as an enthusiastic novice.

To specifically answer your question, your entity has a range of attributes (battery_level, assumed_state etc.). One of them is time_last_updated which is handy for you given that’s the data you were looking for.

However, other entities will have different attributes. For example, sun.sun does not have an attribute for time_last_updated, but you can still use the native feature for all entities of calling last_changed or last_updated.

This should work


{{ state_attr(‘sensor.5_hb153_temperature’ , ’time_last_updated’) }}

So should this


{{ states.sensor.5_hb153_temperature.last_updated }}

This will work


{{ states.sun.sun.last_updated }}

But this won’t

{{ states.sun.sun.time_last_updated }}

And neither will this because time_last_updated isn’t an attribute of the entity sun.sun

{{ state_attr(‘sun.sun’ , ’time_last_updated’) }}

Beyond getting the datetime result of the template, you can then get into a whole other world of datetime formatting and filters. In this case, you should be able to use something like the following and format the date/time to your preference.

{{ as_timestamp(state_attr('sensor.5_hb153_temperature', 'time_last_updated')) | timestamp_custom("%H:%M %d/%m") }}

Hope that helps a bit with understanding. I’m by no means an expert myself!

You are not easy to make happy. :smiley:

{{ as_timestamp(state_attr('sensor.5_hb153_temperature','time_last_updated'),0)|timestamp_custom('%H:%M') }}

This shows hours (%H) and minutes (%M) with 2 digits.
To have other formats use this webpage: Formatting Dates and Time

The 0 in the line mean the value will default to 0 when the sensor is unavailable or unknown.