Help with last changed template

Is there a way to write this:

states.switch.lounge_dehumidifier.last_changed

In this format (this is wrong):

state_attr('switch.lounge_dehumidifier', 'last_changed')
1 Like

I have played with this earlier and my feeling is the latter code you can use only for additional (i.e non-core) attributes of any state/object. And last_xxx, object_id etc are all part of the base Entity object and require a direct access to them.
Haven’t looked into the HA code yet, it should be obvious I presume.

Could you tell why the former is not suitable?

If the entity doesn’t exist the first will throw an error. The second just returns ‘unknown’.

That’s true. Is that the only issue you’d like to avoid?

Yes it is.

how about

states.switch.lounge_dehumidifier and states.switch.lounge_dehumidifier.last_changed

or

{% if states.switch.lounge_dehumidifier is not none %}
  {{ states.switch.lounge_dehumidifier.last_changed }}
{% endif %}

(as I don’t know anything about your context)?

Anyway, from reading this it’s obvious why the state_attr approach doesn’t work.
So basically if you want your template code to work, just check that the entity does exist and only then access its last_changed, that’s it.

And as it’s most likely Jinja2 template, after reading the Implementation section on Variables I’d use something like

{% set device = 'lounge_dehumidifier' %}
{% if states.switch[device] is defined %}
  {# it's safe to access the last_changed attribute #}
  {{ states.switch[device].last_changed }} 
{% endif %}

or

{% set device = 'lounge_dehumidifier' %}
{% if states.switch | attr(device) %}
  {# it's safe to access the last_changed attribute #}
  {{ states.switch[device].last_changed }} 
{% endif %}

I re-take this topic because documentation says:

Avoid using states.sensor.temperature.state, instead use states('sensor.temperature'). It is strongly advised to use the states(), is_state(), state_attr() and is_state_attr() as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).

How can I write states.switch.luz_led_cocina.last_changed in the recommended format?

{{ state_attr('switch.luz_led_cocina', 'last_changed') }} doesn’t work

1 Like

Last changed isn’t an attribute. It’s a property of the object. So in that case you have to use:

states.switch.luz_led_cocina.last_changed

Petro wrote a great post on this:

For anyone trying to do this in 2023: This is the only answer to getting last_changed into a template. I’ve been looking for 3 hours and this is the only way. Save yourself some time and ignore the documentation that says not to access entities like this.

4 Likes

The documentation only recommends accessing the state and attributes with specific functions, not object properties.

1 Like

Can you at least see how somewhat casual users can see that as a distinction without a difference? I realize there’s a lot of nuance here, and I wasn’t attacking you, I just want to help users like me who don’t have a double major in Jinja and Home Assistant.

Sorry if I came off wrong

Edit to add: understand what users in this thread are saying, and then we see these answers a year old. I’m simply updating the timestamp for people.

2 Likes