Lovelace: Button card

Hello everyone, I have a question. Would it be possible to pass the current entity_id into a template (without the use of hardcoded variables?)

E.g. I have a template that opens a browser_mod popup, it will require an entity_id.

- type: custom:button-card
  template: popup
  name: test
  entity: light.patio

and in the templates to have something like this

popup:
  tap_action:
    action: service-call
    service: browser_mod.popup
    service_data:
      title: ' '
    card:
      - type: custom:more-info-card
        entity: "[[[ return entity.this_id ]]]"

This obviously doesn’t work, but I was wondering IF there is such a variable that can pass the same entity_id into the templates (much like auto-entities can do with this.entity_id variable)?

Thanks for your help

it’s [[[ return entity.entity_id ]]] that you want to use :wink:

2 Likes

Hi !

I got a problem to use icon depending on state using operator and value based on another entity:

type: 'custom:button-card'
        entity: sensor.zigbee2mqtt_version
        show_name: false
        color_type: icon
        size: 32px
        styles:
          card:
          - width: 100px
        state:
        - value: '{{ states("sensor.zigbee2mqtt_upstream_version") }}'
          icon: mdi:checkbox-marked-circle-outline
        - operator: default
          icon: mdi:arrow-up-circle-outline
          color: var(--paper-item-icon-active-color)

the icon stay in default state even if states of sensor.zigbee2mqtt_upstream_version and sensor.zigbee2mqtt_version are the same.

Any idea ?

Thanks, great stuff. Unfortunately this doesn’t work when passing it to another card (as in my example). It’s fine didn’t think it would work, but hey could always ask haha. It is probably because the other card doesn’t support templates. My only other option would be using decluttering-card or lovelace_gen, I’ll figure it out haha. Thanks for the quick reply though (as always).

I still haven’t found the time to play with all the new features you’ve added :S

Button card doesn’t support Jinja2, instead it uses JS. Try this out:

type: 'custom:button-card'
entity: sensor.zigbee2mqtt_version
show_name: false
color_type: icon
size: 32px
styles:
  card:
    - width: 100px
state:
  - value: |
      [[[
      return states['sensor.zigbee2mqtt_upstream_version']
      ]]]
    icon: mdi:checkbox-marked-circle-outline
  - operator: default
    icon: mdi:arrow-up-circle-outline
    color: var(--paper-item-icon-active-color)

I got no error on lovelace ui but I got the same result, the default icon.
I suppose it"s a variable type problem. How to debug this ?

Ooops.

type: 'custom:button-card'
entity: sensor.zigbee2mqtt_version
show_name: false
color_type: icon
size: 32px
styles:
  card:
    - width: 100px
state:
  - value: |
      [[[
      return states['sensor.zigbee2mqtt_upstream_version'].state
      ]]]
    icon: mdi:checkbox-marked-circle-outline
  - operator: default
    icon: mdi:arrow-up-circle-outline
    color: var(--paper-item-icon-active-color)

Yes it’s working now ! Thanks KTibow !

Can you please share your config for your sensor.zigbee2mqtt_upstream_version?

Ask @zentoo. I’d reccommend a PM to not pollute this thread too much.

Ok, sorry.
Will do.

Hi guys, I’ve searched the thread a little, but somehow couldn’t find a suitable solution for me. I hope someone of you can help me.

I use this well-known code in my decluttering template for the button card to display the light brightness within the button card:

card:
  type: custom:button-card
  size: '80%'
  color: auto
  lock: false
  aspect_ratio: '1/1'
  entity: '[[entity]]'
  label: >
    [[[
      var bri = Math.round(states['[[entity]]'].attributes.brightness / 2.55);
      return 'Helligkeit: ' + (bri ? bri : '0') + '%';
    ]]]
  show_state: true
  show_label: true

Unfortunately I have some lights that do not provide brightness attributes (e.g. Shelly Lights Switches and light groups).
For these “lights” I get the following error message with the above code and the button card is no longer displayed:

ButtonCardJSTemplateError: TypeError: Cannot read property 'attributes' of undefined in 'var bri = Math.round(states['light.shelly_badspiegel'].attributes.brightness / 2.55); return 'He...'

Is there a simple way within the label template to check whether the brightness attribute is available and to only display the brightness there? For all other lights without a brightness attribute, I just want to display an empty string at this point.

Thank you in advance for your help / advice

I think

'brightness' in states['[[entity]]'].attributes

should work

Sry, but how did you mean that exactly?
For exampe this gives me just blank button cards:

  label: >
    [[[
      if 'brightness' in states['[[entity]]'].attributes
        var bri = Math.round(states['[[entity]]'].attributes.brightness / 2.55);
        return 'Helligkeit: ' + (bri ? bri : '0') + '%';
    ]]]

JS syntax…

  label: >
    [[[
      if ('brightness' in states['[[entity]]'].attributes) {
        var bri = Math.round(states['[[entity]]'].attributes.brightness / 2.55);
        return 'Helligkeit: ' + (bri ? bri : '0') + '%';
      } else {
        return '';
      }
    ]]]

Damn, I’m still a noob :slight_smile:
Thank you very much!

But unfortunately the buttons of lights that have no brightness information still do not load with your example :frowning:

Try opening the browser console and taking a screenshot.

With your example code it is still the same error message that I received at the beginning

Oh, looks like the light is completely undefined. Check Developer Tools (the HA one) and see if it’s available.
leaves computer, won’t see your next reply until a bit later

Damn, that was the cause. Looks like I broke my configuration for light groups somehow and didn’t noticed it. Now everything works. Thanks a lot