Last_updated State and last-changed in Lovelace

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)}}

as_timestamp already gives you seconds – you can just subtract.

UNIX timestamp is the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970. Therefore, if used as a function’s argument, it can be substituted with a numeric value (int or float ).

{{ as_timestamp(states.device_tracker. iphone_2.last_changed) - (3*60) }}

If you also meant you want the result as a datetime, do this:

{{ (as_timestamp(states.device_tracker.iphone_2.last_changed) - (3*60)) | timestamp_local }}
1 Like

I don’t mean to commandeer this, just wanted to ask a quick question before opening a new thread.

Has anyone used any of these states (last updated, especially) in Appdaemon apps? I keep trying to call them in the app, but I keep getting an error that last_updated is not found. Documentation doesn’t have anything about last_updated, so not sure if I can

I’m at work so I have to find the code, but I believe the variable is

porch_motion = binary_sensor.porch_motion
motion = self.last_updated(porch_motion)

Hi,

First of all, thank you for this great theme/support! WOW
Unfortunately I’m a newbie and have a small question.

I want to track the times my child had a bottle and show it in a chip, like here:
But instead of showing two times the bottles she druk I want the last entity to be the last_changed value of the counter. (preferably like this for example: 2h35)

Thx in advance!!

CleanShot 2022-02-01 at 14.08.26 2

FYI: the counter is updated with a tilt sensor :wink:
and resets itself every day.

Welcome to the community.

Unfortunately, chips are not standard in home assistant. If that is your frontend, we will need more information about how you got to that point. Mainly, what custom cards are you using to make that current setup?

1 Like

Thank you!!!
Here is my code showing the counter

The second entity should be replaced by the last_changed (if possible) :slight_smile:

- type: "custom:button-card"
  template: chip_icon_double_state
  variables:
    ulm_chip_icon_double_state_icon: "🍼"
    ulm_chip_icon_double_state_entity_1: counter.aantal_flessen
    ulm_chip_icon_double_state_entity_2: counter.aantal_flessen

You’ll have to make a new custom button card template that grabs the last_updated attribute.

Ow ok, thought this would be simple by using some text strings or something like that :slight_smile: but will try showing it in a different way. Thx!

It worked with this

  - platform: template
    sensors:
      bonnie_melk_time:
        friendly_name: "Bonnie Melk Time"
        value_template: >
          {{ as_timestamp(states.counter.aantal_flessen.last_changed) | timestamp_custom(' %H:%M',true | int) }}

that has some syntax errors

{{ as_timestamp(states.counter.aantal_flessen.last_changed, now().timestamp()) | timestamp_custom('%H:%M', true) }}

Thx Petro, this also works! :slight_smile:

2 Likes

What a great thread, I just need to understand the “Note that writing the exact same state including attributes will not result in this field being updated” bit.
I use “Rest” to make an API call to a sensor miles away which only updates itself randomly.
If it hasn’t been updated, then the API returns exactly the same JSON as before, the state hasn’t changed and neither has the attributes but “state.last_updated” is updated.
Is this because I made a fresh API call?