Template confusion for state changed

Homeassistant noob here,

I’m going crazy trying to work with template sensors. :upside_down_face:

I’ve got several Xiaomi binary door sensors integrated into my setup.
I would like to see the last opended state in my lovelace frontend.

The following is not displaying any value:

       door_last_open:
         friendly_name: 'Door opended last'
         value_template: "{{  states.sensor.my_sensor.last_changed }}"

Neither is the following:

     states('sensor.my_sensor.attributes['last_changed']')

Is there an issue I’m not seeing here using these sensors?

Related: is there a way to debug this kind of sensors / attributes without editing the configuration.yaml file each time & restarting? That would really be useful

You can debug templates in the Template Editor. It’s in the Developers Tools area. E.g., put {{ states.sensor.my_sensor.last_changed }} in the editor and see what it says.

This wouldn’t work because of incorrect quote usage, incorrect usage of the states() function, and last_changed is not under attributes:

     states('sensor.my_sensor.attributes['last_changed']')

You could write it this way (although the dot notation is more usual):

     state.sensor.my_sensor['last_changed']

As far as not working, the first example looks ok, but it’s only partial. I’d have to see the full context. You may have an indentation error or something like that.

Finally! I got it thanks to the debugging expression.

It was the wrong category (i.e. had to be binary_sensor)!

1 Like