How to use "time_in_state" or similar attributes in HA?

I would like to build some automations based on sensor attributes such as “time_in_state” or “last_changed”, but I can’t get any value from e.g.

{{ states.binary_sensor.garage_door_detector_sensor.attributes.time_in_state }} or
{{ states.binary_sensor.garage_door_detector_sensor.attributes["time_in_state"] }}
  1. Where can I find available attributes for a sensor?
  2. How to use it properly as a trigger or condition in HA?

Thanks

Check the states tab, it has all states and the attributes for each entity listed.

binary_sensor.garage_door_detector_sensor

value_index: 0
value_instance: 1
value_id: 72057594076463104
old_entity_id: binary_sensor.garage_door_detector_sensor_2_0
new_entity_id: binary_sensor.garage_door_detector_sensor
friendly_name: Garageporten
device_class: opening
icon: mdi:window-open
custom_ui_state_card: state-card-custom-ui
show_last_changed: true

The thing is, I can get HA to show “last changed” in a state-card, but when I try the above in the Template tab, it shows nothing.

Did you get this sorted? I’m trying to do the same using the London Underground sensor and then have my google home read the description which is an attribute. I can make it work with the main state but not the attribute.

The use case doesn’t matter, it’s the same need as you mention hence I’m interested in whether you resolved.

No, I still haven’t found out how to get hold of that attribute. It seems odd that this attribute shouldn’t be accessible for automations.

So I did finally make this work. It seems you have to create a separate sensor to extract the attribute. And then you refer to that new sensor in the automation itself - it worked fine once I had done that. Surprising but it does work.

That is interesting! Could you please give an example of how you did?

Thanks!

Ok, solved this now. I can use e.g.

{{ now() - states.binary_sensor.garage_door_detector_sensor.last_changed }}