This thread is for custom:button-card. You only posted a very brief snippit of your card config so I’m only guessing this question should be posted in Custom card: Room Card?
I’m trying to use the internal variables of the card to set the variables the card will eventually pass towards a service. I.e. filling out the service_data fields using the button card as a ui, without needing to make helpers whose state would be used to pass the options. Which would also mean the settings are specific for a browser instance, which in the case of some custom scripts I use make more sense, as using helpers for this means the helpers need to be reset after a certain amount of time too.
Anyhow, preamble aside, where I got stuck on was performing javascript code inside custom fields, using the variables of the card itself. custom fields that are html elements are able to perform scripts, however they do not have access to the variables variable, which the card does pass to javascript snippets within configurations. My current test setup is this:
Checking the console log in my browser shows that upon loading, all javascript codes are performed. However, afterwards, only clicking on the html field triggers the on click action in the html element, as well as the tap_action of the main button. This shows that test_var does indeed toggle on each click. However, clicking on the frt custom field does not trigger the javascript code. It does read out the returned value, however (i.e. setting it to return "more-info"; throws an error saying no entity-id has been specified. However the other parts of the script are not performed. Nothing is logged in the console, nor is the variable changed. Is it possible for the action to still perform these parts of the code as well?
I’m not sure if what I’m trying is even possible, as it may be a bit too hacky (which would explain why it won’t work), but maybe someone here actually has an idea of what to do.
Sorry. Not talking about room card, but talking about a template for button card. I thought I included more of the template. Here is a fuller example. Notice how the icon for the i1 custom field is passed through as a variable? I am trying to figure out if I can pass through a template instead of just a single icon. If the entity is on, then one icon and if it is off another. But it doesn’t really seem to work. So my question was if it is possible to pass a template like that as a variable to a custom field. Does that make more sense?
My end goal is to make a room card style template and then use that template. But for something like the fireplace I want to have an icon with the flame if its on and without if it is off. But I am struggling to pass the if/else through as a variable to a template.
switch.2ch_relay_right:
templates:
icon: "return state === 'off' ? 'mdi:fireplace-off' : 'mdi:fireplace';"
icon_color: "return state === 'off' ? 'grey' : 'gold';"
Note: custom:button-card uses my custom-ui templated icon but does not use my templated icon_color which is likely related to this note in the docs:
By default, if the entity state is off , the color will be var(--paper-item-icon-color) , for on it will be var(--paper-item-icon-active-color) and for any other state it will be var(--primary-text-color).
The standard button card applies both custom-ui templates:
Honestly no, I hadn’t because I had never heard of it. Lol. That looks like a very interesting way to go though. Thanks I’ll do some reading tonight. Although it seems development has stopped…
let state = states['variables.entity_1_entity_id'].state;
Also works within entity_2 (not useful, but interesting). So it looks like entity_2 is evaluated all at one go, and nothing within it is available within the sub items.
Turns out even though entity_id is alphabetically before icon in the above example it is still undefined in the icon field. This was my issue.
If it’s alphabetical AND you can use the alphabetically earlier ones in later variables, then there are a lot of possibilities. It’s a matter of what would make sense to you looking at the code later. I’m thinking something short like this maybe?