Different sensor image based on its value

Hi all,

Im new here and fairly new to Home Assistant.

I’m trying to add a new sensor based on my Picnic sensor.
Based on it’s status and eta it will give the moment of delivery.

sensor:
  - platform: template
    sensors:
      tr_picnic_test:
        value_template: >
         {% set yesterdayE = as_timestamp(today_at('23:59') - timedelta(days = 1)) | int %}
         {% set todayS = as_timestamp(today_at('00:00')) | int %}
         {% set todayE = as_timestamp(today_at('23:59')) | int %}
         {% set deliS = as_timestamp(states('sensor.picnic_last_order_eta_start')) | int %}
         {% set deliE = as_timestamp(states('sensor.picnic_last_order_eta_end')) | int %}
         {% set status = states('sensor.picnic_last_order_status') %}
         {% set deliTime = as_timestamp(states('sensor.picnic_last_order_eta_start')) | timestamp_custom('%H:%M', true) + " en " + as_timestamp(states('sensor.picnic_last_order_eta_end')) | timestamp_custom('%H:%M', true) %}
         {% set deliTimeExt = as_timestamp(states('sensor.picnic_last_order_eta_start')) | timestamp_custom('%D tussen %H:%M', true) + " en " + as_timestamp(states('sensor.picnic_last_order_eta_end')) | timestamp_custom('%H:%M', true) %}
         
         {% if status == 'COMPLETED' and deliE < yesterdayE %}Gisteren geleverd
         {% elif status == 'COMPLETED' and deliE < todayE %}Vandaag geleverd
         {% elif status == 'CURRECT' and deliE > todayS %}Vandaag tussen {{deliTime}}
         {% elif status == 'CURRENT' %} {{deliTime}}
         {% else %}Geen actieve bestelling
         {% endif %}
        friendly_name: "Picnic TR status"

So far so good.
I however want to change the image of the sensor based on the status. How can I achieve that?

Just in the same way:


        value_template: |
           …
        icon_template: |
         {% set yesterdayE = as_timestamp(today_at('23:59') - timedelta(days = 1)) | int %}
         {% set todayS = as_timestamp(today_at('00:00')) | int %}
         {% set todayE = as_timestamp(today_at('23:59')) | int %}
         {% set deliS = as_timestamp(states('sensor.picnic_last_order_eta_start')) | int %}
         {% set deliE = as_timestamp(states('sensor.picnic_last_order_eta_end')) | int %}
         {% set status = states('sensor.picnic_last_order_status') %}
         {% set deliTime = as_timestamp(states('sensor.picnic_last_order_eta_start')) | timestamp_custom('%H:%M', true) + " en " + as_timestamp(states('sensor.picnic_last_order_eta_end')) | timestamp_custom('%H:%M', true) %}
         {% set deliTimeExt = as_timestamp(states('sensor.picnic_last_order_eta_start')) | timestamp_custom('%D tussen %H:%M', true) + " en " + as_timestamp(states('sensor.picnic_last_order_eta_end')) | timestamp_custom('%H:%M', true) %}
         
         {% if status == 'COMPLETED' and deliE < yesterdayE %} mdi:cat
         {% elif status == 'COMPLETED' and deliE < todayE %} mdi:dog
          …
          {% else %} 
          {% endif %}

Thank you for your reply.
That does work for icons, but not images??

{% if status == 'COMPLETED' and deliE < yesterdayE %} /local/images/picnic/delivered.png`

Ouuuups… seems that I was still sleeping…

It’s the same with images:


          …
          …
        entity_picture_template: |
          {% set l = states('light.test') %}
          {% if l == 'off' %} local/images/hearts.png
          {% else %} local/images/balloons.png
          {% endif %}

Source: Template - Home Assistant

1 Like