Change icon on state change of other entity

I have an ESP8266 with a servo and a photoresistor with a digital out.

Both work fine Home Assistant can control the servo HA respond to light changes.

image
The HDMI state is the entity that reads which output is active on the switch so on or off means TV or Projector.

I have earlier got the following working with some help.

switch.sonoff_1000000000:
      friendly_name: Kaffekokaren
      templates:
        icon: mdi:coffee-maker
        icon_color: if (state === 'off') return ''; 
                    if (entities['counter.counter'].state == "0") return 'rgb(255, 0, 0)';
                    if (entities['counter.counter'].state == "1") return 'rgb(155, 100, 0)';
                    if (entities['counter.counter'].state == "2") return 'rgb(50, 200, 0)';
                    if (entities['counter.counter'].state == "3") return 'rgb(0, 255, 0)';
                    if (entities['counter.counter'].state == "4") return 'rgb(0, 155, 100)';
                    if (entities['counter.counter'].state == "5") return 'rgb(0, 50, 200)';
                    if (entities['counter.counter'].state == "6") return 'rgb(0, 0, 255)';
                    if (entities['counter.counter'].state == "7") return 'rgb(100, 0, 155)';
                    if (entities['counter.counter'].state == "8") return 'rgb(200, 0, 50)';

and this change the color of the icon.
I figured I could use the same with changing icon, so I tried the following:

script.1585736971040:
      friendly_name: HDMI_Switch
      assumed_state: false
      templates:
        icon: if (entities['binary_sensor.hdmi_state'].state === 'off') return 'mdi:projector';
              if (entities['binary_sensor.hdmi_state'].state === 'on') return 'mdi:television-classic';

I have tried with if, else, with === or == and switching place with on and off state. It will only shows as ‘on’.
Even when I can see the state in the frontend saying off, the HDMI switch icon is not changed.

What I’m doing wrong?

If you can change colour does not mean you can change anything else.
Have you tried search the forum?
AFAIK it’s not possible using standard HA customisation.
I bet you can do it using a custom card - template-entity-row or button-card, for example (there are some more here)

I have searched but didn’t find anything like, this works:.

But this is a binary sensor so it returns on and off states, when it’s off it looks like:

image

Compare that to the image above.
If I make it a device class window, I see a window that is open or closed.
It is possible to change the icon as far as I can see, at least for HA itself.

yes it is, but you asked if you can do that.
actually, I was wrong. If you want to change icon, use template_sensor - it has icon_template

you could also use the template-entity-row custom card. It allows you to template all of the typical entity options and then some. https://github.com/thomasloven/lovelace-template-entity-row#template-entity-row
Very useful.

I can not get that template working.
I first tried creating my own and that failed so then I figured I copy exactly as the example. But that doesn’t work either.
My try:

        icon_template: >-
          {% if is_state('binary_sensor.hdmi_state', 'on') %}
              mdi:projector
          {% else %}
              mdi:television-classic
          {% endif %}

The example that I cant get working either:

        icon_template: >-
          {% if is_state('sun.sun', 'above_horizon') %}
            mdi:weather-sunny
          {% else %}
            mdi:weather-night
          {% endif %}

What I get is a icon that I get every time I fail. I believe it’s a paper with lines written on it.

Interesting, but it uses the same templates that I can’t get working.
Maybe when I get it working

can you post here the whole config of your template sensor?

customizer:
  custom_ui: local

homeassistant:
 customize:
    script.1585736971040:
      friendly_name: HDMI_Switch
      assumed_state: false
      templates:
        icon_template: >-
          {% if is_state('sun.sun', 'above_horizon') %}
            mdi:weather-sunny
          {% else %}
            mdi:weather-night
          {% endif %}

or the other template i wrote above

it’s not a switch, is it?
and did you read my post?

I have not said it’s a switch. The name of the entity contains “switch”.
But that doesn’t matter since it’s not the script that it reads the state of, it reads the state of the HDMI_state state. That is two different entities, one is a script and one is a binary sensor.

And yes I did read, you wrote template_sensor and link to icon_template.
I read icon_template and could not find any template_sensor (nor can CTRL+F), so that is what I tried.

as far ask I can see in the original post

so for HA it is a switch.

It does matter as if you don’t clearly ask what you need it’s impossible to help.
As per this post.

Your taking stuff out of it’s context.
Reread the first post.

I clearly state that the switch is not what I’m asking since I have this working and it changes color, not the icon.
I figured if I say that the Sonoff switch code works then it may be useful to see a know working template of reading the stat of a different entity.

Below that there is a new section that clearly shows that it’s a script with the friendly name “HDMI Switch”.
In the same section you can also see that I’m trying to read the state of the entity “HDMI state” and use that to toggle the icon.

As I said, reread it all and I bet you will see that it’s all clear.

On the topic:
Template switch allows to change its icon and it’s documented.
You can change script’s icon but there is no icon_template or something similar so I don’t think it’s possible at all.

If you really want to display a different icon, you can use template switch or a custom card - template-entity-row, button-card etc.

Imho you need to start your post with a short and clear description of your problem and then add stuff that is kind of related but is not directly a part of it. Otherwise it’s “too long;didn’t read” or, as in this case, a confusion. But it’s up to you.

And if you are your topic like “How dynamically change icon of a script” that would’ve removed a lot of question from the start.