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 a text description 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:
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 am hoping this can be done with macros and as such, have written one that certainly works in Dev Tools> Template. I have tried this on another of the buttons. It is identical in format to the one above in format.
{%- 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.
- config.entity - is this just the entity id in text format or the whole entity including attributes? All I want is the text.
- 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?
- 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?