Help with a Template Sensor (icon template)

I’ve created a template sensor that shows the sun elevation, which works fine (top part in the code below).

Now I’m trying to change the icon depending on the position of the sun, but am having trouble formatting it correctly. This is what I have so far, but it doesn’t work.

sun_elevation_custom:
  friendly_name: "Sun Elevation"
  value_template: '{{ states.sun.sun.attributes.elevation }}'
  unit_of_measurement: '°'
  icon_template: >
    {% if is_state('states.sun.sun.attributes.elevation' > 10 ) %}
        mdi:weather-sunny
    {% elif is_state('states.sun.sun.attributes.elevation' < 10 ) and is_state('states.sun.sun', 'above_horizon' ) %}
        mdi:weather-sunset-down
    {% elif is_state('states.sun.sun.attributes.elevation' < 3 ) and is_state('states.sun.sun', 'above_horizon' ) %}
        mdi:weather-sunset
    {% else %}
        mdi:weather-sunset-up
    {% endif %}

I'm new to templating and just cobbled this together from looking at other examples (so it's no surprise to me that this doesn't work).

Any one know what I need to change in order to get it working properly?

Hi @jono, you can play with templates in Dev Tools/Templates.

{% if states.sun.sun.attributes.elevation > 10 %}
    mdi:weather-sunny
{% elif states.sun.sun.attributes.elevation < 10 and states.sun.sun.attributes.azimuth < 180 %}
    mdi:weather-sunset-up
{% elif states.sun.sun.attributes.elevation < 10 and states.sun.sun.attributes.azimuth > 180 %}
    mdi:weather-sunset-down
{% else %}
    mdi:weather-sunset
{% endif %}
1 Like

That’s great, thanks a lot!

I tried playing around with it in Dev Tools/Templates, but still couldn’t manage it with my limited knowledge :sweat_smile: