Changing icon of Template Switch

Hello,

i made switch like this

      ac_display:
        friendly_name: "AC Display"
        turn_on:
        - service: script.ac_display
        turn_off:
        - service: script.ac_display  
        icon_template: >-
          {% if is_state('switch.ac_display', 'on') %}
            mdi:television-classic
          {% else %}
            mdi:television-classic-off
          {% endif %} 

But i got other icon image

then television-classic and television-classic-off.
Important that in case of template is the same switch switch.ac_display - is is the problem and how can it be solved?

Thanks a lot!

You donโ€™t have a value_template defined. Try:

      ac_display:
        friendly_name: "AC Display"
        value_template: "{{ is_state('switch.ac_display', 'on') }}"
        turn_on:
        - service: script.ac_display
        turn_off:
        - service: script.ac_display  
        icon_template: >-
          {%- if is_state('switch.ac_display', 'on')  -%}
            mdi:television-classic
          {%- else  -%}
            mdi:television-classic-off
          {%- endif -%} 

Icon is now ok, but the button if always off, goes not on (((

 ac_display:
        friendly_name: "AC Display"
        value_template: "{{ is_state('switch.ac_display', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.ac_display
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.ac_display
        icon_template: >-
          {%- if is_state('switch.ac_display', 'on')  -%}
            mdi:television-classic
          {%- else  -%}
            mdi:television-classic-off
          {%- endif -%} 

Pretty sure you canโ€™t use a self referencing value_template.

Think of it like this:

The switch is off, so the value_template = โ€œoffโ€.
You turn the Lovelace representation of the switch on.
The switch integration looks at the value_template to check the state of the switch and sees โ€œoffโ€. (The entity in the template isnโ€™t updated until the template is updated).
So the Lovelace switch representation is turned off.

You need an independent feedback sensor for your switch state.

You could use an input boolean for this.

I thought an optimistic mode had been added to the template switch but I donโ€™t see it in the docs.

This switch turns on/off script.

 ac_display:
        friendly_name: "AC Display"
        value_template: "{{ is_state('switch.ac_display', 'on') }}"
        turn_on:
          service: script.turn_on
          data:
            entity_id: script.ac_display
        turn_off:
          service: script.turn_off
          data:
            entity_id: script.ac_display
        icon_template: >-
          {%- if is_state('switch.ac_display', 'on')  -%}
            mdi:television-classic
          {%- else  -%}
            mdi:television-classic-off
          {%- endif -%} 

Maybe?

do not work(((

because if this i did not added โ€œvalue_templateโ€

If you add two services to the turn_on and turn_off commands or put the input_boolean on/off service in your scrips you can use an input_boolean in your value template.

You mean i have to create extra input_boolean for this switch?

Actually no. I just looked at your switch a bit closer. So your script runs permanently unless you turn it off?

Try this:

value_template: "{{ is_state('script.ac_display', 'on') }}"

no, script send RF comman to broadlink. I have already made so, it turns on, and then automatically off

Then why do you have this?

        turn_off:
          service: script.turn_off

That turns off your script. It does not call another script to turn off your display.

It sends command off to script when the switch is off

No. It turns off your script. I.e. stops a script from running. It wonโ€™t send anything to your display.

Ah i this case yes, but actually it does not matter, because it use only one command.
It is the display of AC, it toggles with one button on and off, because of this turn_on and turn_off leads to one script

Yeah I just looked at your first post. That is the correct way.

Do you have any way to get the state of the display?

Smart switch power consumption, pingable IP address, anything?

If not, you will, have to use an input_boolean. And this will get out of sync if you have any other way of controlling the display other than home assistant. An IR remote control for example.

no, because of IR controlled AC.

Of course its clear.
Could you please post a yaml, because i do not completly understand you what i have do with input_boolean

Create an input boolean called display_state. Use it like this in your switch:

      ac_display:
        friendly_name: "AC Display"
        value_template: "{{ is_state('input_boolean.display_state', 'on') }}"
        turn_on:
        - service: script.ac_display
        - service: input_boolean.turn_on
          data:
            entity_id: input_boolean.display_state
        turn_off:
        - service: script.ac_display  
        - service: input_boolean.turn_off
          data:
            entity_id: input_boolean.display_state
        icon_template: >-
          {%- if is_state('input_boolean.display_state', 'on')  -%}
            mdi:television-classic
          {%- else  -%}
            mdi:television-classic-off
          {%- endif -%}