Mushroom Chips card icon_color changing based on state

Hello,
this is my script for my chips card on top of my dashboard. I would like the color of the battery icon to change, based on its sensor’s state:

  - type: custom:mushroom-chips-card
    chips:
      - type: entity
        entity: sensor.tabingresso_battery_level
        double_tap_action:
          action: none
        use_entity_picture: false
        hold_action:
          action: none
        icon_color: >-
          {% set level = state('sensor.tabingresso_battery_level') %}
          {% if level >= 7 %}
            green
          {% else %}
            red
          {% endif %}

…but con is always white:
immagine

This below code works if used for a custom:mushroom-template-card (still not works for chips card), but only if I tell the script to look for a specific value (29):

{% if is_state('sensor.tabingresso_battery_level', '29') %}
  yellow
{% endif %}

Any idea??
Thanks! :wink:

The function is states() not state()… but there are other changes you need to make as well.
States are always strings, so you need to convert them to a integer or float before using mathematical operations or comparisons.

Optionally, Mushroom allows use of a variable entity so you don’t need to use the whole entity_id

  - type: custom:mushroom-chips-card
    chips:
      - type: template
        entity: sensor.tabingresso_battery_level
        double_tap_action:
          action: none
        use_entity_picture: false
        hold_action:
          action: none
        icon: mdi:battery
        icon_color: >-
          {% set level = states(entity) | int(0) %}
          {% if level >= 7 %}
            green
          {% else %}
            red
          {% endif %}

Thank you very much for the explanation, but it is still not working.
If it helps, this is the entity:

I want an icon color based on energy consumption. I tried some things out of a few topics but I can not het it to work:

type: custom:mushroom-chips-card
chips:
  - type: entity
    entity: sensor.electricity_meter_power_consumption
 icon_color: >
     {% if state(entity) | float > 1500 and states(entity) | float < 2500 }
       orange
     {% elif states(entity) | float > 2500 %}
       red
     {% else %}
       green
     {%- endif %}

also tried this (also with state instead of states. Not sure which one is right)

 {% if is_states(entity, > 1500) and is_states(entity, < 2500) | float < 2500 %
  }
    orange
  {% elif is_states(entity) | float > 2500 %}
    red
  {% else %}
    green
  {% endif %}

I got it :slight_smile: Had to use template (mushroom chip btw)… Duh…

type: template
icon_color: >-
  {% if states('sensor.electricity_meter_power_consumption') | float > 1500 and
  states('sensor.electricity_meter_power_consumption') | float < 2500 %} orange

  {% elif states('sensor.electricity_meter_power_consumption') | float > 2500 %}
  red

  {% else %} green

  {% endif %}

1 Like