I would like to create a sensor which tracks the last time the vacuum ran, which is not affected by restarting Home Assistant. So whenever the vacuum did its cleaning routine and ends the routine with the state ‘returning’ I want it to update the timestamp of a new sensor which I can use as secondary info entity.
I thought I once saw an integration (sensor) which could track the state changes of an entity and create a timestamp sensor out of it, but I can’t find it anymore.
Easily done. Just create a new input.datetime helper then call the set_datetime service in your automation. I do a similar thing for tracking when the dog was last fed.
Name of the entity. Based on friendly_name attribute with fall back to object ID. Example: Kitchen Ceiling.
state.last_updated
Time the state was written to the state machine. Note that writing the exact same state including attributes will not result in this field being updated. Example: 2017-10-28 08:13:36.715874+00:00.
state.last_changed
Time the state changed. This is not updated when there are only updated attributes. Example: 2017-10-28 08:13:36.715874+00:00.
state.attributes
A dictionary with extra attributes related to the current state.
So whenever you use this function:
state_attr(entity_id, ‘attribute’), you are only getting items inside the state.attributes. When you look at the states page, you will only see things that are inside the state.attributes section. But ALL state objects in home assistant have the fields/properties that are listed above. And they are OUTSIDE the attributes field/property.
So how do you access them? Well the only way to access them is through the state object itself. There is no method that will get you these items.
{{ states.domain.object_id.<field> }}
So if you have a light, and it’s entity_id is light.living_room, access any of the properties outside the attributes (or state), would be:
{{ states.light.living_room.entity_id }} # for the entity_id 'light.living_room'
{{ states.light.living_room.domain }} # for the domain 'light'
{{ states.light.living_room.object_id }} # for the object_id 'living_room'
{{ states.light.living_room.name }} # for the friendly_name 'Living Room'
{{ states.light.living_room.last_updated }} # for the datetime object in UTC when it was last updated
{{ states.light.living_room.last_changed }} # for the datetime object in UTC when the state last changed
So you can kinda start to see how where we can access the state and attributes using that method too. You’ve probably seen this many times before without realizing it.
{{ states.light.living_room.state }} # for the state
{{ states.light.living_room.attributes.xxx }} # for the attribute 'xxx' if it exists