Unable to set a icon_template

Hi,

I try to make my icon_template works, it works in devtool template preview tab but when I put the code in configuration.yaml it throws an error

Invalid config for [sensor]: [icon_template] is an invalid option for [sensor]. Check: sensor->icon_template. (See /config/configuration.yaml, line 151).
I tried many things but do not figure out why it does not work.
I tried to set a variable to store sensor value to make it more readable but don’t knoww if we can set vars in icon_template.

I tried to format yaml with vscode yaml extension but the same.

mqtt:

  sensor:

    # **** MQTT Shelly Flood Battery ****

    - name: "MQTTT Shelly Flood Battery"

      state_topic: "shellies/shellyflood-987654321edcba/sensor/battery"

      unit_of_measurement: "%"

      device_class: battery

      icon_template: >

        {% if is_number(states('sensor.shelly_flood_987654321edcba_battery')) and states('sensor.shelly_flood_987654321edcba_battery') | float == 100 %}

            mdi:battery

        {% elif states('sensor.shelly_flood_987654321edcba_battery')|float <= 15 %}

            mdi:battery-outline

        {% else %}

            mdi:battery-{{(states('sensor.shelly_flood_987654321edcba_battery')|float / 10)|round*10}}

        {% endif %}

Device_class battery handles the icon and you don’t need the template.

Also, mqtt sensors don’t support an icon template, so you have to rely on the device class setting.

1 Like

Ok, Thank you !