Need help with sensor icon color based on state

no worries, we’re here to help:

this is my setup:

have this in configuration.yaml:

homeassistant:
  name: HeadQuarters
  latitude: !secret latitude
  longitude: !secret longitude
  elevation: !secret elevation
  unit_system: metric
  time_zone: !secret time_zone
  customize: !include_dir_merge_named customize # customize.yaml
  customize_glob: !include customize_glob.yaml
# https://home-assistant.io/docs/configuration/packages/
  packages: !include_dir_named packages
  whitelist_external_dirs: /config

have files for seperate entities in the folder /config/customize:

27

if you do it like this, you can have each of these files simply filled with the entitities you like to customize, no indentation or hyphens (-)

just like this:

sensor.tv_library_actueel:
  templates:
    icon: "if (state > 0) return 'mdi:television';
                            else return 'mdi:television-off';"
    rgb_color: "if (state > 0) return [251, 210, 41]; else return [54, 95, 140];"

sensor.tv_library_totaal:
  icon: mdi:television

sensor.kantoor_actueel:
  templates:
    icon: "if (state > 0) return 'mdi:laptop';
                            else return 'mdi:laptop-off';"
    rgb_color: "if (state > 0) return [251, 210, 41]; else return [54, 95, 140];"

sensor.kantoor_totaal:
  icon: mdi:desktop-mac

sensor.audio_gym_actueel:
  templates:
    icon: "if (state > 0) return 'mdi:speaker';
                            else return 'mdi:speaker-off';"
    rgb_color: "if (state > 0) return [251, 210, 41]; else return [54, 95, 140];"

just give it a try and come back with your results,
cheers,
Marius

6 Likes