Toggle Switch does not swap icon

Created this simple switch template, that should switch the icon between up and down each time it is pressed.

But it only works the first time after a reset, what am I doing wrong ??

- platform: template
  switches:
    up_down_dir:
      turn_on:
      
      turn_off:

      icon_template: >-
        {% if states('switch.up_down_dir') == 'off' %}
          mdi:arrow-down-circle
        {% else %}
          mdi:arrow-up-circle
        {% endif %}
  1. you have no value template to determine the switch state.
  2. you have no actions to turn anything on or off.

Thanks, well I dont want it to do anything other than switch from up to down, then another button will check it’s state and use that to increment / decrement a value.

I added a value template and that broke it completely:

value_template: "{{ states('switch.up_down_dir') }}"

You can not self reference the template switch state. That’s like saying “hey switch are you on, to check see if you are on”. It is not providing any information.

You probably want an input boolean helper.

These do not have icon templates though. So if this is important to you, then what you do is create a template switch to monitor and control the input_boolean.

- platform: template
  switches:
    up_down_dir:
      value_template: "{{ states('input_boolean.up_down_dir')|bool }}"
      turn_on:
        - service: input_boolean.turn_on
          target:
            entity_id: input_boolean.up_down_dir
      turn_off:
        - service: input_boolean.turn_off
          target:
            entity_id: input_boolean.up_down_dir
      icon_template: "mdi:arrow-{{ 'up' if states('input_boolean.up_down_dir')|bool(0) else 'down'}}-circle"

Excellent, understand now.

Thank you so much

1 Like

Could you explain why using “bool” filter?
The “input_boolean” helper is on/off, same is “switch”.

The value_template has to resolve to true or false. At least that is what I assumed from memory and I see no contradiction here now that I have checked.

However states('input_boolean.up_down_dir') returns 'on' or 'off'.

The |bool filter converts common Home Assistant states like 'on' or 'off' to true or false respectively.

https://www.home-assistant.io/docs/configuration/templating/

The value_template may have changed to recognise the states 'on' or 'off'. There have been a lot of improvements lately and I have not tested this, but as noted above there is nothing in the value_template documentation to say that it does recognise these states. So “better safe than sorry”.

Sorry for a late reply.
I do not see on the provided link any info that the value is “true/false”.
Yes, it is written in many examples like:

value_template: "{{ is_state('switch.source', 'on') }}"

but this does not mean that an internal value is “true/false”.
Checking “Dev tools → Set state” shows this:


as well as “Dev tools → Template”.

Probably it used to be “true/false” some time ago.
I started learning HA on September 2020, created first switch may be at the end of 2020, it was “on/off” that time if I am not mistaken…

To know for sure I created the following two template switches:

- platform: template
  switches:
    test_on_off:
      friendly_name: "Test on-off"
      value_template: "{{ states('input_boolean.test') }}"
      turn_on:
        service: input_boolean.turn_on
        target:
          entity_id: input_boolean.test
      turn_off:
        service: input_boolean.turn_off
        target:
          entity_id: input_boolean.test
    test_true_false:
      friendly_name: "Test true-false"
      value_template: "{{ states('input_boolean.test')|bool }}"
      turn_on:
        service: input_boolean.turn_on
        target:
          entity_id: input_boolean.test
      turn_off:
        service: input_boolean.turn_off
        target:
          entity_id: input_boolean.test

Both worked as expected. So yes, the template will now recognise the states on and off and the |bool filter is not needed.

1 Like

Thanks a lot for the confirmation!