Issues using state_attr

Hi all,
I’ve been searching around trying to find a solution to this, and not yet found one.
I apologise if there is a thread already.

I have been trying to create a template using sun.sun attribute elevation. I have read it is better to use state_attr(‘sun.sun’,elevation) instead of states.sun.sun.attributes.elevation . The issue is when I use state_attr I always get the value none.
In developer tools I put these two lines in.

{{ state_attr('sun.sun',elevation)|float }}
{{ states.sun.sun.attributes.elevation|float }}

The result is:


When using {{ states.sun.sun.attributes.elevation|float }} I get the correct value. When using {{ state_attr('sun.sun',elevation)|float }} I get 0.0 and if I remove |float it will come back always as “None”.

I can get the automation to work using states.sun.sun.attirbutes.elevation, but it has me puzzled why it does not work as state_attr.


Both the entity_id and attribute need to be in quotes.

{{ state_attr('sun.sun', 'elevation') }}

1 Like

Thanks. Will give that a try. I knew it had to be something silly I was missing.