custom-cards / button-card

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

help to lock if another switch and has 1 #824

Closed Eucliwood090 closed 5 months ago

Eucliwood090 commented 5 months ago

I would like to lock a button if another button is already activated but I get errors and I don't understand why

` type: custom:button-card entity: switch.lum_tvlum_tv icon: mdi:floor-lamp lock: enabled: '[[[ if (states['switch.cuisine1'].state == "on") ; ]]]' duration: 3 unlock: hold use_light_color: true collapsible_controls: true

`

joeidea commented 5 months ago

I don't see any indentation; but that my be a result of your copy/paste or typing. I would also try returning the state without the "if".

type: custom:button-card entity: switch.lum_tvlum_tv icon: mdi:floor-lamp lock: enabled: '[[[ return states[switch.cusisine.state ]]]' duration: 3 unlock: hold

joeidea commented 5 months ago

Yikes, Missing "]" should be --> enabled: '[[[ return states[switch.cusisine].state ]]]' You probably had indentation. I dod; but leading blanks are stripped out here.

Eucliwood090 commented 5 months ago

image

Unfortunately it doesn't work.

type: custom:button-card
variables:
  cuisine: switch.cuisine1
entity: switch.lum_tvlum_tv
icon: mdi:floor-lamp
lock:
  enabled: '[[[ return states[switch.cusisine1].state ]]]'
  duration: 3
  unlock: hold
use_light_color: true
collapsible_controls: true
joeidea commented 5 months ago

Alas, much of this is trial and error. I am able to reference an entity "outside" of my custom button card. Single quoting the entity made this work. Also, you cannot assume (I did) that "on" is true while "off" is false.; use if/else to return true/false. The following worked for me. Give it a try:

lock:
  enabled: |
    [[[ 
      if (states['switch.power_bar_office_socket_1'].state == "on") return true
      else return false
    ]]]
Eucliwood090 commented 5 months ago

Oh it's perfect It works well