WTH No "template safe" way of getting last_updated, last_changed from an entity

I see in https://github.com/home-assistant/core/pull/39382 that the use of entities: in templates has been deprecated. The usual trick of using now() in a template combined with sensor.time in the entity_id: will eventually no longer work. Everyone who used this method will have to find a different way to get a datetime string.

Traditionally sensor.time is the default used in the documentation. It only produces time in the format of HH:MM. If I want to get a python datetime string out of it I can do so indirectly with states.sensor.time.last_changed. However we are not supposed to use state objects directly in templates because if it is referenced before the entity is available the template will fail to render and generate an error in the system log. However, I don’t know of any template safe is_state, is_state_attr(), states(), state_attr() equivalent to get last_updated or last_changed.

I haven’t encountered any actual problems using the state object directly in some circumstances, but the documentation standards say we shouldn’t use it in examples, but what other options do we have to get last_updated, or last_changed?

Would it be worth it to implement something like state_obj(‘sensor.time’,‘last_changed’) and is_state_obj(‘sun.sun’,‘domain’,‘sun’)?

Some methods to accomplish this have been discussed here, none of which require a datetime object.

Unless you need a datetime object and then …
… The World is your Molusc.
:smile:

The method would be called ‘is_state_prop’ or some variation of that. This is because you are accessing state object properties, not an attribute. No need for more than 1 method.