Accessing the button entity that called a script

I am using a custom:button-card button to call a script on the tap_action of the button. I can successfully send in an entity name to the script using the data: parameters and everything works fine.

My issue is that I have a lot of these buttons and having to specify the entity name for the button itself and then again for each action is rather cumbersome. It would be much better if the script could simply access the entity associated with the button that called it. I’m guessing that this must get passed through as if I long press the button a dialog pops up for the associated entity.

It would be most elegant if the script itself can access the button entity name but if that’s not possible, how can I template it so it sends it in data:? I’ve searchedfor many hours and found almost nothing so any help would be appreciated.

The custom:button-card code is

                    - entity: climate.chris_bedroom_aircon
                      name: Aircon
                      color: black
                      state:
                        - value: heat
                          color: DarkGoldenRod
                        - value: cool
                          color: blue
                        - value: auto
                          color: lightgreen
                        - value: 'off'
                          color: black
                      tap_action:
                        action: call-service
                        service: script.aircon_action
                        data:
                          aircon: {{ states('entity.entity_id') }}
                          hvmode: cool
                      double_tap_action:
                        action: call-service
                        service: script.aircon_action
                        data:
                          aircon: climate.chris_bedroom_aircon
                          hvmode: heat
                      template: standard
                      icon: mdi:air-filter
                      size: 90%
                      type: custom:button-card

You can see that in the double-tap action I send the actual entity name and in the tap-action I TRY (and fail) to send the entity name set at the top of the button code.

Any help would be much appreciated.

Are you actually coding {{ states(‘entity.entity_id’) }} or is that just changed for the sake of the posting? Im gonna assume that is your actual code (which won’t work). Instead try this {{ this.entity_id }}. So full code would look like this:

                    - entity: climate.chris_bedroom_aircon
                      name: Aircon
                      color: black
                      state:
                        - value: heat
                          color: DarkGoldenRod
                        - value: cool
                          color: blue
                        - value: auto
                          color: lightgreen
                        - value: 'off'
                          color: black
                      tap_action:
                        action: call-service
                        service: script.aircon_action
                        data:
                          aircon: '{{ this.entity_id }}'
                          hvmode: cool
                      double_tap_action:
                        action: call-service
                        service: script.aircon_action
                        data:
                          aircon: '{{ this.entity_id }}'
                          hvmode: heat
                      template: standard
                      icon: mdi:air-filter
                      size: 90%
                      type: custom:button-card

I have not tested this so you may have to tweak it a bit, but it should work.
Tweak as in: {{ this.entity_id.friendly_name }}

Art aka kartcon

Thanks for your quick reply.

I’ve tried both of your suggestions this morning;

aircon: '{{ this.entity_id }}'

and

aircon: '{{ this.entity_id.friendly_name }}'

Unfortunately neither work :frowning: It just sends the string as is to the script it doesn’t convert it so the script fails saying " Template rendered invalid entity IDs: aircon: ‘{{ this.entity_id.friendly_name }}’ "

I’m guessing this may be some issue with custom:button-card not processing templates.

Any other suggestions? Is there any way to access the ID from the calling button in the actual script rather than the button code?

Try it without enclosing it in single quotes

If this doesn’t work, paste the entire code for the button card so I can try to duplicate.

Art

Hi

Thanks for your suggestion. I had already tried that but unfortunately it didn’t work.

As requested I’m posting the entire button card code below. For clarity its a custom:button-card containter with 4 buttons controlling different aircon units but each button if effectively the same, simply referencing a different climate entity.

It’s the third “Chris Aircon” button I’m trying this out on and you can see the {{ this.entity_id }} reference on the tap_action but I’ve left the full entity name on the double_tap_action so I can see that still works.

type: custom:button-card
template: container
name: Air Conditioning
custom_fields:
  buttons:
    card:
      type: horizontal-stack
      cards:
        - entity: climate.orangery_aircon
          name: Orangery<br>Aircon
          color: black
          state:
            - value: heat
              color: DarkGoldenRod
            - value: cool
              color: blue
            - value: auto
              color: lightgreen
            - value: 'off'
              color: black
          tap_action:
            action: call-service
            service: script.aircon_action
            data:
              aircon: climate.orangery_aircon
              hvmode: cool
          double_tap_action:
            action: call-service
            service: script.aircon_action
            data:
              aircon: climate.orangery_aircon
              hvmode: heat
          template: standard
          icon: mdi:air-filter
          size: 90%
          type: custom:button-card
        - entity: climate.movie_room_aircon
          name: Movie Room<br>Aircon
          color: black
          state:
            - value: heat
              color: DarkGoldenRod
            - value: cool
              color: blue
            - value: auto
              color: lightgreen
            - value: 'off'
              color: black
          tap_action:
            action: call-service
            service: script.aircon_action
            data:
              aircon: climate.movie_room_aircon
              hvmode: cool
          double_tap_action:
            action: call-service
            service: script.aircon_action
            data:
              aircon: climate.movie_room_aircon
              hvmode: heat
          template: standard
          icon: mdi:air-filter
          size: 90%
          type: custom:button-card
        - entity: climate.chris_bedroom_aircon
          name: Chris<br>Aircon
          color: black
          state:
            - value: heat
              color: DarkGoldenRod
            - value: cool
              color: blue
            - value: auto
              color: lightgreen
            - value: 'off'
              color: black
          tap_action:
            action: call-service
            service: script.aircon_action
            data:
              aircon: {{ this.entity_id }}
              hvmode: cool
          double_tap_action:
            action: call-service
            service: script.aircon_action
            data:
              aircon: climate.chris_bedroom_aircon
              hvmode: heat
          template: standard
          icon: mdi:air-filter
          size: 90%
          type: custom:button-card
        - entity: climate.andy_bedroom_aircon
          name: Andy<br>Aircon
          color: black
          state:
            - value: heat
              color: DarkGoldenRod
            - value: cool
              color: blue
            - value: auto
              color: lightgreen
            - value: 'off'
              color: black
          tap_action:
            action: call-service
            service: script.aircon_action
            data:
              aircon: climate.andy_bedroom_aircon
              hvmode: cool
          double_tap_action:
            action: call-service
            service: script.aircon_action
            data:
              aircon: climate.andy_bedroom_aircon
              hvmode: heat
          template: standard
          icon: mdi:air-filter
          size: 90%
          type: custom:button-card

I thought it might be useful to post the aircon-action script too which I’ve kept very simple at the moment.

alias: aircon_action
sequence:
  - variables:
      aircon_entity: "{{ aircon }}"
      aircon_current_mode: "{{ states(aircon_entity) }}"
      aircon_new_mode: |-
        {% if aircon_current_mode == 'cool' or aircon_current_mode == 'heat' %}
          off
        {% else %}
          {{ hvmode }}
        {% endif %}
  - target:
      entity_id: "{{ aircon }}"
    data:
      hvac_mode: "{{ aircon_new_mode }}"
    action: climate.set_hvac_mode
description: ""
icon: mdi:air-conditioner

Thanks for any advice.

After digging into the card details and looking over your code again, I don’t think there is an easier way to accomplish what you want. Because it’s a custom card that generates the call to the script, that card would need to support the ability to ‘know’ which entity is associated with an action. I think simply defining it as you do in the double tap action may be your only option. I did look at some of the advanced options such as custom fields, but I don’t think it’s not designed to do what you want.
Honestly, sometimes the simple solution is the best, even if it requires lots of copy/paste repetitions.
Best of luck, and post the solution if you find a better way.
Art aka kartcon

What you want to achieve is perfectly feasible, but if you check the documentation for the card, you’ll see that it uses JavaScript notation, not jinja2 for its templates.