Templating query - state.entity.attribute vs state_attr

Hi,

Maybe someone can help, just trying to determine difference between using state.entity.attribute and state_attr(‘entity’,‘attribute’)

For example given this:

action: notify.mobile_app_pixel_8
data:
  message: |-
    {{ states.input_boolean.testing.last_changed }}

    {{ state_attr('input_boolean.testing','last_changed') }}
  data:
    actions:
      - action: test
        title: please press me    
  title: Test

In the notification message I receive the correct timestamp for states.input_boolean.testing.last_changed but for state_attr I get “None”

Thanks!

The first one isn’t looking in the attributes dictionary: the comparison isn’t what your title suggests.

last_changed is a property of the State object, not one of its attributes. That’s why state_attr can’t find it.

The methods in your title are mostly equivalent when you are actually looking at attributes:

1 Like

Petro also wrote a very good explainer post on attributes v properties: Last_updated State and last-changed in Lovelace - #2 by petro

2 Likes

Thanks all, very helpful! :smiley: