Lovelace: Button card

Yes, I’m using yaml mode, I’ve put after the resources: without using the !include way and strangely does not work

title: My home
resources:
  - url: /customcards/custom-lovelace/thermostat-card/thermostat-card.js?track=true
    type: module
.....

qnap_button_card_template:
  color_type: icon
  size: 30px
  show_label: true
  show_name: true
  action: none
  styles:
    icon:
      - height: 18px
    card:
      - height: 80px
    name:
      - color: white
      - font-size: 16px
    label:
      - font-size: 12px
  show_last_changed: true

EDIT:

Done it!

I’ve done your way, with includes, and now it’s working, I was missing the button_card_templates: and then the template

1 Like

Cool, it took me some time, but I have some working result now. From here I can build it out to include more stuff. Thanks for the help.

Result:

5 Likes

hello
is there anyway to define color according to lamp color (xiaomi) to the text only when on

image

i search this solution that is not the ideal for me…the real want that i wish is somethink like this:

image

looking very nice indeed!
how did you calculate the buttons widths, above the actual light buttons?

also, I take it this is desktop view? does it fit the mobile device also?

You can have the buttons call the MQTT service in the on tap action and then have something listen for the correct messages. I do all my logic in node red so i have node red listen for some mqtt actions. This is how I do the dim lights button below where it sends a message to mqtt and node red listens and dims some lights and turns some off.

image

I would like to have a card like yours for my fan/light. Right now I have the below setup but yours is much more compact.

image

If you want something that works right now, pop over to the fan-control-entity-row page and setup the custom card. This card works well is the inspiration behind my attempt to recreate it using button-card.

If you’d rather work from my incomplete configuration, here it is. As of now, the fan speed, on/off button and the button coloring are either incomplete. don’t work, or both. Also, don’t forget to change my entity ids to your own.

I offer this purely as a courtesy. As I said, this only partially works in my environment. I may abandon my efforts since it appears that to do this properly, I may have to learn Jinja 2 which may be outside of my capacity to do at this point. Good luck!

Button Templates

button_card_templates:
  ceiling-fan-icon-settings:
    show_icon: true
    show_label: false
    show_name: false
    styles:
      card:
        - height: 36px
        - width: 50px
      grid:
        - grid-template-areas: '"i"'
        - grid-template-columnns: auto
      icon:
        - height: 26px
        - width: 26px
    state:
      - value: 'on'
        # styles:
        #   icon:
        spin: true

  ceiling-fan-name-settings:
    show_icon: false
    show_label: false
    show_name: true
    styles:
      card:
        - height: 36px
      grid:
        - grid-template-areas: '"n"'
        - grid-template-columnns: auto
      name:
        - justify-self: start
    state:
      - value: 'on'
        styles:
          name:
            color_type: icon
            color: rgb(181, 142, 49)

  fan-speed-settings:
    show_icon: false
    styles:
      card:
        - height: 35px
        - font-size: 12px
        - width: 35px
        - border: 1px solid white
        - border-radius: 5px
        - text-transform: uppercase
        - --paper-card-background-color: grey
      icon:
        - icon-size: 14px
    state:
      - value: 'on'
        styles:
          name:
            - color: blue
      - value: 'off'
        styles:
  vertical-divider-grey:
    color_type: blank-card
    styles:
      card:
        - width: 3px
        - color: '#292929'

  horizontal-divider-grey:
    color_type: blank-card
    styles:
      card:
        - height: 3px
        - background-color: '#292929'

  horizontal-divider-blk:
    color_type: blank-card
    styles:
      card:
        - height: 3px
        - background-color: '#222222'

Card

- type: custom:vertical-stack-in-card
  cards:
    - type: custom:button-card
      template: horizontal-divider-grey
    - type: horizontal-stack
      cards:
        - type: custom:button-card
          template: ceiling-fan-icon-settings
          entity: fan.living_room_fan
          tap_action:
            action: call-service
            service: fan.toggle
            service_data:
              entity_id: fan.living_room_fan
        - type: custom:button-card
          template: vertical-divider-grey
        - type: custom:button-card
          template: ceiling-fan-name-settings
          entity: fan.living_room_fan
          name: Fan
          tap_action:
            action: call-service
            service: fan.toggle
            service_data:
              entity_id: fan.living_room_fan

        - type: custom:button-card
          entity: fan.living_room_fan
          template: fan-speed-settings
          name: low
          tap_action:
            action: call-service
            service: fan.set_speed
            service_data:
              entity_id: fan.living_room_fan
              speed: low
        - type: custom:button-card
          template: vertical-divider-grey
        - type: custom:button-card
          entity: fan.living_room_fan
          template: fan-speed-settings
          name: med
          tap_action:
            action: call-service
            service: fan.set_speed
            service_data:
              entity_id: fan.living_room_fan
              speed: medium
        - type: custom:button-card
          template: vertical-divider-grey
        - type: custom:button-card
          entity: fan.living_room_fan
          template: fan-speed-settings
          name: high
          tap_action:
            action: call-service
            service: fan.set_speed
            service_data:
              entity_id: fan.living_room_fan
              speed: high
        - type: custom:button-card
          template: vertical-divider-grey
        - type: custom:button-card
          entity: fan.living_room_fan
          template: fan-speed-settings
          name: Off
          tap_action:
            action: call-service
            service: fan.toggle
            service_data:
              entity_id: fan.living_room_fan
        - type: custom:button-card
          template: vertical-divider-grey
    - type: custom:button-card
      template: horizontal-divider-grey

Can the new templates work with the storage mode, if I am not using the yaml mode? Or do I need to go back to using yaml mode.

I’ve been trying to figure out where to place the template in the UI editor but so far no places have worked.

YAML mode only.
Edit: It’s possible in storage mode also.

I think I’m attempting to do something unorthodox. I’m trying to check for the value in speed_list, an attribute of a fan entity and then apply coloring to the appropriate Low, Medium, High button. The snippet below would appear once in each card with the value low/medium/high where appropriate.

Am I doing this right? (Syntax adopted from a previous Mariusthvdb example.)

state:
  - operator: template
    value:
      return states['fan.office_fan'].speed_list === 'low'
    color: rgb(181, 142, 49)
  - operator: default

EDIT: After reading through the States section under Templating, I suspect I should be using return state_attr instead of return state but can’t seem to get that working either.

You are missing attributes and a >:

value: >
  return states['fan.office_fan'].attributes.speed_list === 'low'

Also, the templates are not the one from home-assistant for this button. This is plain javascript,check the documentation on github.

I needed to clear the cache and hard reset, now it looks like it is pulling the template

Way out of my league here, So might misunderstand. But I’m not in YAML mode and are using the templates. Not by !including but by adding the template code using the “Raw editor”.

2 Likes

Right, so if that’s possible, it’s good :slight_smile: (I never used storage mode and thought you couldn’t edit the “yaml” directly)
I’ll edit my post, thanks for pointing it out.

That explains it, thank you. I’ll disregard the HA templating docs.

I made the change you suggested and after some trial and error, this is the new statement.:

return states['fan.office_fan'].attributes.speed === 'low'

The results, however, are puzzling. My desired text effects are not applying. I’ve confirmed that the above statement does indeed return true when I set the fan speed to low. I suspect there is a conflict somewhere between my button template and the card but I can’t see it.

Template

button_card_templates:
  fan-speed-settings:
    show_icon: false
    styles:
      card:
        - height: 30px
        - font-size: 12px
        - border: 1px solid white
        - border-radius: 5px
        - text-transform: uppercase
      icon:
        - icon-size: 14px

Card

- type: custom:vertical-stack-in-card
  cards:
    - type: horizontal-stack
      cards:
        - type: conditional
          conditions:
            - entity: fan.office_fan
              state: "on"
          card:
            type: horizontal-stack
            cards:
              - type: custom:button-card # Low Button
                template: fan-speed-settings
                name: low
                tap_action:
                  action: call-service
                  service: fan.set_speed
                  service_data:
                    entity_id: fan.office_fan
                    speed: low
                state:
                  - operator: ==
                    value: >
                      return states['fan.office_fan'].attributes.speed === 'low'
                    styles:
                      name:
                        - color: rgba(181, 142, 49, 0)
                  - operator: default
                    styles:
                      name:
                        - color: '#292929'

Operator should be operator: template :slight_smile:

Ugh… I really shouldn’t do this when I’m tired. Thank you!

Hi there
thanks for the hint
i have tried like this but no luck:

      - color: auto
        color_type: card
        dbltap_action:
          action: more-info
        entity: light.yeelight_color1_04cf8c9557ff
        icon: 'mdi:floor-lamp'
        name: CANDEEIRO DE PÉ
        show_entity_picture: true
        size: 80%
        state:
          - entity_picture: /local/images/salapeon.jpg
            value: 'on'
          - entity_picture: /local/images/salapeoff.jpg
            value: 'off'
            styles:
              entity_picture:
                - transform: rotate(90deg)

when i change size the image resizes, but the card also resizes…i want to freeze the card size, but change the image size. is it possible?

has for this hint:

state:
- value: 'on'
  styles:
    name:
      - background-color: your_color
- value: 'off'
  styles:
    name:
      - background-color: other_color

it works great…but is there anyway that the background is applied to the whole line?
image

i AM REALLY LOVING THIS CARD !!!

I don’t understand what you are trying to achieve? Can you show me what happens?

You could try:

styles:
  name:
    - width: 100%

Yes of course
so for question number 1:
i want that the 3rd light have always the same size as 1 and 2
i want to reduce the image size
image

then i want to apply question 2 in all cards so that for example on card 2 i have:
IMAGE
STATE OF THE IMAGE “SALA DE JANTAR” WITH FULL ROW IN COLOR RELATED TO STATE

question numner 2:

tried:

        show_entity_picture: true
        size: 80%
        state:
          - value: 'on'
            entity_picture: /local/images/HallOFF.jpg
          - styles:
              name:
              width: 100%
              background-color: 'rgb(255, 228, 181)'
          - entity_picture: /local/images/HallON.jpg
            value: 'off'

but background color does not change :frowning: (only want background of the ‘name’ to change, not the card)

image
image

:tada::tada: Version 1.10.0 :tada::tada:

Feedback welcome as alway :slight_smile:

BREAKING CHANGES

  • I had to change some default styles to support aspect ratio and icon auto-resizing, this might break some of your custom styles. Hopefully not too much. Feel free to request some help on the forum.
  • Deprecate style. Same result with:
    • before:
      style:
        - font-size: 10px
      
    • after:
      styles:
        card:
          - font-size: 10px
      

NEW FEATURES

  • You can now merge states together by id when using templates. The states you want to merge have to have the same id. This id parameter is new and can be anything (string, number, …). States without id will still be appended. Styles embedded in a state are merged together as usual. Fixes #165

    button_card_templates:
      sensor:
        styles:
          card:
            - font-size: 16px
            - width: 75px
        tap_action:
          action: more-info
        state:
          - color: orange
            value: 75
            id: my_id
    
      sensor_humidity:
        template: sensor
        icon: 'mdi:weather-rainy'
        state:
          - color: 'rgb(255,0,0)'
            operator: '>'
            value: 50
          - color: 'rgb(0,0,255)'
            operator: '<'
            value: 25
    
      sensor_test:
        template: sensor_humidity
        state:
          - color: pink
            id: my_id
            operator: '>'
            value: 75
            styles:
              name:
                - color: '#ff0000'
    ############### Used like this ##############
      - type: custom:button-card
        template: sensor_test
        entity: input_number.test
        show_entity_picture: true
    

    Will result in this state object for your button (styles, operator and color are overridden for the id: my_id as you can see):

    state:
      - color: pink
        operator: '>'
        value: 75
        styles:
          name:
            - color: '#ff0000'
      - color: 'rgb(255,0,0)'
        operator: '>'
        value: 50
      - color: 'rgb(0,0,255)'
        operator: '<'
        value: 25
    
  • Icon will auto adapt to the height of the card and not only to it’s width. This will enable you to skip using size: xx% when you defined the width, height or aspect-ratio of a card.

  • You can now define an aspect ratio for your cards using aspect_ratio in the main config, and the card will adapt to all of your screen size. This will enable you to skip setting the width and the height of a card to make it square for example.

    image

    - type: vertical-stack
      cards:
        - type: horizontal-stack
          cards:
            - type: custom:button-card
              name: 1/1
              icon: mdi:lightbulb
              aspect_ratio: 1/1
            - type: custom:button-card
              name: 2/1
              icon: mdi:lightbulb
              aspect_ratio: 2/1
            - type: custom:button-card
              name: 3/1
              icon: mdi:lightbulb
              aspect_ratio: 3/1
            - type: custom:button-card
              name: 4/1
              icon: mdi:lightbulb
              aspect_ratio: 4/1
        - type: horizontal-stack
          cards:
            - type: custom:button-card
              name: 1/1.2
              icon: mdi:lightbulb
              aspect_ratio: 1/1.2
            - type: custom:button-card
              name: 1/1.3
              icon: mdi:lightbulb
              aspect_ratio: 1/1.3
            - type: custom:button-card
              name: 1/1.4
              icon: mdi:lightbulb
              aspect_ratio: 1/1.4
            - type: custom:button-card
              name: 1/1.5
              icon: mdi:lightbulb
              aspect_ratio: 1/1.5
    
  • New option to ignore the light temperature (Fixes #153): color: auto-no-temperature

  • name_template and entity_picture_template. Also available per state (because why not). For both, the javascript code needs to return a string. Fixes #162

  • color: auto-no-temperature: This will have the same effect as color: auto but will ignore your light temperature.

  • When action: more-info, you can now define also an entity other than the one assigned to the card (Fixes #163):

    - type: custom:button-card
      entity: light.my_light
      tap_action:
        action: more-info
        entity: sensor.my_sensor
    
5 Likes