Update entity color based on attribute

Hi.

I am trying to set the icon color for the entities defined by the yahoofinance integraion. They are in the form yahoofinance.stock_name. Each entity provides an attribute called ‘trending’. I wish to set the icon color for each entity based on this attribute. Now, two questions:

  1. I want to define a group which holds all of these stock entities. Rather than listing each entity individually, can I do something like yahoofinance.*?
  2. What template should I write to achieve this? How can I apply the template to the group rather than repeating the code for each entity? I probably have to use expand somewhere.

I have tried this w/o luck in customize.yaml

 yahoofinance.tsla:
  friendly_name: TSLA
  templates:
    icon_color: >
      if (is_state_attr('yahoofinance.tsla','trending','up') return [0,255,0];
      return [255,0,0];

I have also tried rgb_color instead of icon_color.

Thanks

I can’t help with that integration, but if that integration exposes sensors then you could you something like the button-text-card:

type: 'custom:button-text-card'
title: |
  [[[ return states["sensor.purpleair_description"].state ]]]
subtitle: >
  [[[ return "Air Quaility Index is " + states["sensor.purpleair_aqi_a"].state
  ]]]
icon_size: 55
icon_color: |
  [[[
    if(states["sensor.purpleair_aqi_a"].state > 300){
      return 'white';
    } else if(states["sensor.purpleair_aqi_a"].state > 200){
      return 'white';
    } else if(states["sensor.purpleair_aqi_a"].state > 150){
      return 'white';
    } else if(states["sensor.purpleair_aqi_a"].state > 100){
      return 'white';
    } else if(states["sensor.purpleair_aqi_a"].state > 50){
      return 'black';
    } else{
      return 'black';
    }
  ]]]
icon: |
  [[[
    if(states["sensor.purpleair_aqi_a"].state > 300){
      return 'mdi:emoticon-dead';
    } else if(states["sensor.purpleair_aqi_a"].state > 200){
      return 'mdi:emoticon-cry';
    } else if(states["sensor.purpleair_aqi_a"].state > 150){
      return 'mdi:emoticon-sad';
    } else if(states["sensor.purpleair_aqi_a"].state > 100){
      return 'mdi:emoticon-confused';
    } else if(states["sensor.purpleair_aqi_a"].state > 50){
      return 'mdi:emoticon-neutral';
    } else{
      return 'mdi:emoticon-happy';
    }
  ]]]
font_color: |
  [[[
    if(states["sensor.purpleair_aqi_a"].state > 300){
      return 'white';
    } else if(states["sensor.purpleair_aqi_a"].state > 200){
      return 'white';
    } else if(states["sensor.purpleair_aqi_a"].state > 150){
      return 'white';
    } else if(states["sensor.purpleair_aqi_a"].state > 100){
      return 'white';
    } else if(states["sensor.purpleair_aqi_a"].state > 50){
      return 'black';
    } else{
      return 'black';
    }
  ]]]
large: true
background_color: |
  [[[
    if(states["sensor.purpleair_aqi_a"].state > 300){
      return '#731425';
    } else if(states["sensor.purpleair_aqi_a"].state > 200){
      return '#8C1A4B';
    } else if(states["sensor.purpleair_aqi_a"].state > 150){
      return '#EA3324';
    } else if(states["sensor.purpleair_aqi_a"].state > 100){
      return '#EF8533';
    } else if(states["sensor.purpleair_aqi_a"].state > 50){
      return '#FFFF55';
    } else{
      return '#68FF43';
    }
  ]]]