Lovelace: Button card

Ildar, using cad_mod works quite well !! Many thanks!
Now I need to check if a variable I use for a select entity is not null and if it’s not null check whether the value is the same of the state of the entity to change the color. How do I do this in jinja2 ? Sorry for my lack of knowledge…
The following is the code in custopm button card:

            if (variables.var_select_option_value) {
              if (states[entity.entity_id].state == variables.var_select_option_value) {
                return "rgb(0,100,255)";
              } else {
                return "Silver";
              };
            };

Basically I need to know how to call a variable, for the entity I just used the code of your example and I need to add the above code into the below code:

        {% if config.entity.startswith('switch.') -%}
          {% if states(config.entity) == 'on' -%}
            color: rgb(0,100,255) !important;
          {%- else -%}
            color: silver !important;
          {%- endif %}
        {%- endif %}

One more thing, are the dashes “-” of the structures correct? {%- and -%} Seems a little mysterious…

Many thanks again :wink:

for that specific need, we have the has_value() test or filter |has_value

Marius, what I need to know is how to call the “variables.var_select_option_value” in jinja2? I don’t know the syntax? Thanks!

that might be a problem, because that is something that lives in the Frontend, and Jinja is connected with backend…

It makes me speak out my earlier thought, on why you would want to write this ‘generic’ template for all button cards? why not only do this on these specific cards, so you can leave out that test.

Could any CSS gurus help me with this. What I am trying to achieve is the fade in animation on page load. And then when I hover over the card, transition the opacity again. The opacity on hover changes but it does not respect the duration/fade.

      - type: "custom:button-card"
        entity: sun.sun
        styles:
          card:
            - animation: fade-in 1s forwards 0.3s ease-in
            - visibility: hidden
            - transition: opacity 1.5s ease-in-out
        extra_styles: |
          @keyframes fade-in {
            0% {opacity: 0;}
            100% {opacity: 1; visibility: visible;}
          }

          @media (hover: hover) {
            ha-card:hover {
              opacity: 0.5 !important;
            }
          }

buttonfade_example-ezgif.com-video-to-gif-converter

Not a big issue for card-mod & SOME other use-cases for jinja, but compare these variants:

where the last variant leads to
image

i.e. here for card-mod it is a matter of a compact look (although card-mod will work anyway).

Marius, because I have many cards with the same needs… So basically it’s not possible to read a variable set in each card? Unless it’s the main entity? That’s pretty frustrating?
I’ll need to fing a workaround…

type: custom:button-card
entity: sun.sun
variables:
  COLOR: red
card_mod:
  style: |
    div#img-cell > ha-state-icon {
      {% if config.entity.startswith('sun.') -%}
        color: {{config.variables['COLOR']}} !important;
      {%- endif %}
    }

Ildar, if this is possible it would be amazing!
I tried the following code, but it doesn’t seem to work, unfortunately. I might have done something wrong?:

  card_mod:
    style: |
      div#img-cell > ha-state-icon {
        {% if config.entity.startswith('switch.') -%}
          {%- if states(config.entity) == 'on' -%}
            color: rgb(0,100,255) !important;
          {%- else -%}
            color: Silver !important;
          {%- endif -%}
        {%- elif states(config.entity) == {{config.variables['var_select_option_value']}} -%}
          color: rgb(0,100,255) !important;
        {%- endif %}
      }

I don’t know if the “elif” part is OK?
Many thanks!!

Because you tried to use a template inside a template.
Should be like (untested)

{%- elif states(config.entity) == config.variables['var_select_option_value'] -%}

Ildar, wow, it works! Many thanks again, you’re too much :slight_smile:

One more thing, how can I do a negation in jinja? Is it possible to use the exclamation mark?:

{%- if !config.variables['var_select_option_value'] -%}

Not a huge expert in jinja.
What is a purpose of this “!config.variables[‘xxx’]”?
Just to check that this variable is defined?
If yes - try smth like

{% if config.variables['xxx'] is defined %}
...

Well, you’re right, I set it as null unless the card defines it, so I’ll need to check if it’s null or “is defined” as you suggest…
Can I do:

{% if config.variables['xxx'] is defined && config.variables['xxx'] is not null %}

Try smth like

{% if config.variables['xxx'] is defined and config.variables['xxx'] != 'null' %}

See, most of these things you may test yourself in Dev tools → Template

{% set config = {
  'variables': {'a': 1, 'b': 2, 'c': 'null'},
  'd': 3
} %}

{{config.variables['a']}}
{{config.variables['c'] is defined}}
{{config.variables['d'] is defined}}
{{config.variables['c'] != 'null' }}
{{config.variables['c'] == 'null' }}

Ildar I just found this:

{%- elif config.variables['var_select_option_value'] is defined and config.variables['var_select_option_value'] is not null -%}

But it doesn’t work?

I also tried

{%- elif config.variables['var_select_option_value'] != null -%}

It doesn’t work either?

I do not think that “null” is a correct word in jinja…
Check my example from Dev tools, try comparing with a STRING “null”.
I am a “C/C++” person, for me these python/jinja things are smth weird.

I just realized the quotes ‘null’, but it doesn’t work…

How to you initialise unused variables with “null”?

Not even this is workig…:

        {%- elif config.variables['var_select_option_value'] is defined -%}
          {%- if states(config.entity) == config.variables['var_select_option_value'] -%}
            color: rgb(0,100,255) !important;
          {%- else -%}
            color: Silver !important;