#why wont this custom:button-card variable work...

1 messages · Page 1 of 1 (latest)

neat bough
#

Must be going blind here, but I cant see why this variable cant be used, and I need to rewrite the exact same condition in the other configuration options:

type: custom:button-card
template: subview_navigate
variables:
  motion: >
    [[[ return states['binary_sensor.motion_inside'].state === 'on' ||
               states['binary_sensor.motion_outside'].state === 'on'; ]]]
icon: >
  [[[ return states['binary_sensor.motion_inside'].state === 'on' ||
             states['binary_sensor.motion_outside'].state === 'on' ?
      'mdi:run-fast' : 'mdi:motion-sensor-off'; ]]]
styles:
  icon:
    - color: >
        [[[ return states['binary_sensor.motion_inside'].state === 'on' ||
                   states['binary_sensor.motion_outside'].state === 'on' ?
            'var(--alert-color)' : 'var(--success-color)'; ]]]

while I would have wanted to use the

icon: >
  [[[ return variables.motion ?
      'mdi:run-fast' : 'mdi:motion-sensor-off'; ]]]
styles:
  icon:
    - color: >
        [[[ return variables.motion ?
            'var(--alert-color)' : 'var(--success-color)'; ]]]

syntax.

I have another example using:

  variables:
    united: >
      [[[ return states['zone.home'].state === '6'; ]]]

and

    card:
      - animation: >
          [[[ return variables.united ? 'card_bounce 4s infinite' : 'none'; ]]]

working perfectly.

please have a look?

tidal pier
#

Maybe it has something to do with your template. I took your code, removed the template line, set the entities for something that matches my set up (though I used the same entity since they're both on) and it seems to work fine. (I added the name: to see what motion was returning.) ```yaml
type: custom:button-card

variables:
motion: >
[[[ return states['input_boolean.tester'].state === 'on' ||
states['input_boolean.tester'].state === 'on'; ]]]

icon: >
[[[ return variables.motion ?
'mdi:run-fast' : 'mdi:motion-sensor-off'; ]]]

styles:
icon:
- color: >
[[[ return variables.motion ?
'var(--alert-color)' : 'var(--success-color)'; ]]]

name: '[[[ return variables.motion ]]]'

neat bough
#

thx, I also suspected that, but took it out and it didnt change... I had the same idea for the name, but added it to the tooltip..

tidal pier
#

But, the template being the problem doesn't make sense, because anything entered at the card level would override the template.

neat bough
#

ive also figured it might the naming of the variable, zo gave it a name with a z so that the order is at least 100% certain the last variable, and even tht didnt make a difference. Its my first, in all of the buttons is use... really silly (btw, no errors in Inspector)

tidal pier
#

I do remember that variables are handled alphabetically. So var_a cannot reference var_b because it has not been created yet.

#

My result is what you're looking for, right? (Works for me but not for you?)

neat bough
#

yes, working well. also with template and 2 different booleans

#
type: custom:button-card
template: subview_navigate
# variables:
#   beweging: >
#     [[[ return states['binary_sensor.motion_inside'].state === 'on' ||
#                states['binary_sensor.motion_outside'].state === 'on'; ]]]
# icon: >
#   [[[ return states['binary_sensor.motion_inside'].state === 'on' ||
#              states['binary_sensor.motion_outside'].state === 'on' ?
#       'mdi:run-fast' : 'mdi:motion-sensor-off'; ]]]
# styles:
#   icon:
#     - color: >
#         [[[ return states['binary_sensor.motion_inside'].state === 'on' ||
#                    states['binary_sensor.motion_outside'].state === 'on' ?
#             'var(--alert-color)' : 'var(--success-color)'; ]]]
#
# variables:
#   tooltip: Beweging
#   path: /ui-sub-views/beweging

variables:
  motion: >
    [[[ return states['input_boolean.test'].state === 'on' ||
               states['input_boolean.test_2'].state === 'on'; ]]]

icon: >
  [[[ return variables.motion ?
      'mdi:run-fast' : 'mdi:motion-sensor-off'; ]]]

styles:
  icon:
    - color: >
        [[[ return variables.motion ?
            'var(--alert-color)' : 'var(--success-color)'; ]]]

name: '[[[ return variables.motion ]]]'
#

AARRGGHHH

#

its working after all...... stupid me..... I had also this in the button:```
variables:
tooltip: Beweging
path: /ui-sub-views/beweging

#

effectively overriding the templated variables .........

#

my apologies

tidal pier
#

Does it override the entire section? Not add more variables to the variables section?

neat bough
#

yes, it overrides. the config only considers the last option in the yaml, as it does for all options

#
type: custom:button-card
template: subview_navigate
variables:
  beweging: >
    [[[ return states['binary_sensor.motion_inside'].state === 'on' ||
               states['binary_sensor.motion_outside'].state === 'on'; ]]]

variables:
  tooltip: Beweging
  path: /ui-sub-views/beweging

only uses the bottom variables option

#

detail: it does this only when in 1 complete single config. If the configuration template contains a variables: it merges that with the config of the button

tidal pier
#

You know what, I remember that now. When I design my templates, I define all of the variables (to act like "defaults"), but in the card, the variables set here are what actually gets considered.

neat bough
#

yes.

#

anyways, can close this as resolved. Thanks for your help! appreciated.