Last_updated State and last-changed in Lovelace

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

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

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

states.binary_sensor['0x00158d000273bbe4_contact'].last_updated

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

Hello,

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

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.