Good morning to all of you
I have a custom:button-card that changes based on the state of 2 sensors.
name: |
[[[
if (states['switch.sensore_magnetico_cancello_chiuso'].state === "on")
return "Chiuso";
else if (states['switch.sensore_magnetico_cancello_chiuso'].state === "off")if (states['switch.sensore_magnetico_cancello_aperto'].state === "on")
return "Aperto";
else if (states['switch.sensore_magnetico_cancello_chiuso'].state === "off")if (states['switch.sensore_magnetico_cancello_aperto'].state === "off")
return "In Movimento";
]]]
It’s working at the moment
I wanted to improve it by adding the various possibilities, but it doesn’t work.
Can you tell me where I’m going wrong?
name: |
[[[
if (states['switch.sensore_magnetico_cancello_chiuso'].state === "on")if (states['switch.sensore_magnetico_cancello_aperto'].state === "off")
return "Chiuso";
else if (states['switch.sensore_magnetico_cancello_chiuso'].state === "off")if (states['switch.sensore_magnetico_cancello_aperto'].state === "on")
return "Aperto";
else if (states['switch.sensore_magnetico_cancello_chiuso'].state === "off")if (states['switch.sensore_magnetico_cancello_aperto'].state === "off")
return "In Movimento";
else if (states['switch.sensore_magnetico_cancello_chiuso'].state === "on")if (states['switch.sensore_magnetico_cancello_aperto'].state === "on")
return "Da Sincronizzare";
]]]
If you need an “AND” logic:
if (… && …)
{
…
}
WallyR
(Wally)
3
And just to clarify Ildar_gabdullin’s reply.
No extra if either.
if (states['switch.sensore_magnetico_cancello_chiuso'].state === "on") && (states['switch.sensore_magnetico_cancello_aperto'].state === "off")
it gives me an error
ButtonCardJSTemplateError: SyntaxError: Unexpected token '&&' in 'if (states['switch.sensore_magnetico_cancello_chiuso'].state === "on") && (states['switch.sensore_...'
WallyR
(Wally)
5
I know too many programming languages and I think I got them mixed up here.
Try to replace && with and
unfortunately it doesn’t work
WallyR
(Wally)
7
Hmm, and is normally one of these:
Or is normally one of these:
Case can be important!
It gives me an error in the cart
WallyR
(Wally)
9
It should be &&, because it is javascript.
Maybe it is your brackets that are wrong.
if ((states['switch.sensore_magnetico_cancello_chiuso'].state === "on") && (states['switch.sensore_magnetico_cancello_aperto'].state === "off"))
type: custom:button-card
show_entity_picture: true
Show_Name: null
tap_action:
action: call-service
confirmation:
text: Sicuro di aprire il cancello?
service: script.apertura_cancello
double_tap_action:
action: call-service
service: script.chiusura_cancello
hold_action:
action: call-service
service: script.apertura_cancelletto
show_state: false
show_label: true
size: 60%
name: |
[[[
if (states['switch.sensore_magnetico_cancello_chiuso'].state === "on")if (states['switch.sensore_magnetico_cancello_aperto'].state === "off")
return "Chiuso";
else if (states['switch.sensore_magnetico_cancello_chiuso'].state === "on")if (states['switch.sensore_magnetico_cancello_aperto'].state === "on")
return "Da Sincronizzare";
else if (states['switch.sensore_magnetico_cancello_chiuso'].state === "off")if (states['switch.sensore_magnetico_cancello_aperto'].state === "off")
return "In Movimento";
else if (states['switch.sensore_magnetico_cancello_chiuso'].state === "off")if (states['switch.sensore_magnetico_cancello_aperto'].state === "on")
return "Aperto";
]]]
this way it reads the first and second condition, but it does not read the third and fourth condition
WallyR
(Wally)
11
I tested it and it works with && and the right brackets as said earlier.
type: custom:button-card
show_entity_picture: true
Show_Name: null
tap_action:
action: call-service
confirmation:
text: Sicuro di aprire il cancello?
service: script.apertura_cancello
show_state: false
show_label: true
size: 60%
name: |
[[[
if ((states['input_boolean.test1'].state === "on") && (states['input_boolean.test2'].state === "off"))
return "1";
else if ((states['input_boolean.test1'].state === "on") && (states['input_boolean.test2'].state === "on"))
return "2";
else if ((states['input_boolean.test1'].state === "off") && (states['input_boolean.test2'].state === "off"))
return "3";
else if ((states['input_boolean.test1'].state === "off") && (states['input_boolean.test2'].state === "on"))
return "4";
]]]
I didn’t realize I had to put the parentheses.
Now everything works.
Thanks