Last_updated State and last-changed in Lovelave

Groups has nothing to do with lovelace, nor do they do anything with the interface. The used to do things with the interface back in 2018. Since then, they’ve been completely separate.

It seems that there is a bug when parsing sensor name:
Test with template in Developer Tool
{{ states.binary_sensor.0x00158d000273bbe4_contact.last_updated }}

Got this:

Error rendering template: TemplateSyntaxError: expected token 'end of print statement', got 'x00158d000273bbe4_contact'

Right, now I am converting groups.yaml, customize.yaml into new ui-lovelace.yaml and get rid of those old style.

Thats because your object_id starts with a number, which is a no-no. To access it…

{{ states.binary_sensor['0x00158d000273bbe4_contact'].last_updated }}

Thank you so much!

I got Unknown error when trying to convert timezone,

         {{ as_timestamp( states.binary_sensor['0x00158d000273bbe4_contact'].last_updated + '+06:00') | timestamp_custom('%a %b %-d %-I:%M%p')}}

Unknown error rendering template


This printed a UTC date time, but converting like above having error.

last updated is a datetime object. You can’t add a string to it.

{{ as_timestamp( states.binary_sensor['0x00158d000273bbe4_contact'].last_updated) | timestamp_custom('%a %b %-d %-I:%M%p', False)}}


I’m looking for to show the last changed of the sensor, but the “timestamp_custom” doesn’t work. This is my conf :

{{ states.binary_sensor.porte_entree.last_changed | timestamp_custom('%H %M',False) }}

The result :

2019-11-20 08:57:18.122862+00:00

The custom format isn’t apply (I just want 08:57), and, the local time isn’t correct, because I need to add 1 hour in more ( either 2019-11-20 09:57:18.122862+00:00 and when I try with the timestamp_local the time is incorrect ).

Do you have some idea why ?

I found a solution, I’m not sure is the good solution :

{{ as_timestamp(states.binary_sensor.porte_entree.last_changed) | timestamp_custom('%H %M',True)  }}

It is, if you look at the template I posted 14 days ago, you’ll notice that it uses as_timestamp()

i am trying to get the last changed attribute with:

{{ states.sensor.[‘0007da4989ac6f_state’].last_changed }}

but got this error:

TemplateSyntaxError: expected name or number

any idea?

you are using incorrect quotes. Notice how the quotes used in my template are not fancy and curly?

thanks for your answer.

i guess thats just a copy paste error as it seems to be right in the ha ui.

its working when i rename the entity from 0007da4989ac6f_state to a0007da4989ac6f_state so that there is no number at the beginning… but it should also work with the number … any ideas?

remove the . between sensor and [

1 Like

thats it… awesone! thanks!!
where can i find such things in the documentation?

It’s actually fairly basic programming syntax for many modern programming languages: The . is a way to reference values through fields. The [some_key] is a way to access a dictionary value. You can use both but if your key looks like a number or starts with a digit you must use the latter syntax. In other words, you can’t use both . and [] next to each other.

There is a basic example in the docs – right at the end. It’s basic because it isn’t really specific to HA.

1 Like

Hi guys,

the topic’s name seems quite close match to what I am looking for so let me put it here…

I have a sensor that reads some numbers and it is valid if a new number is exactly the same as an old number.
However that reading may never happens because of failed backend, so I need to know when it was updated last time (or how may seconds ago).

state.last_updated seemed to be exactly what I was looking for, but… “Note that writing the exact same state including attributes will not result in this field being updated.

Any ideas how to overcome that?

Thank you.

P.S. Can I use “attributes”, like supplying some random stuff as an attribute to trigger “last_updated” parameter to be refreshed?

I have a mqtt sensor that reads this string form mqtt:
{"date":"2021/03/21", "time": "09:39:12", "value": "393.0"}
so at least time is always different .
The sensor is:

  - platform: mqtt
    state_topic: "homeassistant/power"
    name: Power
    expire_after: 30
    value_template: "{{ value_json.value }}"
    unit_of_measurement: W

Can you tell me please how to extract those “additional attributes” from mqtt?

Thank you

Hi, I have the below template working but I want to substract 3 min to the result. Can you help me to write the new template? I only receive error messages

{{ as_timestamp(
    states.device_tracker.iphone_2.last_changed) | timestamp_custom(' %H:%M',
    true | int)}}