Last update from zigbee2mqtt sensors

Hi,
Trying to get entity last update, everything I found doesn’t help. I have Xiaomi sensors that work through zigbee2mqtt and MQTT integration.

When trying to get a template like this:

{{ states.sensor.0x00158d000315a681_temperature.last_changed }}

I get error:

invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'x00158d000315a681_temperature') for dictionary value @ data['template']. Got '{{ states.sensor.0x00158d000315a681_temperature.last_changed }}\n{{ states.sensor.cpu_temperature.last_changed }}'

It works with other sensors, e.g. CPU core temperature. Meanwhile UI on device shows the last update time, so it is stored somewhere.
Do you have a clue on where the issue could be or maybe there is a topic already that I haven’t found?

Try this:

{{ states.sensor['0x00158d000315a681_temperature'].last_changed }}

I think the leading 0x in the entity’s object_id poses a problem (ambiguity) when you use dot notation. The example I provided uses bracket notation to clarify and eliminate any ambiguity about the meaning of the leading 0.

Thanks, it helped!

1 Like