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.
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.
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:
Unfortunately neither work 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?
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.
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