Lovelace: Button card

I’m currently using the url action / url_path capability of the button-card to navigate to a Grafana URL for displaying a historical graph. My problem is that I access my HA using both the LAN IP address as well as a VPN IP address and the url_path needs to be specific to how the user is accessing the system.

Is there a way to include a system variable in the path which has the hostname or IP address that the current connection is using to access HA? Or some other way to dynamically generate the url_path based on the hostname / IP address being used to access HA?

Someone has an idea?

My code above has the percentage. You will need to incorporate this to your existing code to have the last changed as well.

@sparkydave i cant figure it out, how to display both of them at the same time

It is indeed possible and not that difficult !

You want to have a look at custom fields, and learn the basics of css grid.

With a little bit of JS & templating, you can pull any value available in HA and display it however you want.

1 Like

really puzzled here… trying to template the icon of a button:

icon: >
  [[[ return 'mdi:weather-' + entity.attributes.awareness_type.split(';')[1].toLowerCase(); ]]]

where the second part equals ‘fog’.I’m sure of that because I’ve got the exact code as a value for another field and it shows fog…

there is no icon displayed unfortunately, while when using

icon: mdi:weather-fog

displays fine. What am I doing wrong here??

type: custom:button-card
template: button_body
entity: 'binary_sensor.meteoalarm_brabant'
aspect_ratio: 1/1
name: >
  [[[ return 'Meteo Alarm (' + entity.state + ')']]]
icon: >
  [[[ return 'mdi:weather-' + entity.attributes.awareness_type.split(';')[1].toLowerCase(); ]]]

secondly, as you can see I try to use the ‘state’ in the name template. It shows ‘on’, which is the state… I like it to show ‘unsafe’ though, which is displayed when using
‘show_state: true’
which I dont use, because that takes another row on the button… can I somehow show up the Unsafe directly? (of course I can create another template based on state, but that would be silly since it should be there somewhere)

interested in this as welll…

come on guys…someone has to be able to put the mini graph inside of this button card.

What are you trying to achieve with a mini-graph-card inside the button-card? If you’d explain it a bit more, there may be other solutions. :slight_smile:

Tried without success, thanks anyway

I wanted to do this simply so that I can have my graph the same size as all my buttons.

Am I missing something that allows me to do this?

image

Hi,

How you displayed both last changed : “4 hours ago”
And battery percentage “62%” at the same time .

Would you mind share your code ?

Thank you

I can but my config uses templates so is quite complex (and very probably not the most elegent as I built it up over a long time as the decluttering card and the button card evolved) and it is also quite specific to my use.

However, the button card has an option

        show_last_changed: true

which might be all you need to know.

Let me know if you need more than this and I will try an help.

If you put the mini-graph-card and the button-card together in a horizontal-stack-card, you can assign a height attribute to the button-card. That should do the trick.

Would you mind posting your code, so I can take a look at it?

Take a look at the documentation, exactly this is shown with an example.

Here (the second example, the one with the blueisch background): https://github.com/custom-cards/button-card#custom-fields

Sorry, I’m sure this is a simple question, but I’m having trouble figuring this out. I want to display brightness when the light is on, but when it’s off I just want it to say “Off”

I have this to display brightness and it works great

        label: >
          [[[ var bri =
          Math.round(states['light.left_shelf_lights'].attributes.brightness /
          2.55); 
		  return 'Brightness ' + (bri ? bri : '0') + '%'; ]]]

I saw some If logic in the guides, but could not figure out how to combine

EDIT:
Answering my own question

          [[[
            if (states['light.living_room_fan_light'].state == 'off') return "Off";
            else return 'Brightness ' + (Math.round(states['light.living_room_fan_light'].attributes.brightness / 2.55)) + '%'; 
          ]]]

@paddy0174
As I said it may not be the most efficient way of doing things, it has grown organically over the months…

          - type: custom:decluttering-card
            template: standard_square_button
            variables:
              - entity: sensor.hall_battery_level
              - name: Battery
              - unknown_icon: battery-alert
              - tap_action: more-info
              - show_last_changed: true

Template:

  #===========================
  #=== Standard Square Button
  #===========================
  default:
    - size: 30%
    - card_height: none
    - lock_enabled: false
    - color: auto
    - name_color: auto
    - spin: false
    - show_name: true
    - show_state: true
    - show_label: false
    - show_icon: true
    - show_last_changed: false
    - layout: vertical
    - tap_action: none
    - double_tap_action: none
    - hold_action: none
    - aspect_ratio: 1/1
    - name: '&nbsp'
    - label: '&nbsp'
    - opacity: 0.6
    - on_icon_color: yellow
    - active_icon_color: yellow      
    - playing_icon_color: yellow
    # - on_icon_color: var(--secondary-text-color)
    # - active_icon_color: var(--secondary-text-color)      
    # - playing_icon_color: var(--secondary-text-color)
    - box_shadow: 0px 0px 2px 1px
    # - box_shadow: 0px 0px 2px 1px var(--secondary-text-color)
    # - box_shadow: 0px 0px 8px 2px var(--secondary-text-color)
    - box_shadow_color: var(--secondary-text-color)
    # - box_shadow_color: var(--paper-item-icon-active-color)
  card:
    type: custom:button-card
    entity: '[[entity]]'
    name: '[[name]]'
    lock:
      enabled: '[[lock_enabled]]'
    size: '[[size]]'
    aspect_ratio: '[[aspect_ratio]]'
    label: '[[label]]'
    layout: '[[layout]]'
    color: '[[color]]'
    show_name: '[[show_name]]'
    show_icon: '[[show_icon]]'
    show_state: '[[show_state]]'
    show_label: '[[show_label]]'
    show_last_changed: '[[show_last_changed]]'
    tap_action: 
      action: '[[tap_action]]'
    double_tap_action:
      action: '[[double_tap_action]]'
    hold_action:
      action: '[[hold_action]]'
    styles:
      lock:
        - color: red
      card:
        - height: '[[card_height]]'
        - border-radius: 6px
        - padding-left: 5px
        - opacity: '[[opacity]]'
      name:
        - color: '[[name_color]]'
        - justify-self: start
        - text-align: start
        - font-family: Helvetica 
        - font-size: 13px
        - font-weight: bold
      label:
        - justify-self: start
        - text-align: start
        - font-family: Helvetica
        - font-size: 11px
        - opacity: '[[opacity]]'
      state:
        - justify-self: start
        - text-align: start
        - font-family: Helvetica
        - font-size: 11px
        - font-weight: bold
        - text-transform: capitalize
      grid:
        - grid-template-areas: '"i" "n" "s" "l"'
        - grid-template-columns: 1fr
        - grid-template-rows: 1fr min-content min-content
      img_cell:
        - justify-content: start
        - align-items: start

    state:
      #=== Used for Windows and Doors
      #=== Used for Lights
      #=== Used for TV On/Off Sensor
      - value: 'on'
        icon: '[[[ 
                  if (entity.entity_id.includes("door")) return "mdi:door-open";
                  else if (entity.entity_id.includes("window")) return "mdi:window-open";
                  else if (entity.entity_id.includes("light")) return "mdi:lightbulb-on";
                  else return "mdi:[[on_icon]]";
               ]]]'
        styles:
          card:
            - opacity: 1.0
            - box-shadow: '[[box_shadow]] [[box_shadow_color]]'
            - color: var(--secondary-text-color)
          name:
            - color: black
          icon:
            - color: '[[on_icon_color]]'

      #=== Used for Motion Sensors
      - value: 'active'
        icon: '[[[ 
                  if (entity.entity_id.includes("motion")) return "mdi:motion-sensor";
                  else return "mdi:[[active_icon]]";
               ]]]'
        styles:
          card:
            - opacity: 1.0
            - box-shadow: '[[box_shadow]] [[box_shadow_color]]'
            - color: var(--secondary-text-color)
          name:
            - color: black
          icon:
            - color: '[[active_icon_color]]'

      #=== Used for Sonos and Kodi
      - value: 'playing'
        icon: 'mdi:[[playing_icon]]'
        styles:
          card:
            - opacity: 1.0
            # - box-shadow: 0px 0px 8px 2px var(--secondary-text-color)
            - box-shadow: '[[box_shadow]] [[box_shadow_color]]'
            - color: var(--secondary-text-color)
          name:
            - color: black
          icon:
            - color: '[[playing_icon_color]]'

      #=== Used for Windows and Doors
      #=== Used for Lights
      #=== Used for TV
      - value: 'off'
        icon: '[[[ 
                  if (entity.entity_id.includes("door")) return "mdi:door-closed";
                  else if (entity.entity_id.includes("window")) return "mdi:window-closed";
                  else if (entity.entity_id.includes("light")) return "mdi:lightbulb";
                  else return "mdi:[[off_icon]]";
               ]]]'
        styles:
          card:
            - opacity: 0.6

      #=== Used for Motion Sensors
      - value: 'idle'
        icon: '[[[ 
                  if (entity.entity_id.includes("motion")) return "mdi:human-handsdown";
                  else return "mdi:[[idle_icon]]";
               ]]]'
        styles:
          card:
            - opacity: 0.6

      #=== Used for Sonos and Kodi
      - value: 'paused'
        icon: 'mdi:[[paused_icon]]'
        styles:
          card:
            - opacity: 0.6

      #=== Used for Sonoff Switches
      - value: 'unavailable'
        icon: 'mdi:[[unavailable_icon]]'
        styles:
          card:
            - opacity: 0.6

      #=== Used for Battery Sensors
      - value: 'unknown'
        icon: 'mdi:[[unknown_icon]]'
        styles:
          card:
            - opacity: 1.0
            - box-shadow: '[[box_shadow]] [[box_shadow_color]]'
            - color: var(--secondary-text-color)
          icon:
            - color: red
            - animation: blink 2s ease infinite

      #=== Used for Battery Sensors (Low Charge)
      - value: '[[[ if (entity.entity_id.includes("battery")) return 40 ]]]'
        operator: <
        icon: mdi:battery-alert
        styles:
          card:
            - opacity: 1.0
            - box-shadow: '[[box_shadow]] [[box_shadow_color]]'
            - color: var(--secondary-text-color)
          icon:
            - color: red
            - animation: blink 2s ease infinite

      #=== Used for Battery Sensors (Charge Level)
      - value: '[[[ 
                    if (entity.entity_id.includes("battery")) return 0;
                    else return 101;
                ]]]'
        operator: '>='
        icon: >
          [[[
            if (entity.state < 20) return "mdi:battery-10";
            if (entity.state < 30) return "mdi:battery-20";
            if (entity.state < 40) return "mdi:battery-30";
            if (entity.state < 50) return "mdi:battery-40";
            if (entity.state < 60) return "mdi:battery-50";
            if (entity.state < 70) return "mdi:battery-60";
            if (entity.state < 80) return "mdi:battery-70";
            if (entity.state < 90) return "mdi:battery-80";
            if (entity.state < 100) return "mdi:battery-90";
            if (entity.state = 100) return "mdi:battery";
          ]]]

1 Like

ok, answering to myself here, for reference, and some followup:

Appeared there was a space in the attribute I hadn’t noticed, so the split had to be at that space, and not the semi-colon. Also, since the split is now made on the space, the ending semi colon appears in the output, so I need to cut-off that also, which leads to this

entity.attributes.awareness_level.split(’ ')[1].slice(0,-1)

and the Button is starting to be useful:
07

the icon is a bit of a hack for now and I will work on that later. First, Id like to have the custom_field for Level, which now is:

  level: >
    [[[
      return `<ha-icon
        icon=${'mdi:numeric-' + entity.attributes.awareness_level.split(';')[0] + '-box'}
        style='width: 12px; height: 12px;'>
        </ha-icon><span>Level: <span style='color: var(--text-color-sensor);'>${entity.attributes.awareness_level.split(' ')[1].slice(0,-1)}</span></span>`
    ]]]

use a var somehow to make it more readable, and to caplitalize the first letter. A bit like this:

    level:
      - padding-bottom: 2px
      - align-self: middle
      - justify-self: start
      - --text-color-sensor: >
          [[[ return entity.attributes.awareness_level.split(';')[1]; ]]]
      - --color-name: >
          [[[function capitalizeFirstLetter(string) {
            return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
            }
            var id = entity.attributes.awareness_level.split(' ')[1].slice(0,-1);
            return capitalizeFirstLetter(id);]]]

no matter what I try though, I cant get it to show in the button using the var(–color-name) in the custom_field config. Please help and see what I could do to use that?

to proof the template itself is correct, Ive tested it in the name field for the button:

18

Hello all and in particular @RomRider, long time no see/talk. Haven’t been on this thread for quite some time. I hope you are doing well.

I wanted to ask if you ever had the time to look at my old feature request (dropdown menu inside button card). I know you are busy, and I am totally not in a hurry so no worries. Though I was wondering if you could (and ofc if you are willing) to take a look at it. If you think it doesn’t suit the card I understand.

Once again I thank you for picking up this project and make it massively better than it once used to be.

Merci beaucoup mon ami!

I think it makes more sense to use a custom field for that. Try

type: custom:hui-input-select-entity-row

For starters

Do you put this inside a button card? Or is that not what you meant?

I will go and play with this, though not exactly sure how to implement it.