In card, access label attached to entity - custom button-card and card-mod

I am creating a dashboard that includes smart Meross power strips in my home. Although specific sockets currently are connected to specific electrical devices, these can often be changed around. Therefore I am trying not to put the name of the electrical item in the entity name for each socket.

Currently, I am using the label tag to hold the device name against the entity. This has led me to wonder whether I can access this label in a card that toggles these switches. As of yet, I simply have a text under the icon that is a Switch 1 to 6 text. It would be better if I could replace this with the label associated.

Currently:

2024-10-31

In this image, Switch 4 on the first card relates to the “Living room left plugs 1 Outlet 4” entity. As can be seen below, this has the label Hildebrand Glo

The code for this icon button is:

                - type: custom:button-card
                  show_entity_picture: true
                  label: Switch 4
                  entity: >-
                    switch.smart_switch_xxxxxxxxxxxxxxxxxxutlet_4
                  state:
                    - value: "off"
                      icon: mdi:power-plug
                      color: grey
                    - value: "on"
                      icon: mdi:power-plug
                      color: yellow
                    - value: away
                      icon: mdi:power-plug
                      color: red
                    - value: unavailable
                      icon: mdi:power-plug
                      color: red
                  tap_action:
                    action: toggle
                  show_state: false
                  show_label: true
                  show_name: false
                  size: 20%

Can I set the “label” on the card to the “Label” attached to the entity and, if so, what code retrieves this?

I don’t see any indication in Custom Button Card’s docs that it has a way to access an entity’s labels.

I am hoping this can be done with macros and as such, have written one that certainly works in Dev Tools> Template

{%- macro return_label_for_entity(entityname) -%}
	{% set entitynameregex = (entityname  | regex_replace("[^A-Za-z0-9]", "")) %} 
	{% set ns = namespace(foo=entitynameregex[-1]) %}
	{% set label_ids = labels() %}
	{%- for l in label_ids -%}
		{% set ln = label_name(l) %} 
		{% set ent = label_entities(l) %} 
		{% set entregex = (ent | regex_replace("[^A-Za-z0-9]", "")) %} 
		{% if entitynameregex == entregex %}
			{% set ns.foo = ln %}
			{% break %}
		{% endif %}
	{% endfor %}
	{{- ns.foo | regex_replace("Powering ", "") -}}
{% from 'label_tools.jinja' import return_label_for_entity %}
{% set labtext = return_label_for_entity('switch.smart_switch_2103263136625690847448e1e9679b57_outlet_1') %}
{{ labtext }}

Output:

Result
Result type: string
Raspberrypihifi

So the basic jinja works. However, all my attempts to get this to even be accepted in Custom:button-card using card-mod, has failed to even be saved in the card ui.

This is my latest attempt that will not even be accepted as correctly formatted style:

            - type: horizontal-stack
              cards:
                - type: custom:button-card
                  show_entity_picture: true
                  label: Switch 1
                  entity: >-
                    switch.smart_switch_2103263136625690847448e1e9679b57_outlet_1
                  state:
                    - value: "off"
                      icon: mdi:power-plug
                      color: grey
                    - value: "on"
                      icon: mdi:power-plug
                      color: yellow
                    - value: away
                      icon: mdi:power-plug
                      color: red
                    - value: unavailable
                      icon: mdi:power-plug
                      color: red
                  tap_action:
                    action: toggle
                  show_state: false
                  show_label: true
                  show_name: false
                  size: 20%
                  style: |
                    ha-card {
                    label: 
                       {% from 'label_tools.jinja' import return_label_for_entity %}
                       {% set labtext = return_label_for_entity(config.entity) %}
                       {{ labtext }}
                    }

There are a few things I think may be an issue but not really sure.

  1. config.entity - is this just the entity id in text format or the whole entity including attributes? All I want is the text.
  2. macros - I have seen suggestions that macros can be used in card-mod but no actual examples. Should it be the same format as my template testing above or does it need to change?
  3. As “label” is in custom:button-card, I assumed this was an ha-card attribute that could be changed. Am I wrong? In which case, is there some other attribute I can use for the text under icon?

I realise this has become more of a card-mod question so have moved the query into the 🔹 Card-mod - Add css styles to any lovelace card discussion.