Change battery icon in custom button card

I have this custom button card and want to change the battery icon color and icon itself based on its batterylevel but i can’t find it. I have been looking around for hours and came this far.
I’m already happy with the card but i would love to have the ability to change the icon’s color and even when possible the icon itself.

type: custom:button-card
entity: binary_sensor.lenovo_tab_m10_plus_3rd_gen_plugged_in
name: Tablet
custom_fields:
  Batterij: |
    [[[ 
     return `<span>Batterij: <span style="color: var(--text-color-sensor);">${states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state}%</span></span>`
    ]]]
  Watt: |
    [[[
      return `<span>Vermogen: <span style="color: var(--text-color-sensor);">${states['sensor.902_power'].state}W</span></span>`
    ]]]
styles:
  grid:
    - grid-template-areas: '"i n" "i Batterij" "i Watt"'
    - grid-template-columns: 0.5fr 1fr
    - grid-template-rows: 1fr min-content min-content
  name:
    - align-self: start
    - padding-top: 5px
    - padding-bottom: 10px
    - padding-left: 0px
    - justify-self: left
  icon: |-
    if (states['sensor.lenovo_tab_m10_plus_3rd_gen_batter'].state < 95) {
         icon = "mdi:battery-50";
         color = "yellow" }
  custom_fields:
    Batterij:
      - padding-bottom: 10px
      - padding-top: 5px
      - align-self: start
      - font-size: 17px
      - justify-self: start
      - color: white
    Watt:
      - padding-bottom: 10px
      - padding-top: 0px
      - align-self: start
      - font-size: 17px
      - justify-self: start
      - color: white
view_layout:
  grid-area: a4

this came up in another question recently. didn’t have a great final answer but a workaround:

Definitely doable!

type: custom:button-card
entity: switch.kitchen_lights
name: Tablet
icon: |
  [[[ return states['switch.kitchen_lights'].state === 'on' ? 'mdi:battery' : 'mdi:battery-off';
           ]]]
styles:
  icon:
   - color: |
      [[[ return states['switch.kitchen_lights'].state === 'on' ? 'red' : 'green';
           ]]]

chrome-capture-2024-3-10

ah, the other thread is for the little overlays. i couldn’t find a clean way to make the overlay icons change.

Thx for your reply.
Strange thing happened.
I had it working and suddenly it stopped working.
How is that possible ?
I got all the icons like i programmed it in yaml.
Now i get only this icon.
Schermafbeelding 2024-03-11 185455

This is the code that worked 5 minutes ago.
The only thing i noticed is that where i placed value = ‘on’ suddenly icon: >- changes in icon: |- and i can’t change it anymore.
What is going on ?

type: custom:button-card
entity: binary_sensor.lenovo_tab_m10_plus_3rd_gen_plugged_in
name: Tablet
custom_fields:
  Batterij: |
    [[[ 
     return `<span>Batterij: <span style="color: var(--text-color-sensor);">${states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state}%</span></span>`
    ]]]
  Watt: |
    [[[
      return `<span>Vermogen: <span style="color: var(--text-color-sensor);">${states['sensor.902_power'].state}W</span></span>`
    ]]]
icon: mdi:battery-charging
styles:
  grid:
    - grid-template-areas: '"i n" "i Batterij" "i Watt"'
    - grid-template-columns: 0.5fr 1fr
    - grid-template-rows: 1fr min-content min-content
  name:
    - align-self: start
    - padding-top: 5px
    - padding-bottom: 10px
    - padding-left: 0px
    - justify-self: left
    - font-size: 25px
  icon:
    - width: 50px
    - height: 50px
    - transform: scale(0.9)
    - padding-left: 0px
  custom_fields:
    Batterij:
      - padding-bottom: 10px
      - padding-top: 5px
      - padding-left: 0px
      - align-self: start
      - font-size: 17px
      - justify-self: start
      - color: white
    Watt:
      - padding-bottom: 10px
      - padding-top: 0px
      - padding-left: 0px
      - align-self: start
      - font-size: 17px
      - justify-self: start
      - color: white
state:
  - value: 'off'
    icon: >-
      [[[ if (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state >= 60)
      return 'mdi:battery-60'; 
          if (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state <= 40) return 'mdi:battery-40';
          if (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state <= 10) return 'mdi:battery-10';
          
      ]]]
    styles:
      icon:
        - color: >-
            [[[   if (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state
            >= 60) return 'green';  if
            (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state <= 40)
            return 'yellow';  if
            (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state <= 10) 
            return 'red'; ]]]  
  - value: 'on'
    icon: |-
      [[[
         if (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state >= 60) return 'mdi:battery-charging-60'; 
         if (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state <= 40) return 'mdi:battery-charging-40';
         if (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state <= 10) return 'mdi:battery-charging-10';
      ]]]
    styles:
      icon:
        - color: >-
            [[[   if (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state
            >= 60) return 'green'; if
            (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state <= 40)
            return 'yellow'; if
            (states['sensor.lenovo_tab_m10_plus_3rd_gen_battery'].state <= 10)
            return 'red';  ]]]  
view_layout:
  grid-area: a4

It’s getting worse…
I didn’t do anything ???

Can somebody please help ??

Schermafbeelding 2024-03-11 191203

i put your code above in my dashboard replacing only the entities and it works for me…
image

Code worked for me too! Let @armedad or myself know if you need further assistance.

Thx guys.
I don’t know what happened but I went back to my last backup.
Then I pasted the code again and now it works.
Can you tell me what caused this strange behavior ? Not easy I guess :thinking:
I had the code also in my test dashboard but i don’t think that can be an issue.

I would worry about it unless it happens again.