Lovelace: Button card

That’s a question better asked in a private message so we can stay on topic here. It’s already a giant thread

is there something wrong in the code?

        state:
          - styles:
              icon:
                - color: >
                    [[[ if (['cover.pc_r'].attributes.current_position == '0') return 'green'; else return 'white' ]]]

trying to change the color of the icon when the position of the cover.pc_r entity is set to 0

use
states['cover.pc_r'].attributes.current_position

to make it:

   - color: >
       [[[ if (states['cover.pc_r'].attributes.current_position == '0') return 'green'; return 'white' ; ]]]
1 Like

I am really struggling with creating a label showing the attribute of an entity.
This returns a string in the template developer console:
{{ state_attr('alarm_control_panel.sector_alarm_XXXXXXXX', 'changed_by')}}

So, I want to create a label for my card containing that string, but I can’t figure out how to configure that with the button-card. I have tried many things, among them this:

      label: >
        {{ state_attr('alarm_control_panel.sector_alarm_XXXXXXXX', 'changed_by')}}

and

      label: >
        [[[  return state_attr('alarm_control_panel.sector_alarm_XXXXXXXX', 'changed_by')]]]

How should I do this?

@RomRider I have a question about input_selects. I actually want to use an input_select on a button card with the ability to show a drop-down menu.

The reason I ask is because the entities card looks aweful in my setup and I would just love to see a plain button card that could do this.

There is no way I can make this input select button to show an icon only and make it aspect ratio 1/1 with the core card.

Examples below:

As you can see it doesn’t scale well (the right bottom button is a core entities card).

What I want is to put this in my top bar and have the menu accessible from there without the need of going to a separate view. The way I want to do this is with browser_mod. However this looks bad and your card is soo much better for this. It just doesn’t seem to support this dropdown menu does it?

I’d love to hear your reaction.

The post above your post shows how to retrieve an attribute in the button card template style.

you’re mixing jinja (your templates) with JS (needed for the buttons)

depending on the rest of your button of course (have you set an entity?) the way to set a label is:

using the button entity:

        label: >
          [[[
          return entity.attributes.friendly_name
          ]]]

using another entity:

        label: >
          [[[
          return states['alarm_control_panel.sector_alarm_XXXXXXXX'].attributes.changed_by
          ]]]

should show you something

Here is my code:

    - type: custom:button-card
      entity: alarm_control_panel.sector_alarm_XXXXXXXX
      show_label: true
      show_name: false
      label: >
        [[[
        return entity.attributes.changed_by
        ]]]

The above gives me
button-card-label-1

Trying this:

    - type: custom:button-card
      entity: alarm_control_panel.sector_alarm_XXXXXXXX
      show_label: true
      show_name: false
      label: >
        [[[
        return states['alarm_control_panel.sector_alarm_XXXXXXXX'].attributes.changed_by
        ]]]

gives me this
button-card-label-2

most awkward… can you test with an other entity, just take a simple sensor, to see if it has anything to do with the alarm_control_panel entity ?

though I doubt that because:

  - type: custom:button-card
    template: button_picture_script_small
    entity: alarm_control_panel.ha_main_alarm
    show_label: true
    show_name: false
    label: >
      [[[
      return entity.attributes.friendly_name
      ]]]

gives me:

33

and using an attribute of another entity works just as perfectly…

Are you definitely on the latest version of the button card module?

Seems I wasn’t on the latest version, so I installed the latest code and restarted HA. It works now.

Thanks!

How to insert templates into a custom template?

i have several templates into a button-card config so i’d like to put them into a custom template to not insert all of them always in all the cards.

For example i have this template:

                entity_picture: >
                  [[[
                    if (entity.state !== 'home') return '/local/lovelace/images/Maurizio_away.jpg';
                    return '/local/lovelace/images/Maurizio.jpg';
                  ]]]

or this:

                custom_fields:
                  blocked: |
                    [[[
                      return `<span>Maurizio: <span style='color: var(--text-color-sensor);'>${states['person.maurizio'].state}</span></span>`
                    ]]]
                  queries: |
                    [[[
                      return `<span>Batteria: <span style='color: var(--text-color-sensor);'>${states['sensor.samsung_s8_battery'].state}%</span></span>`
                    ]]]

How to put them into custom templates and then use into a card?

Some examples can really help me…

You’re right, it doesn’t support the drop-down menu, maybe I can have a look but I’m really really busy with work currently. Please open a feature request so that I can track it down. Thanks.

thanks for your help!
looks like it still doesn´t work. color won´t change.
do i still have something wrong?

      - entity: group.all_covers
        icon: 'mdi:arrow-right-box'
        name: Mitte
        show_state: false
        state:
          - styles:
              icon:
                - color: >
                    [[[ if (states['cover.pc_r'].attributes.current_position == '51') return '#008bef'; return 'white' ; ]]]
        tap_action:
          action: call-service
          haptic: success
          service: cover.set_cover_position
          service_data:
            position: 51
        template: cover_template
        type: 'custom:button-card'

cover current_position is 51

styles should be on the top hierarchy here, (and not under state):

styles:
  icon:
    - color: >
        [[[ if (states['cover.pc_r'].attributes.current_position == '51') return '#008bef'; return 'white' ; ]]]

have a look at this button:

      - type: custom:button-card
        template: button_body
        entity: automation.count_warnings
        name: Count warnings
        state:
          - value: 'on'
            spin: true
        styles:
          state:
            - color: >
                [[[ if (entity.state == 'on') return 'rgb(251, 210, 41)'; return 'grey';]]]
          icon:
            - color: >
                [[[ if (entity.state == 'on') return 'rgb(251, 210, 41)'; return 'grey';]]]
1 Like

damn, i´m an idiot.
is it also possible to set the value between 49 and 51? because sometimes my cover stop at 49, 50 and 51.

nevermind, got it!

- color: |
                [[[
                  if (states['cover.pc_r'].attributes.current_position == '50') return '#008bef';
                  if (states['cover.pc_r'].attributes.current_position == '51') return '#008bef';
                ]]]

or try something in the lines of:

- color: |
    [[[ if (2 < entity.state && entity.state <= 20) return 'purple';]]]

of course replace the entity.state with the attributes of your liking, or even the external entity

note the

 [[[ if (2 < entity.state  <= 20) return 'purple';]]]

which would work in jinja templates doesn’t work in JS…

in your case:

[[[ var  position = states[‘cover.pc_r’].attributes.current_position;
    if ( 49 <= position && position <= 51) return '#008bef'; ]]]
1 Like

Is it possible to use a template like this:

              - type: custom:button-card
                name: >
                  [[[
                    if is_state('sensor.moon', 'new_moon')
                       return 'Luna Nuova';
                    elif is_state('sensor.moon', 'waning_crescent')
                       return 'Gibbosa crescente';     
                    elif is_state('sensor.moon', 'waxing_crescent')
                       return 'Mezzaluna crescente';
                    elif is_state('sensor.moon', 'first_quarter') 
                       return 'Primo Quarto';
                    elif is_state('sensor.moon', 'waxing_gibbous') 
                       return 'Gibbosa crescente';
                    elif is_state('sensor.moon', 'full_moon') 
                       return 'Luna Piena';
                    elif is_state('sensor.moon', 'waning_gibbous') 
                       return 'Gibbosa calante';
                    elif is_state('sensor.moon', 'last_quarter') 
                       return 'Ultimo Quarto';
                  ]]]

It is not working here… where is the culprit?

you’re trying to use Jinja templating in a Javascript setting…

Yes, you’re right, but my knowledge doesn’t allow me to make this right,
Can you give me the right template so i can learn ?