How to animate an icon inside a picture-elements card?

I would like to rotate the icon when ‘on’. But so far it only changes colour, what am I doing wrong?

type: picture-elements
elements:

  • type: state-icon
    entity: binary_sensor.nibecompressor
    icon: mdi:fan
    style:
    hui-generic-entity-row:
    $:
    state-badge:
    $:
    ha-icon:
    $: |
    ha-svg-icon {
    color: {{ ‘#FDD835’ if is_states(binary_sensor.nibecompressor) == ‘on’ else ‘#44739E’}};
    animation:
    {% if states(binary_sensor.nibecompressor) == ‘on’ -%}
    rotation 2s linear infinite;
    {% else %} null
    {% endif %}
    }
    @keyframes rotation {
    0% {
    transform: rotate(0deg);
    }
    100% {
    transform: rotate(360deg);
    }
    }
    top: 63%
    left: 36%

@nuiler next time you post your code use three backticks (also called backquotes which are above the left tab key and to the left of the number 1 key on your keyboard) before and after your code. It will display your code in the format the you’re using it. Below is an example.

type: picture-elements
elements:
  - type: state-icon
	entity: binary_sensor.nibecompressor
	icon: mdi:fan
	style:
	  hui-generic-entity-row:

I can’t be sure of the issue until you post your code so I could see it format but below it the correct way to accomplish what you want.

type: picture-elements
image: (don't forget your background image)
style: |
  ha-card {
    {% if is_state('binary_sensor.nibecompressor','on') %}
    --state-color: #FDD835;
    --state-animation: rotation 2s linear infinite;
    {% else %}
    --state-color: #44739E;
    {% endif %}
    }
  @keyframes rotation {
    0% {transform: rotate(0deg)}
    100% {transform: rotate(360deg)}
    }
elements:
  - type: state-icon
    state_color: false
    entity: binary_sensor.nibecompressor
    icon: mdi:fan
    style:
      top: 63%
      left: 36%
      transform: translate(0,0)
      position: absolute
      '--paper-item-icon-color': var(--state-color)
      animation: var(--state-animation)
1 Like
type: picture-elements
image: http://192.168.178.57:8123/local/nibe.jpg
style: |
  ha-card {
    {% if is_state('binary_sensor.nibecompressor','on') %}
    --state-color: #FDD835;
    --state-animation: rotation 2s linear infinite;
    {% else %}
    --state-color: #44739E;
    {% endif %}
    }
  @keyframes rotation {
    0% {transform: rotate(0deg)}
    100% {transform: rotate(360deg)}
    }
elements:
  - type: state-icon
    state_color: false
    entity: binary_sensor.nibecompressor
    icon: mdi:fan
    style:
      top: 63%
      left: 36%
      transform: translate(0,0)
      position: absolute
      '--paper-item-icon-color': var(--state-color)
      animation: var(--state-animation)
´´´



Unfortunately, it does not work for me with these entries.

What error are you getting because it works for me?

It looks right except for the image: which should be…

image: /local/nibe.jpg

And don’t forget to use the three backticks at the end of the code. You used the single quotes. It’s a common mistake that I make all the time.

Card mod thread → 1st post → link at the bottom → styles for picture elements

Hi. I’m trying to figure out how to have multiple icons with their animations triggered by different entities. Your code works great with one icon. I tried duplicating the ha-card part but don’t really have the skills. Do you have any ideas?

@cappadanna I’m a little confused. Do you mean one card that shows multiple icons for separate entities and that trigger based on their state or do you mean one card that will show and trigger a different icon based on the state of separate entities? And sorry if my question seems even more confusing. I’m just trying to figure out exactly what you’re trying to do.

If you’re trying to add multiple icons for multiple entities you need to add additional style and element code for them. For example if adding two more entities you add them under elements: and then also add additional style code with a different name for the --state-color:, --state-animation:, and rotation and use whatever parameters you want for each one. Be sure to change all instances of them accordingly. Also change the location under top: and bottom: so they do not sit on top of one another. Try adding one before you add two. Less chance of writing the wrong code and then easier to troubleshoot. (Hopefully I don’t make any errors when I added the notation for you.)

type: picture-elements
image: (don't forget your background image)
style: |
  ha-card {
    {% if is_state('binary_sensor.nibecompressor','on') %}
    --state-color: #FDD835;
    --state-animation: rotation 2s linear infinite;
    {% else %}
    --state-color: #44739E;
    {% endif %}

    {% if is_state('binary_sensor.nibecompressor','on') %} (change to second entity)
    --state-color-1: #FDD835; (change name)
    --state-animation-1: rotation-1 3s linear infinite; (change both names)
    {% else %}
    --state-color-1: #44739E; (use changed name from above)
    {% endif %}

    {% if is_state('binary_sensor.nibecompressor','on') %} (change to third entity)
    --state-color-2: #FDD835; (change name)
    --state-animation-2: rotation-2 4s linear infinite; (change both names)
    {% else %}
    --state-color-2: #44739E; (use changed name from above)
    {% endif %}
    }

  @keyframes rotation {
    0% {transform: rotate(0deg)}
    100% {transform: rotate(360deg)}
    }

  @keyframes rotation-1 { (change name)
    0% {transform: rotate(0deg)}
    50% {transform: rotate(90deg)}
    100% {transform: rotate(360deg)}
    }

  @keyframes rotation-2 { (change name)
    0% {transform: rotate(180deg)}
    100% {transform: rotate(360deg)}
    }

elements:
  - type: state-icon
    state_color: false
    entity: binary_sensor.nibecompressor
    icon: mdi:fan
    style:
      top: 63%
      left: 36%
      transform: translate(0,0)
      position: absolute
      '--paper-item-icon-color': var(--state-color)
      animation: var(--state-animation)

  - type: state-icon
    state_color: false
    entity: binary_sensor.nibecompressor (change to second entity)
    icon: mdi:fan (change icon if necessary)
    style:
      top: 63% (adjust so they're not on top of each other)
      left: 46% (adjust so they're not on top of each other)
      transform: translate(0,0)
      position: absolute
      '--paper-item-icon-color': var(--state-color-1) (change name accordingly)
      animation: var(--state-animation-1) (change name accordingly)

  - type: state-icon
    state_color: false
    entity: binary_sensor.nibecompressor (change to third entity)
    icon: mdi:fan (change icon if necessary)
    style:
      top: 63% (adjust so they're not on top of each other)
      left: 46% (adjust so they're not on top of each other)
      transform: translate(0,0)
      position: absolute
      '--paper-item-icon-color': var(--state-color-1) (change name accordingly)
      animation: var(--state-animation-1) (change name accordingly

Thank you so much, I meant multiple icons with different entities. I didn’t understand that I had to use different names for the state-color etc. but now it makes perfect sense. Thanks for great explanation!

thanks for helping the community.

For me it’s not working.
maybe I am overlooking something.

I took your code and now it is:

type: picture-elements
image: local/ioniq_test.png
style: |
  ha-card {
    {% if is_state('binary_sensor.ioniq_5_ev_battery_charge','off') %}
    --state-color: #FDD835;
    --state-animation: rotation 2s linear infinite;
    {% else %}
    --state-color: #44739E;
    {% endif %}

  @keyframes rotation {
    0% {transform: rotate(0deg)}
    100% {transform: rotate(360deg)}
    }

  @keyframes rotation-1 { (change name)
    0% {transform: rotate(0deg)}
    50% {transform: rotate(90deg)}
    100% {transform: rotate(360deg)}
    }

  @keyframes rotation-2 { (change name)
    0% {transform: rotate(180deg)}
    100% {transform: rotate(360deg)}
    }
    
elements:
  - type: state-icon
    state_color: false
    entity: binary_sensor.ioniq_5_ev_battery_charge
    icon: mdi:ev-plug-type2
    style:
      top: 63%
      left: 36%
      transform: translate(0,0)
      position: absolute
      '--paper-item-icon-color': var(--state-color)
      animation: var(--state-animation)

nothing is happening sadly

You now need to use card_mod:

  - type: picture-elements       
    card_mod:
      style: |
        ha-card {
1 Like