Fun with custom:button-card

Good work on this card. Just a question what is this sensor that you are referring to sensor.g3_1_iphone

is it possible to have a button change colour depending on the state of a sensor while when pressed run a script?

I have a button for my 3D printer that runs a script to determin if the printer is safe to turn off or if it needs turned on. what I’d like to do though is have the button be the standard blue if its “Off”, be green if its “Safe” and be red if its “Not Safe”

here is what I have tried

type: custom:button-card
entity: sensor.ender_safe_shutdown
icon: mdi:printer-3d-nozzle
state:
  - value: 'Off'
    color: black
  - value: Not Safe
    color: red
  - value: Safe
    color: green
tap_action:
  action: toggle
  entity: script.ender_5_safe_switch

but when I press it, I get “Failed to call service sensor/turn_off. Service not found”

the button changes colour correctly it’s the tap action that isn’t doing as I want.

any help?

I believe, this should be:

tap_action:
  action: call-service
  service: script.ender_5_safe_switch

Thank you @suxlala I’ve just tried that and I get no error but the script is not run either

hmm, strange. Are you sure the script is working? Have you tried to run it from Developer tools/services?
Anyway, details of tap_action can be found here: Actions - Home Assistant

1 Like

Thank you Suxlala! I had to change it to this

tap_action:
  action: call-service
  service: script.toggle
  service_data:
    entity_id: script.ender_5_safe_switch

now that part works a charm and I can also use that to clean up some other buttons.

now to work out the tweeks and change the background colour but I’m sure ive seen somewhere how to do that
thank you again

Hi all!

Trying to set up a custom:button-card to change the opacity to 1 depending on what state my vacuum is in, I want it to go to 1 on all states except docked (then the “go home” card is hidden). Anyone have any thoughts on how to solves this and I dont want add one section for each state :slight_smile:

thought I could use something like this but it doesn´t work:

state:
  - value: [[[ return state['vacuum.name'].states != docked; ]]]

Hope someone can help me get this correct.
Kind regards

It’s


  state:
    - operator: template
      value: |
        [[[ return … ]]]

or


  state:
    - operator: template
      value: "[[[ return … ]]]"


Thanks for the response but all I get is this:
ButtonCardJSTemplateError: ReferenceError: state is not defined in ‘return state[‘vacuum.name’].states != ‘docked’;’

And it doesnt matter which solution i try. The both render the same error and I´ve tried with double-quotes, single, none. Same error.

when using the template in the name field it works “[[[ return state[‘vacuum.name’].states ]]]” the name changes to docked, any ideas?

Thanks!

For reference, this is the entire card:

type: custom:button-card
entity: vacuum.name
icon: mdi:robot-vacuum-variant
name: "[[[ return states['vacuum.name'].state ]]]"
styles:
  card:
    - height: 90px
    - width: 120px
    - border-radius: 10%
    - padding: 5%
    - color: white
    - font-size: 20px
    - background-color: '#000000'
    - border-width: 5px
    - border-color: darkgrey
    - border-style: solid
    - opacity: 0
  name:
    - color: white
  icon:
    - color: lightgrey
state:
  - operator: template
    value: "[[[ return states['vacuum.name'].state !='docked']]]"
    icon: mdi:home-import-outline
    tap_action:
      action: call-service
      service: vacuum.return_to_base
      service_data:
        entity_id: vacuum.name
    hold_action:
      action: none
    styles:
      card:
        - opacity: 1
1 Like

I don’t use a vacuum cleaner, so I can only test with other entities and cannot confirm the mentioned failure notice.

Think I solved it by using this:

opacity: “[[[ if (entity.state = docked) return 0; else 1 ]]]”

Haven’t tested it yet but looks promising :slight_smile:

Is it possible to use a template for colors? I can’t seem to make this work:

      - type: custom:button-card
        entity: input_boolean.set64_0
        color_type: card
        color: '{{ states("input_select.set64_colors") }}'
        aspect_ratio: 1/1
        show_name: false
        show_icon: false

It’s


color: '[[[ return states["input_select.set64_colors"].state ]]]'

But I can’t test now, if color is templatable.

it’s not: https://github.com/custom-cards/button-card#main-options

you can change color in the state: or styles: section of the button card.

It probably says something about not using jinja templates too :wink: Always good to read the docs…

i have some custom_fields in my card.
is it somehow possible to only show them when the entity is “on”.

custom_fields:
  bright: |
    {% if is_state("light.sockel_kueche", "on") -%}
    [[[
      var b = states['light.sockel_kueche'].attributes.brightness;
      return parseInt(b ? b/2.55 : '0') + '%';
    ]]]
    {%- else -%}
    {%- endif %}

Kick out all Jinja code and control the visibility of the custom field with CSS:


custom_fields:
  bright: |
    [[[
      var b = states['light. sockel_kueche'].attributes.brightness;
      return parseInt(b ? b/2.55 : '0') + '%';
    ]]]
styles:
  custom_fields:
    bright:
      - display: |
          [[[
            return states['light.sockel_kueche'].state == 'off' ? 'none' : 'block'
          ]]]

1 Like

just noticing you use parseInt() where I have Math.round(entity.attributes.brightness/2.55)

would there be any advantage of one over the other?

amazing!!! Thank you very much

I don’t like to change great codes =)
but that’s how it should work, right?

I have a few buttons that all look the same… and I think with this adjustment I “only” need to select the entity afterwards and then I wouldn’t have to adjust everything in the code anymore, right?

custom_fields:
  bright: |
    [[[
      var b = states['light. sockel_kueche'].attributes.brightness;
      return parseInt(b ? b/2.55 : '0') + '%';
    ]]]
styles:
        bright:
          - display: |
              [[[
                return entity.state == 'off' ? 'none' : 'block'
              ]]]

do you think so ?

custom_fields:
  bright: |
    [[[
      var b = entity.attributes.brightness ? Math.round(entity.attributes.brightness/2.55) : '';
      return parseInt(b ? b/2.55 : '0') + '%';
    ]]]