šŸ”¹ Card-mod - Add css styles to any lovelace card

another day, another challengeā€¦

trying to create a conditional badge, and it should be added to an existing card-mod. Here, display: none is suggested by Troon, but I canā€™t get it right just yet.

  - entity: sensor.state_badge
    card_mod:
      style: |
        :host {
          display: {% if config.entity is none %}none{%endif%}
        }
        ha-state-label-badge:
          $:
            ha-label-badge:
              $: |
                .badge-container .label-badge {
                  /* Top circle of the Badge (State) */
                  border-style: dashed;
                  border-color: blue;
                  color: grey;
                  background-color: yellow;
                 }
                  /* Label of the Badge (Unit) */
                .badge-container .label-badge .label span {
                  border-style: dotted;
                  border-color: pink;
                  color: red;
                  background-color: green;
                 }
                  /* Below the Badge (Name) */
                .title {
                  color: orange;
                }

So I have two challenges here:

  • on config.entity: the config.entity is none template doesnt work, (while a test in dev tools does work) tried config.entity.entity_id etc. if not config.entity which should be valid, same effect: Keep getting
    Schermafbeelding 2021-06-29 om 10.00.31

  • how to combine the display: none template with the other card_mod config settingsā€¦

as an example, this does work:

    card_mod:
      style: |
        :host {
          display: {% if states('binary_sensor.updater') == 'off' %} none {% endif %}
        }

thanks for having a look

Marius,

  1. Wrong placement for ":host" style.
  2. Should be is_state(config.entity, ā€œnoneā€).
  3. Wrong expression for "display" style.
        card_mod:
          style:
            ha-state-label-badge:
              $:
                ha-label-badge:
                  $: |
                    .badge-container .label-badge {
                      border-style: dashed;
                      border-color: blue;
                      color: grey;
                      background-color: yellow;
                     }
                    .badge-container .label-badge .label span {
                      border-style: dotted;
                      border-color: pink;
                      color: red;
                      background-color: green;
                     }
                    .title {
                      color: orange;
                    }
            .: |
              :host {
                {% if is_state(config.entity,"none") %} display: none; {% endif %}
              }

ŠøŠ·Š¾Š±Ń€Š°Š¶ŠµŠ½ŠøŠµ

1 Like

thanks, and indeed, I can now use the 2 templates. Finally see and understand the use of .: | ā€¦

still, the test for existence doesnt work:

Schermafbeelding 2021-06-29 om 13.17.23

Simply try it on an incorrect entity, and youā€™ll see.
seems the config.entity cant be tested for existence in card-mod, hence 'if config.entity is none' template doesn't work Ā· Issue #134 Ā· thomasloven/lovelace-card-mod Ā· GitHub

Try this, it works in my setup:

{% if states(config.entity) in ['unavailable','unknown','none'] %} display: none; {% endif %}

No that doesnt work either. Did you try to take out a single character of the entity? like binary_sensr.updater ?

That was exactly how I tested - with "sensor.cleargrass_1_co"

well Iā€™ll beā€¦

please dont tell me Ive got another typo here:

  - entity: sensor.state_badg
    card_mod:
      style:
        ha-state-label-badge:
          $:
            ha-label-badge:
              $: |
                .badge-container .label-badge {
                  /* Top circle of the Badge (State) */
                  border-style: dashed;
                  border-color: blue;
                  color: grey;
                  background-color: yellow;
                 }
                  /* Label of the Badge (Unit) */
                .badge-container .label-badge .label span {
                  border-style: dotted;
                  border-color: pink;
                  color: red;
                  background-color: green;
                 }
                  /* Below the Badge (Name) */
                .title {
                  color: orange;
                }
        .: |
          :host {
            display: {% if states(config.entity) in ['unavailable','unknown','none'] %} display: none; {% endif %}
          }

Here is double display word, causes error.

this is what I am trying to replicate with config.entity:

{% if states.binary_sensor.updater is defined %} none {% endif %}

or even better:

{% if states.binary_sensor.updater %} none {% endif %}

both of these work as expected, and likewise with ā€˜not is definedā€™ or is none

Regarding the condition:
if condition = true:

    card_mod:
      style: |
        :host {
          display: none
        }

if condition = false:

    card_mod:
      style: |
        :host {
          display: 
        }

The 2nd expression will not work if using this construction:

          display: {% if config.entity is none %}none{%endif%}

so this works:

      style: |
        :host {
          display: {% if states(config.entity) in ['unknown'] %} none {% endif %}
        }

and this:

      style: |
        :host {
          display: {% if states(config.entity) == 'unknown' %} none {% endif %}
        }

ā€¦ pff now thats a bit of a surprise. I mean, not that these 2 templates do the same. of course they do. but the fact we can not use config.entity, and can not use is none, but need to use the states() of the config.entity, and need to check for 'unknown'

This is anyway wrong conditional check.

A=
if(condition)
  123

What if condition is false? The value A becomes undefinedā€¦
You should specify ā€œelseā€ or put ā€œA=ā€ under the conditionā€™s check.

Seems so:
ŠøŠ·Š¾Š±Ń€Š°Š¶ŠµŠ½ŠøŠµ

yes, thats what I wrote here, and as state there, it seems to work just fine. Have just tested it with this full card-mod, and can confirm once more that is shows when the existence check is positive. (the condition isnā€™t met, iow the states(config.entity) != 'unknown'

yes, of course I understand that, but thatā€™s checking for the states state (as in state of the entity), while I was trying to check for the state (as in entity) itself

compare this js template:

        - color: >
            [[[ if (entity) return entity.attributes.icon_color;
                return 'var(--secondary-text-color)'; ]]]

first check if the entity itself exists, then return an attributes of that entity

seems we should be able to do that with config.entity, unless thereā€™s something in card-mod preventing that.
Well, youā€™ve pointed me to a solution, to thanks again! Hope Thomas will have a look at the issue and see if things can be simplifiedā€¦

add this to the top of your lovelace view:

    style:
      <<: &exist
        .: |
         :host {
           display: {% if states(config.entity) == 'unknown' %} none {% endif %}
         }

and simply add it to any other badge with:

    style:
      <<: *exist

Nice !

btw, with {% else %} guard:

           :host {
             display: {% if states(config.entity) == 'unknown' %} none
                      {% else %} inline-block
                      {% endif %}

or:

    card_mod:
      style:
        <<: &exist
          .: |
           :host {
             {% if states(config.entity) == 'unknown' %} display: none
             {% else %} display: inline-block
             {% endif %}
           }

Do not touсh it. One day it may be changed in HA code. It is better to simply ā€œdo not change the styleā€.

now you lost meā€¦ I thought you wanted an else clause? what would you suggest in this case then?

building on this, I tried the same mod on a button-card in the view, but that doesnā€™t work. It simply shows a button, with all style settings, but without a working entityā€¦

That is why I proposed you:

              :host {
                {% if is_state(config.entity,"none") %} display: none; {% endif %}
              }

which is equal to:

              :host {
                display: none;
              }

and

              :host {
              }

Give me a code for the button & your expectations, I will try to figure it out.

ok I see, thanks. didnā€™t realize thatā€™s what you implied with that.

the interpreter wonā€™t choke on an empty {} then? Guess not, it doesnā€™t choke on the absence of the else either :wink:

no, it will be OK.
you may put a useless style if you like:

--my-invented-variable: red;