SENSOR entity_picture_template

have a sensor that I convert from hours to percent, that works as far as all goes well.
But I would like to have different icons for the states.

and I would need something that I the sensor value of 1 percent to 5 percent indicates an icon, etc …

Pellets Aschelade 800h Brenndauer ist wenn Aschelade LEER!

  • platform: template
    sensors:
    asche_sensor:
    entity_id: sensor.asche_sensor
    value_template: ‘{{100-((states. sensor.verbleibende_heizstunden_bis_zur_asche_entleeren_warnung.state | int) / 800 * 100) | int }}’
    unit_of_measurement: ‘% voll’
    friendly_name: Asche Füllstand
    #device_class: battery
    entity_picture_template: >
    {% if is_state(‘sensor.asche_sensor’, ‘4’) %}
    ‘/local/icons/asche/asche_leer.png’
    {% endif %}

how can i edit this line the HA displays me the same icon of 1-5 percent?

Thank you for your effort! :wink:
{% if is_state(‘sensor.asche_sensor’, ‘4’) %} ???

Not sure i understand what you want.
Show the same entity_picture if the state of the sensor is 1-5?
{% if states('sensor.asche_sensor') | int <= 5 %}
should do it.

Thanks for your code but the sensor does not work anymore and in HA unknown is displayed.

Fri Feb 15 2019 19:02:32 GMT+0100 (Mitteleuropäische Normalzeit)
Update for sensor.asche_sensor fails
Traceback (most recent call last):
File “/usr/src/app/homeassistant/helpers/entity.py”, line 221, in async_update_ha_state
await self.async_device_update()
File “/usr/src/app/homeassistant/helpers/entity.py”, line 347, in async_device_update
await self.async_update()
File “/usr/src/app/homeassistant/components/sensor/template.py”, line 215, in async_update
setattr(self, property_name, template.async_render())
File “/usr/src/app/homeassistant/helpers/template.py”, line 140, in async_render
return self._compiled.render(kwargs).strip()
File “/usr/local/lib/python3.6/site-packages/jinja2/asyncsupport.py”, line 76, in render
return original_render(self, *args, **kwargs)
File “/usr/local/lib/python3.6/site-packages/jinja2/environment.py”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “/usr/local/lib/python3.6/site-packages/jinja2/environment.py”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “/usr/local/lib/python3.6/site-packages/jinja2/_compat.py”, line 37, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
TypeError: ‘<=’ not supported between instances of ‘int’ and ‘str’

Can you post the code of your sensor once again?
And please format the code as described in the blue box on top.

platform: template
sensors:
  asche_sensor:
   entity_id: sensor.asche_sensor
   value_template: '{{100-((states. sensor.verbleibende_heizstunden_bis_zur_asche_entleeren_warnung.state | int) / 800 * 100) | int }}'
   unit_of_measurement: '% voll'
   friendly_name: Asche Füllstand
   entity_picture_template: >
     {% if states('sensor.asche_sensor') | int <= '5' %}
       '/local/icons/asche/asche_leer.png'
     {% endif %}

Well, the template in your first post ('4') shows me that the sensor returns a string.

With states('sensor.asche_sensor') | int , we convert this string to a number.

Now we want to compare that number <= 5, but when you put a number in quotes, you create a string.
→ TypeError: ‘<=’ not supported between instances of ‘int’ and ‘str’

So remove the quotes around 5 and it should work.
Like i wrote it in my first post.

  entity_picture_template: >
    {% if states('sensor.asche_sensor') | int <= 5 %}
      /local/icons/asche/asche_leer.png
    {% endif %}

Also the quotes around the picture path are not needed, i think.

1 Like

Thank you mery much, it works very well!

1 Like