custom-cards / button-card

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

Unable to use template variable for input_select entity values #686

Closed cyr123 closed 1 year ago

cyr123 commented 1 year ago

Checklist

Describe the bug I'm trying to use a number of button cards to display and select the options in an input_select helper. I managed to get it working, but I want to use a template to avoid a lot of code duplication. I'm trying to use a variable to map each button to a specific value as shown below, but neither the state indication (color) or tap_action works.

I tried a number of things that do work:

Disclaimer: Although I am a software developer, I have next to zero JS or web development experience and YAML makes me go cross-eyed so I may be missing something obvious...

Version of the card Version: 3.5.0

To Reproduce This is the configuration I used:

button_card_templates:
  selector:
    color_type: label-card
    name: '[[[ return variables.value; ]]]'
    state:
      - value: '[[[ return variables.value; ]]]'
        color: green
    tap_action:
      action: call-service
      service: input_select.select_option
      service_data:
        entity_id: entity
        option: '[[[ return variables.value; ]]]'
views:
  - title: Home
    cards:
      - type: custom:button-card
        template: selector
        entity: input_select.home_theater_aspect_ratio
        variables:
          value: '1:43.1'
      - type: custom:button-card
        template: selector
        entity: input_select.home_theater_aspect_ratio
        variables:
          value: '1:66.1'
      - type: custom:button-card
        template: selector
        entity: input_select.home_theater_aspect_ratio
        variables:
          value: '1:78.1'

(the input_select helper does exist, and includes these values as valid options)

Expected behavior

Desktop (please complete the following information):

pejotigrek commented 1 year ago

don't know if my problem is totally related, but I guess there's something wrong going on with variables. I am trying to utilize variables just to save a lot of typing and use them in different templates, buttons etc., but variables are always "undefined".

I tried with the simplest code possible:

- type: custom:button-card
  entity: update.0_sts_zamel
  variables:
    test_var: "this-is-test"
  name: "[[[ return variables.test_var; ]]]"

and still - instead of default button with "this-is-test" as name, I can see the entity's name anyway..

button card: 3.5.0, homeassistant: 2023.04.4, browser: latest chrome

cyr123 commented 1 year ago

OK, update: I'm a massive idiot and/or was too tired when I wrote this code. It works perfectly as long as I actually type the correct values (hint: "." and ":" are not the same thing).

pejotigrek commented 1 year ago

OK, update: I'm a massive idiot and/or was too tired when I wrote this code. It works perfectly as long as I actually type the correct values (hint: "." and ":" are not the same thing).

can you provide a fragment of corrected code for reference? as far as I am understanding your case - I guess that it will not resolve my problem :( so I want to investigate more to be sure if I need to step up my template game or to post a new issue with my scenario exactly ;)

cyr123 commented 1 year ago

My code as posted is actually working correctly, my only issue was that the values I had typed in for the variable ("1:78.1" etc) didn't match the values defined for the input_select I was targeting. It should have been "1.78:1" etc.

Mariusthvdb commented 1 year ago
```yaml
- type: custom:button-card
  entity: update.0_sts_zamel
  variables:
    test_var: "this-is-test"
  name: "[[[ return variables.test_var; ]]]"

this exact card config is working fine:

    - type: custom:button-card
      entity: sensor.alarm_button_hall_battery
      variables:
        test_var: "this-is-test"
      name: "[[[ return variables.test_var; ]]]"
Scherm­afbeelding 2023-04-20 om 12 58 40

if you still experience configuration challenges (these are not 'issues', as in: bugs) then please create a post in the community, and lets keep this repo clean as possible

pejotigrek commented 1 year ago

this exact card config is working fine

thanks for testing!

if you still experience configuration challenges (these are not 'issues', as in: bugs) then please create a post in the community, and lets keep this repo clean as possible

sure! after few hours of searching for typos and other errors in my templates I almost gave up and then suddenly found this issue, looked similar to my problem, but now I know it is not button-card related. thans again :)