Should this card-mod-row-yaml (or templates) be written differently

  card-mod-row-yaml: |
    "*:first-child$": |
      state-badge {
        {% if config.entity.startswith('switch') %}
          {% if is_state(config.entity, 'on') %} color: var(--primary-color)
          {% else %} color: steelblue
          {% endif %};
        {% elif config.entity.startswith('automation') %}
          {% if is_state(config.entity, 'on') %} color: var(--primary-color)
          {% else %} color: steelblue
          {% endif %};
        {% elif config.entity.startswith('input_boolean') %}
          {% if is_state(config.entity, 'on') %} color: var(--primary-color)
          {% else %} color: steelblue
          {% endif %};
        {% elif config.entity.startswith('binary_sensor') %}
          {% if is_state(config.entity, 'on') %} color: var(--primary-color)
          {% else %} color: steelblue
          {% endif %}
        {% endif %};
      }

wildly copied from other themes and incorporating several my custom-ui color theming, I try to be as precise and adequate as possible. Not at all sure ion the termination here (:wink: and the opening

  card-mod-row-yaml: |
    "*:first-child$": |

it does work and all mentioned domains are colored. I’d rather write is as
if config.entity is of domain [‘automation’,‘binary_sensor’,‘input_boolean’,‘switch’] or something like that, but not sure how to handle that…

would appreciate any improvements you can find.

thanks!

Try this:

  card-mod-row-yaml: |
    "*:first-child$": |
      state-badge {
        {% if config.entity is match('(switch|automation|input_boolean|binary_sensor)', ignorecase=False) %}
          {% if is_state(config.entity, 'on') %} color: var(--primary-color)
          {% else %} color: steelblue
          {% endif %};
        {% endif %};
      }
1 Like

very nice, thank you very much Taras.
werking beautifully and solving an inspector unhandled promise rejection in 1 go ! (I think)
might I bother you with this too:
lokking for and ending match.

sensor.*_actueel, and sensor.*_state need some formatting too ;-=)
In fact, they need the identical formatting… now _state could maybe fitted in the match you made for me, _actueel can not, because they are numbers, and now use:

          icon_color: >
            if (state > 0) return 'var(--primary-color)';
            return 'steelblue';

You’re welcome!

Sorry but I don’t understand your follow-up question. That looks like JavaScript, not Jinja2, so what exactly am I supposed to “format”?

my pardon, and yes, that’s custom-ui JS. Which I now am trying to transform to Jinja.

so, first, all sensors ending with _state (they have state on/off) need the same styling as what you made above. I hoped those would somehow also fit in that template.

then, the sensors with ending _actueel, which are power sensors, need that same styling when not 0 (which most of the time means the corresponding switches are ‘off’…), hence the identical styling

Ah! OK. This will select entities that contain either of the two desired substrings and they must be located at the end of the string.

{% if config.entity is search('(_state|_actueel)$', ignorecase=False) %}
  • match determines if the string starts with the desired substring.
  • search determines if the string contains the desired substring.

The $ in the regex filter means the substring must be located at the end of the string.

1 Like

thanks, will try and work with that. Since I am rather new to these card-mod themes, I am not sure what this means in the log:

2021-11-03 17:05:17 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'entity' when rendering 'state-badge {
  {% if config.entity is match('(switch|automation|input_boolean|binary_sensor)', ignorecase=False) %}
    {% if is_state(config.entity, 'on') %} color: var(--primary-color)
    {% else %} color: steelblue
    {% endif %};
  {% endif %};
}

again, much appreciated!

I’m not sure I can be of much help with that. My example is based on your example (containing config.entity.startswith) so I don’t understand why it now claims the config dict object has no entity attribute. :thinking:

Unfortunately I don’t use nearly as many custom cards as you do so I have no experience with templating that uses built-in objects like config. All I can confirm is that the suggested templates do work in the Template Editor.

its fine Taras, Ill try to work this out myself with your assisted templates. Fyi, this is the source of the card-mod Theme cookbook ¡ thomasloven/lovelace-card-mod Wiki ¡ GitHub I took the example from.

OK. Please consider marking my first post with the Solution tag because, I believe, it adequately answers your original question. Other users might have a similar need to select entities by multiple domains.

Ofc, you shouldn’t have had to remind me, my apologies.