Change Button icon based on binary sensor state

Hi,

I would like to change the icon of this button card based on the state of some other binary sensor (so not the state of this switch).

What would be best practice to do so?

Look into creating a template sensor entity:
Template - Home Assistant (home-assistant.io)

Here’s an example

template:
  - binary_sensor:
      - name: Back Door
        unique_id: backdoor
        state: "{{ is_state('binary_sensor.kitchendoorlock', 'on') }}"
        icon: |
          {% if is_state('binary_sensor.kitchendoor', 'on') %}
          mdi:door-open
          {% elif is_state('binary_sensor.kitchendoorlock', 'off') %}
          mdi:lock-outline
          {% else %}
          mdi:lock-open-outline
          {% endif %}

You can hide the original entity, if you want, so you don’t see duplicates.

1 Like

Thank you, I think this is the right direction but the documentation always gives me trouble. So the “switch.gate_1” and “binary_sensor.gate_1_bin” entities are already there because I use MQTT autodiscovery.
I then fail to see how to change the icon belonging to card with name “Poort 1” (controlling entity “switch.gate_1”) based on the state of “binary_sensor.gate_1_bin”.
I think my issue is that in your backdoor example I don’t get how it’s linked to a certain card.
Something like this maybe:

template:
 - binary_sensor:
     - name: Poort 1
       state: "{{ is_state('binary_sensor.gate_1_bin', 'on') }}"
       icon: |
         {% if is_state('binary_sensor.gate_1_bin', 'on') %}
         mdi:gate-open
         {% elif is_state('binary_sensor.gate_1_bin', 'off') %}
         mdi:gate-closed
         {% else %}
         mdi:gate-closed
         {% endif %}

Got it to work using

  - platform: template
    switches:
      gates:
        value_template: "{{ is_state('switch.gate_1', 'on') }}"
        availability_template: "{{ not is_state('switch.gate_1', 'unavailable') }}"
        turn_on:
          service: switch.toggle
          target:
            entity_id: switch.gate_1
        turn_off:
          service: switch.toggle
          target:
            entity_id: switch.gate_1
        icon_template: >-
          {% if is_state('binary_sensor.gate_1_bin', 'on') %}
            mdi:gate
          {% else %}
            mdi:gate-open
          {% endif %}

Thanks for your help.

1 Like