Create my own function in Home Assistant

I have a few entities on an entities card. For every entity I have a card-mod node to style the icon color.
For every entity I have to use almost the same code to change the icon color. Normally, I would change that by putting the code in a function and calling that function with a parameter.
Is that possible? I really don’t know where, how and in what languate it should be done :confused:

Anyone who can point me in the right direction? Don’t need a full solution :wink: .

NOW

card_mod:
      style: |
        :host {
          --card-mod-icon-color: 
          {% set state = states('sensor.SENSOR1') %}
          {% if 'WWW' in state %}
          LimeGreen
          {% elif 'XXX' in state %}
          DeepSkyBlue
          {% elif 'YYY' in state %}
          DarkOrange
          {% elif 'ZZZ' in state %}
          blue
          {% else %}
          LightGray
          {% endif %};
          }

WHAT I WANT

card_mod:
      style: |
        :host {
          --card-mod-icon-color: 
            GET_ICON_COLOR(states('sensor.SENSOR1') );
          }

Function GET_ICON_COLOR

Function GET_ICON_COLOR(in SENSORSTATE string, out ICONCOLOR string) {
{% if 'WWW' in state %}
          LimeGreen
          {% elif 'XXX' in SENSORSTATE %}
          'DeepSkyBlue'
          {% elif 'YYY' in SENSORSTATE %}
          'DarkOrange'
          {% elif 'ZZZ' in SENSORSTATE %}
          'blue'
          {% else %}
          'LightGray'
          {% endif %};
}

or something like that. What I wrote before: I dont know the right language for coding this. Hope you get the idea…

Hi @Paul1865

This may be a job for Custom Template Macros. These are described here.

Ah. thanks a lot :slight_smile: . I will dive into that!

And it works :slight_smile: !
Only have to include for every entity the macro library, but that cannot be done on a card-main-level, or once in a sensor template