custom-cards / button-card

❇️ Lovelace button-card for home assistant
MIT License
1.97k stars 242 forks source link

Pass entity name as variable and use that in template #778

Closed arifroni closed 1 year ago

arifroni commented 1 year ago

I want to pass an entity name via a variable for the template. And use that variable in template. How can I do that?

For example: my card

- type: custom:button-card
  entity: switch.eh_kitchen_sw_switch
  name: Ceiling
  variables:
    real_light_entity: "light.diningroom"
  template:
    - light_switch

In the following template title is the friendly name of the entity (switch.eh_kitchen_sw_switch). But I want to use the friendly name of the variable (light.diningroom). How do I write that? template:

light_switch:
  double_tap_action:
    action: fire-dom-event
    browser_mod:
      service: browser_mod.popup
      data:
        title: >
          [[[
            return !entity || entity.attributes.friendly_name;
          ]]]
smartqasa commented 1 year ago

Try this...

light_switch:
  double_tap_action:
    action: fire-dom-event
    browser_mod:
      service: browser_mod.popup
      data:
        title: >
          [[[
            return states[variables.real_light_entity] ? states[variables.real_light_entity].attributes.friendly_name : "Unknown";
          ]]]

This is a helpful site... https://www.w3schools.com/jsref/jsref_operators.asp