Make an icon change based on state

Similar to how door and window icons change based on their state, I want my refrigerator to change based on if it’s door is open or not. Specifically, I want the icon to change from mdi:fridge-variant-outline to mdi:fridge-variant. I still want the color to change to yellow as well if that matters. Where and how do I make this happen?

Would this work with the mushroom template card where the icon can be controlled by a template?

It can be done in different places.

In a card, like a markdown.

type: markdown
content: |-
  <center>
  {%-if (states('person.guest')=='home')-%}
    <font color="green"><ha-icon icon="mdi:home"></ha-icon></font>
  {%-elif (states('person.guest')=='not_home')-%}
    <font color="red"><ha-icon icon="mdi:home-outline"></ha-icon></font>
  {%-else-%}
    <font color="yellow"><ha-icon icon="mdi:help"></ha-icon></font>
  {%-endif-%}
  </center>

It can be done as a sensor template.

template:
  - sensor:
      - name: test
        state: "{{ states('person.guest') }}"
        icon: >-
          {% if (states('person.guest')=='home') %}
            mdi:home
          {% elif (states('person.guest')=='not_home') %}
            mdi:home-outline
          {% else %}
            mdi:help
          {% endif %}

This works great to make a sensor icon that changes based on state. This sensor icon also works when added to a picture elements card in lieu of the original entity, but then the color won’t change based on state, even if the icon does.

So i went back and replaced the sensor with the original entity, hoping that would change. Unfortunately, the original icon stays the same, regardless of state. Is it possible to change the original icon based on state, so that it works across the board everywhere, including changing color as needed?

As you said:

What kind of sensor do you use to represent your refrigerator:
– is it a sensor or a binary_sensor?
– is it a template sensor/binary_sensor or from some integration?

Binary sensors (with or w/o a defined “device_class”) have a native support of icon color dependently on a state (on/off).
Sensors do not have a similar icon color support (exception - “device_class: battery”).

If you need to define an icon dynamically:
– if this is a template sensor/binary_sensor - define an icon inside a template;
– if this is a sensor/binary_sensor from some integration - your only option seems to be using card-mod (a “–card-mod-icon” variable); also, you may create a template sensor/binary_sensor based on this entity with a templated icon - but you need to define same “device_class” (in case of “binary_sensor”) if present.

You’re absolutely correct, moved template to binary_sensor.yaml and it worked as requested.