Newbie needs help with setting an entity to have icons based on state

I’m pretty much brand-new to Home Assistant. I’ve had years and years of programming experience and a number of years working with other home automation products. I’ve read through a number of posts so far on setting an entity’s icon based on state. What I’d like is for my garage cover to have mdi:garage when closed and mdi:garage-open when open. I’d also like to have a virtual switch of mine to use mdi:lock when off and mdi:lock-open when on and another (physical) switch to use mdi:water-pump when on and mdi:water-pump-off when off.
I’ve tried sussing out Templates and can’t quite get it to work. Is anybody doing this sort of thing? (To be honest, if I can’t figure it out, I can live with the color changing by state which I’ve already got working.)

You’ll want to use icon_template for this. To play around with the code, use the Developer Tools accessible from the left hand menu and then go to Template. This gives you a playground to figure out syntax for Jinja2 as well as links to the documentation.

Does this help or were you already this far?

1 Like

I had already figured out to use templates, but I seem to have missed the example you linked to which had an actual cover. That’s helpful.
It also looks like I have to code a separate template for each individual entity. (I don’t see a way to code a general case for multiple entities.)
I fiddle with the example and see if I can get it to work. Thanks.

It worked! Here’s what I did (in configuration.yaml) (see below)
The only odd thing is that the standard ]<>[ and ]><[ icons are now up-arrow and down-arrow, the template and all functionality work. Anyone have a clue on how to set those auxiliary icons?
Thanks overkill32 for the link.

cover:
  - platform: template
    covers:
      garage:
        value_template: "{{ is_state('cover.gocontrol_smart_garage_door_controller', 'open') }}"
        open_cover:
          service: cover.open_cover
          data:
            entity_id: cover.gocontrol_smart_garage_door_controller
        close_cover:
          service: cover.close_cover
          data:
            entity_id: cover.gocontrol_smart_garage_door_controller
        icon_template: >-
          {% if is_state('cover.gocontrol_smart_garage_door_controller', 'open') %}
            mdi:garage-open
          {% else %}
            mdi:garage
          {% endif %}