Lovelace: Button card

This looks like it could be a nice way to have my current AdGuard pause functionality in a button card, I was not aware of this nesting.
But somehow it does not work.
Currently I use an input boolean (as a switch in lovelace) and a timer to pause AdGuard for 30 minutes, and reactivate once the timer has reached zero:

alias: Man - AdGuard 30 min. pausieren
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.adguard_pause
    to: 'on'
condition: []
action:
  - type: turn_off
    device_id: 24b9239338d7b9c5ec8b297c1e474f96
    entity_id: switch.adguard_protection
    domain: switch
  - service: timer.start
    target:
      entity_id: timer.timer_30_min
    data:
      duration: '0'
  - wait_for_trigger:
      - platform: state
        entity_id: timer.timer_30_min
        to: idle
  - type: turn_on
    device_id: 24b9239338d7b9c5ec8b297c1e474f96
    entity_id: switch.adguard_protection
    domain: switch
  - service: input_boolean.turn_off
    target:
      entity_id: input_boolean.adguard_pause
mode: restart

This works perfectly fine:
grafik

But if possible I would of course put that in a single fance button card :slight_smile:
So with your example of the timer inside the button card, I tried to start as simple as possible with my input boolean and the timer:

type: custom:button-card
entity: input_boolean.adguard_pause
aspect_ratio: 3/1
show_state: true
state_display: '[[[ return entity.state; ]]]'
custom_fields:
  notification:
    type: custom:button-card
    entity: timer.timer_30_min
    show_state: true

But somehow the timer thing does not work, while the button changes the input boolean as intended and the timer actually starts, the card stays with that object placeholder:
grafik

Where is the mistake?

this doesnt happen often, but for the life of me I cant understand why a button shows an icon that isnt explicitly set, or is inherited from the domain or entity itself for that matter:

      - type: custom:button-card
        template: button_boolean
        tooltip: Toggle Notify
        entity: input_boolean.notify_alarm

shows as
Schermafbeelding 2022-01-21 om 23.11.32

while I would have expected it to follow the boolean defaults mdi:check-circle-outline or mdi:close-circle-outline.

what could be behind this…

fixed it by using:

  icon: >
    [[[ return entity.state === 'on' ? 'mdi:check-circle-outline' : 'mdi:close-circle-outline'; ]]]

same happening on a switch, nb with device_class switch. showing the flash icon while the frontend should be showing toggle/off

@romrider, sorry for this silly question, but doesnt button card follow backend icon settings by default? Or has is just not adjusted to the new frontend pr that were merged lately

I’m trying to get the name and state of an entity horizontally aligned within this button. Unfortunately, layout: name_state doesn’t do what I want, since it just crams the two together:

type: custom:button-card
entity: sensor.total_interior_lights_on
show_state: true
show_name: true
show_icon: false
name: Inside Lights On
color_type: label-card
styles:
  grid:
    - grid-template-areas: '"n" "s"'
  card:
    - height: 85px
    - background-color: rgba(7, 103, 215, 0.24)
    - padding: 2%
    - box-shadow: none
  name:
    - color: white
    - font-size: 20px
    - justify-self: left
    - padding-left: 10px
  state:
    - color: white
    - font-size: 30px
    - justify-self: right
    - padding-right: 10px

Yields this:

LightsOn

Padding either element, top or bottom, just moves both elements. Any ideas?

1 Like

means vertical aligned items.


grid-template-areas: '"n s"'

arranges the elements side bei side.

1 Like

So it does…! Thanks for the help!

I’m trying to color a button based on the state_attr of another entity… this stems from a conditional script I’m putting together to join/unjoin Sonos speakers to groups, and display different colors based on their group membership. Here’s what I have that doesn’t work for me so far:

type: custom:button-card
color_type: card
aspect_ratio: 2/1
color: auto
entity: media_player.sonos_living_room
name: Living Room
show_name: true
tap_action:
  action: call-service
  service: script.turn_on
  service_data:
    entity_id: script.sonos_living_room_join_unjoin_kitchen
show_state: false
state:
  - color: |
      [[[
        if ('media_player.sonos_kitchen' in state_attr('media_player.sonos_living_room', 'sonos_group') return 'rgba(0,200,0,.7)';
        else return 'rgba(200,200,0,.7)';
      ]]]
    icon: mdi:speaker-wireless
    value: playing

Unfortunately, it’s only returning my rgba(0,200,0,0.7). Where have I gone wrong here?

your template is incorrect syntactically.

[[[ return states['media_player.sonos_living_room'].attributes.sonos_group.includes('media_player.sonos_kitchen') ? 'rgba(0,200,0,.7)' : 'rgba(200,200,0,.7)'; ]]]

might work though (didnt check any logic, imply followed js syntax here (you seem to be using some mix jinja /js code :wink: )

You’re right! And I edited my comment it just as you responded. I’m now getting one color, but not both.Nope… I was mistaken. My code is still worthless, let me try what you’ve suggested…

EDIT: No, unfortunately, that just gave me a blank white button in either case… still fooling with it.

OK, this is highly odd:

type: custom:button-card
color_type: card
aspect_ratio: 2/1
color: auto
entity: media_player.sonos_living_room
name: Living Room
show_name: true
tap_action:
  action: call-service
  service: script.turn_on
  service_data:
    entity_id: script.sonos_living_room_join_unjoin_kitchen
show_state: false
state:
  - color: >
      [[[ if ("media_player.sonos_kitchen") in
      (state_attr["media_player.sonos_living_room"], 'sonos_group') return
      'blue';
        else return 'rgba(200,200,0,.7)';
      ]]]
    icon: mdi:speaker-wireless
    value: playing

When I play sonos_living_room (state = playing) and it’s in no group, the button remains green and the icon changes from white to black. When I then add it to the group with sonos_kitchen, it does not change. Hm.

Unfortunately, I’ve tried this and it’s not netting me a color change during playing state:

type: custom:button-card
color_type: card
aspect_ratio: 2/1
color: auto
entity: media_player.sonos_living_room
name: Living Room
show_name: true
tap_action:
  action: call-service
  service: script.turn_on
  service_data:
    entity_id: script.sonos_living_room_join_unjoin_kitchen
show_state: false
state:
  - color: >
      [[[ return
      states['media_player.sonos_living_room'].attributes.sonos_group.includes('media_player.sonos_kitchen')
      ? 'orange' : 'blue'; ]]]
    icon: mdi:speaker-wireless
    value: playing

I’m not seeing a similar example over on the gist, either. Any other ideas?

EDIT: This doesn’t work:

state:
  - color: >
      [[[ if (states['media_player.sonos_living_room.attributes.sonos_group']
      includes['media_player.sonos_kitchen'])
       return 'orange';
       return 'blue'; ]]]
    icon: mdi:speaker-wireless
    value: playing

…nor does this:

state:
  - color: >
      [[[ if
      (states['media_player.sonos_living_room']attributes.sonos_group.includes["media_player.sonos_kitchen"])
       return 'orange';
       return 'blue'; ]]]
    icon: mdi:speaker-wireless
    value: playing

…so I’m at a bit of a loss.

first of all, I didn’t know you were using the entity in your card, so used a ‘global’ states syntax. you should change that to

[[[ return
      entity.attributes.sonos_group.includes('media_player.sonos_kitchen')
      ? 'orange' : 'blue'; ]]]

and save some resources.

also, you’re getting mixed up in states and styles… I take it you want the icon color to change, so you have to indicate that as in the docs:

state:
    - value: playing
      styles:
        icon:
          - color: >
              [[[ return entity.attributes.sonos_group.includes('media_player.sonos_kitchen')
                  ? 'orange' : 'blue'; ]]]

also, since the icon isnt state dependent (?) id set that in the button_config itself.

nope, none of these 3 will work, because they are not correct :wink: you need to set the color to the icon, which you dont do in either of the 3 examples. (the last 2 also have incorrect templates swill not work. there should be a . before attributes, and states['media_player.sonos_living_room.attributes.sonos_group'] simply isnt the way the state attributes are found.

Thank you–I’m making some progress, but I’m not there yet.

I have color_type set to card, and I’m actually trying to get the card color to change (the icon will stay white, or maybe change color on state only). For that reason–and because it’s where my entity states have been responsible for changing colors until now–I’ve been playing around in this section.

So, I used your code and replaced icon with card, and while I do get a color change, the card is black when playing; and black regardless of the value of sonos_group:

state:
  - value: playing
    styles:
      card:
        - color: >
            [[[ return
            entity.attributes.sonos_group.includes('media_player.sonos_kitchen')
            ? 'orange' : 'blue'; ]]]

Had I seen orange and blue, I could claim success, but I’m not there yet. Thanks for looking at this and providing suggestions; any ideas to push me over the finish line?

can you confirm the template in dev tools:

{{'media_player.sonos_kitchen' in state_attr('media_player.sonos_living_room','sonos_group') }}

works ? If not, please post the result of

{{state_attr('media_player.sonos_living_room','sonos_group')}}

also, take out color: auto

do note that setting

card: 
  - color

sets the color to the items in the card (except icon)… so name, state etc. if you want the card itself to get a color, you need background-color:

        card:
          - color: var(--text-color-off)
          - background-color: var(--background-color-off)

Yes! I checked this in my script, linked above. I watch the value toggle by grouping and ungrouping in the Sonos app.

I did in fact remove color: auto before the last attempt. I’m not sure where I’d put this code… at the highest level?

        card:
          - color: var(--text-color-off)
          - background-color: var(--background-color-off)

I should note that these state and color changes work fine:

state:
  - color: rgba(0, 200, 0, .7)
    icon: mdi:speaker
    value: paused
  - color: rgba(169, 169, 169, .7)
    icon: mdi:speaker
    value: idle
  - color: rgba(200, 0, 0, .7)
    icon: mdi:close-circle-outline
    value: unavailable

great, so the js template should be ok too (always check in inspector to notice any syntax errors though…)

I guess it is the (in) correct configuration of your button config that prevents it from doing what you want …

Well… using this, I get the icon to correctly reflect the sonos_group values (blue and orange):

type: custom:button-card
color_type: card
aspect_ratio: 2/1
entity: media_player.sonos_living_room
name: Living Room
show_name: true
tap_action:
  action: call-service
  service: script.turn_on
  service_data:
    entity_id: script.sonos_living_room_join_unjoin_kitchen
show_state: false
state:
  - value: playing
    styles:
      icon:
        - color: >
            [[[ return
            entity.attributes.sonos_group.includes('media_player.sonos_kitchen')
            ? 'orange' : 'blue'; ]]]
  - value: paused
    color: rgba(0, 200, 0, .7)
    icon: mdi:speaker
  - value: idle
  - color: rgba(169, 169, 169, .7)
    icon: mdi:speaker
  - color: rgba(200, 0, 0, .7)
    icon: mdi:close-circle-outline
    value: unavailable
styles:
  grid:
    - grid-template-areas: '"i" "n"'
    - grid-template-rows: 2fr 20%
  icon:
    - color: white
    - height: 35px
    - padding-top: 5px
  card:
    - height: 80px
  name:
    - font-size: 14px
    - color: white

Now I just need to fool around with it until I can get the card to change colors.

Yep, and start doing that by always keeping the - for value, you make several edits that lead to errors.
(See idle)

Ah, right. Thanks. I first created this card a long time ago from copying & pasting examples, and I’ve learned a bit since then. This card and every similar one started this morning with - color , and now I’m remedying that.

I’ve been at this little problem for a few hours and I’m stuck. I frankly don’t get why icon: works and card: doesn’t. I can change the setting of color_type: but that royally screws up the card.

just so you dont misunderstand me:
the order isnt perse important, but it keeps you from making mistakes like:

  - value: paused
    color: rgba(0, 200, 0, .7)
    icon: mdi:speaker
  - value: idle
  - color: rgba(169, 169, 169, .7)
    icon: mdi:speaker
  - color: rgba(200, 0, 0, .7)
    icon: mdi:close-circle-outline
    value: unavailable

where you probably meant:

  - value: paused
    icon: mdi:speaker
    styles:
      icon:
        - color: rgba(0, 200, 0, .7)
  - value: idle
    icon: mdi:speaker-off
    styles:
      icon:
        - color: rgba(169, 169, 169, .7)
  - value: unavailable
    icon: mdi:close-circle-outline
    styles:
      icon:
        - color: rgba(200, 0, 0, .7)

Well, those colors were relevant to the card (state) and it worked as I’d hoped, at least until my requirements changed earlier today. At this point I’m not sure how to accomplish the card-color-change based on sonos_group.

No, the order isn’t important, but putting the value of state first is more logical for me than color. In the original code, I wanted to change the card color and not the icon.

EDIT: The corrected code in your last post does something different from mine; mine may (?) be bad form, but the icon changes in form as intended, and the card changes in color as intended. Your corrected code changes the icon color instead of the card, and the card turns black instead of whatever color it was before. That’s not what I was looking to do when I put these buttons together sometime last year.

Ah well. I’m still playing with it and not finding any way to do it, so maybe it’s not possible with the button card.

EDIT: In the end… I accomplished what I wanted to do with a few template sensors and conditional cards. The button backgrounds change according to whether the speakers are grouped or not, playing or not, etc. As a bonus, I can call the join & unjoin services directly instead of using scripts. My only remaining gripe is that using conditional cards in this way–showing one button or the other, arranged in a horizontal stack–results in a slight horizontal offset; i.e., things don’t line up as nicely. Oh well.