Icon template

Hi,

I’m trying to use a dynamic icon template for a template sensor. The config is the following:

- platform: template
    sensors:
      multisensor_chambre_batterie:
        value_template: '{{ states.zwave.multisensor_chambre.attributes.battery_level }}'
        friendly_name: Batterie
        unit_of_measurement: '%'
        icon_template: >
          {% if states.zwave.multisensor_chambre.attributes.battery_level|int < 20 %}
            'mdi:battery-alert'
          {% else %}
            'mdi:battery'
          {% endif %}'

In the frontend, there is no icon displaying. If I paste the template in the templates tools (developers tools), it displays ‘mdi:battery’.

What’s wrong?

Can you try removing the quotes from the icon.

I already tried. Still not working.

Try putting a dash (-) after/before the % in the statement. That cleans up the spaces in the jinja code. I find more consistent results with dashes:

icon_template: >
          {%- if states.zwave.multisensor_chambre.attributes.battery_level|int < 20 -%}
            mdi:battery-alert
          {%- else -%}
            mdi:battery
          {%- endif -%}

You also have a quote at the endif which seems odd.

1 Like

Yeah, so I had 3 problems: removed the dashes and the lose quote. Then I had to add the dash in the jinja code.

Thanks!