Lovelace: Button card

What exactly is not working? Colors is, unfortunately, not specific enough. :wink:

My guess, you’re getting always the “default” styles, as the variables in value should normally be a javascript template. So first change

states:
    - operator: '>'
      value: variables.max

to

states:
    - operator: '>'
      value: "[[[ return variables.max ]]]"

and see if it works like you want. Otherwise specify a little more, or just show a picture of what it looks with this code, and what you want to have in the end. :slight_smile:

After looking at it again, my guess would be the aspect_ratio. Remove the aspect ratio and see how hard it hits the design :rofl:
 I’m quite sure, you’re limiting the resize (because of the aspect_ratio) and therefor it get’s cut in smaller windows.

Try experimenting with it in the browser window, where you can make the window bigger and smaller to see the result. :slight_smile:

EDIT: Google Chrome has a good develper tool, there you can view your layout in different sizes (pre-configured)

If I delete aspect_ratio, the card disappears


That’s unfortunate, but it is the source of the display problem
 :wink:

I’d say you’d have to start again, as with aspect_ratio you won’t get the result you wanted. I can only speak for the Minimalist “theme”, and there the alarm card is handled a lot different than other cards. You might want to take a look at the alarm_card clemalex made, maybe this helps you? :slight_smile:

Thanks for your help.

Chips_icon is the “UV : HAUT” card

IMG_20211019_203621

The card do not update the style (text-color or background color) according the entity state.

If entity.uv > max (6), the card should display “prefix” + “haut” (works well) and change the background color to red (not working)

Edit : I tried your fix but it doesn’t work :confused:

I can’t try it myself, can’t get to my PC right now, sorry. So bear with me
 :wink:

Have you tried using the template function instead for the operator? Like so:

states:
  - operator: template
    value: >
      [[[
        if (states[variables.entity].state < variables.min)
          return true;
      ]]]
    styles:
      [...]

And for the other states as well. The value needs to return true, to use the style underneath its definition.

And you should leave the “fix” from before in place. Even if it wasn’t the culprit here, it is the best way to use it like this. :slight_smile:

EDIT: any error messages in the developer console of the browser?

Yes template works well ! Thanks a lot !! :grin:

1 Like

Hi,
I have the following issue:
In order to display an adress I readout the subattribute of a places sensor:

var street = states['sensor.joergs_location'].attributes.street

Sometimes, this attribute is null.
Then an error appears:

ButtonCardJSTemplateError: TypeError: Cannot read properties of null (reading 'substring') in 'var street = states['sensor.joergs_location'].attributes.street var number = states['sensor.jo...'

How can I manage this error so that the card is simply displayed with ‘unknown’?
I tried to insert an if clouse 'If states[‘sensor.joergs_location’].attributes.street is not null or != null

That doesn’t work. Has anybody an idea?

Maybe for better understanding, the whole code looks like this and basically works:

name: |
  [[[
      var street = states['sensor.joergs_location'].attributes.street
      var number = states['sensor.joergs_location'].attributes.street_number
      street = street.substring(0,30)
      if (states['sensor.joergs_location'].attributes.devicetracker_zone == 'not_home') 
         return street + ' ' + number;
      else return states['person.jorg'].state;
  ]]]
label: |
  [[[
     var town = states['sensor.joergs_location'].attributes.city
     var postal = states['sensor.joergs_location'].attributes.postal_town
     town = town.substring(0,30)
     postal = postal.substring(0,30)
     if (town == '-') return postal;
     else return town;
  ]]]

thats probably because the entity doesnt exist or has unknown/unavailable state. you need to test for that:

          [[[ if (entity && entity.attributes && entity.attributes.templates) {
                const tpl = entity.attributes.templates;
                if (tpl.icon_color) {
                  return new Function('state', `'use strict'; ${tpl.icon_color}`).call(this, entity.state);
                }
              }
              return null; ]]]

sorry if this is a bit of a complex example, but the crux is in the first line


also you might be better of by setting the var to states['sensor.joergs_location'] and reference that in the rest of the template, to make it shorter and more readable

this one is a better example:

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

Thanks Marius,

I understand, that you check if the entity is valid in the first if clause.
But my problem is, that the entity is valid but the content of the attribute is null.
in other languages, I can simply check with a function like ‘ISNULL(value) = true’
I tried this

if (states['sensor.joergs_location'].attributes)

this doesn’t work because the entity is valid

it’s either the entity itself you check for existence (the above set of template checks does that), and one could even use this for that:

exist:
  styles:
    card:
      - display: >
          [[[ if (!entity) return none; ]]]

or if it does exist but still throws the error of null or none, you should check if the entity.state is not in [‘unavailable’,‘unknown’]. In fact you should always do that


like this in jinja templates for automation conditions:

    condition:
# first check if the state (the entity) itself exists
      - >
        {{trigger.to_state is not none and
          trigger.from_state is not none}}

# next check if the states state is not unknown
      - >
        {% set x = ['unavailable','unknown'] %}
        {{trigger.to_state.state not in x and
          trigger.from_state.state not in x}}

# finally check if the old and new states are not identical
      - >
        {{trigger.to_state.state != trigger.from_state.state}}
  [[[
    if (!states['sensor.yvonnes_location'].attributes.street) return 'unknown';
    else
      var street = states['sensor.yvonnes_location'].attributes.street
      var number = states['sensor.yvonnes_location'].attributes.street_number
      street = street.substring(0,30)
      if (states['sensor.yvonnes_location'].attributes.devicetracker_zone == 'not_home')
         return street + ' ' + number;
      else return states['person.yvonne'].state;
    ]]]

result is the error described above, because the entity is valid but the attribute “street” returns null and that cannot be computed.
All what I need is a method that handles this return value.

if  states['sensor.yvonnes_location'].attributes.street is null return 'unknown';

I tried it, but it doesn’T work

this is the state of the entity:
image

hmm, ok, so the entity exists, it even has a state, but none of the attributes has a valid value.

simple check for ‘null’ on the attributes, where you now check for ‘unknown’

or maybe even better the integration. that creates these entities 
 which is that?

you can check the template editor in dev tools with a jinja template

{{state_attr('sensor.yvonnes_location','street')}} and
{{is_state_attr('sensor.yvonnes_location','street','null')}} to see if it actually returns the string ‘null’ ? Or whether it is in fact not yet set, and thus still null (so not the string), in which case you can check for none :wink:

{{state_attr('sensor.yvonnes_location','street')}}

simply returns nothing

exactly, test for none on the attribute
{{state_attr('sensor.yvonnes_location','street') is none }} will return true ?

Yes, that works

{% if state_attr('sensor.yvonnes_location','street') is none  %} 'test' {% else %} 'huhu' {% endif %}

right, so no you know what to test for in those JS templates

return none;
1 Like

Thanks for your help! Now I have the approach!
In the end I still have the possibility to define a sensor :wink: That’s maybe easier.

1 Like

any time! hope you get it working. and remember there are many options and variations.

if (entity != undefined)

is a valid test too

Ok, I gave up :slight_smile:
Created a sensor now:

{% set joerg = states.sensor.joergs_location.attributes %}
        {% if states('sensor.joergs_location') != 'not_home' %}
            {% if joerg.street is none %}
                {% set street = 'unknown' %}
            {% else %}
                {% set street = joerg.street %}
            {% endif %}
            {% if joerg.street_number is not none %}
                {% set number = joerg.street_number %}
            {% endif %}
            {{ street }}  {{ number }}
        {% else %}
            {{ states('sensor.joergs_location') }}
        {% endif %}

that works !
thanks again!