custom-cards / button-card

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

Question, Using IF statement in multi_calls #620

Open JayChun opened 2 years ago

JayChun commented 2 years ago

It's just a question about java script tempating. I am making 'multi_calls' that call different actions depending on the button entity state.

If button entity state is ON.

if button entity state is OFF.

There are probably many ways to make it, but I'd like to handle by Button-card.

This is my code, but it is not working. Can someone please explain what is wrong?

tap_action:
  action: nothing
  multi_calls: |
    [[[
      if ( states['switch.digital_frame_hdmi'].state == "on" ) {
          hass.callService(
            "switch",
            "turn_off",
            { entity_id: "switch.digital_frame_hdmi" }
          );
          hass.callService(
            "automation",
            "turn_off",
            { entity_id: "automation.switch_digital_album_control" }
          ) }
      if ( states['switch.digital_frame_hdmi'].state == "off" ) {
          hass.callService(
            "switch",
            "turn_on",
            { entity_id: "switch.digital_frame_hdmi" }
          );
          hass.callService(
            "automation",
            "turn_on",
            { entity_id: "automation.switch_digital_album_control" }
          ) }
    ]]]
JayChun commented 2 years ago

I inserted { } in the code, and updated it in the body. But the switch and automation are 'turn off' and 'turn on' and repeats over and over again.

JayChun commented 2 years ago

I made another single button-card and test the code. It's working well.

But the code is not a standalone button, it is button-card inside button-card. I think the problem is that doesn't work with button-card which made with 'customfield' inside parent button-card.

il77781 commented 2 months ago

@JayChun, have you tried adding do_not_eval: true for this nested button-card? Something like this:

type: custom:button-card
############
custom_fields:
  nested_card:
    do_not_eval: true
    card:
      type: custom:button-card
      #############
      tap_action:
        action: nothing
        multi_calls: |
          [[[
            if ( states['switch.digital_frame_hdmi'].state == "on" ) {
                hass.callService(
                  "switch",
                  "turn_off",
                  { entity_id: "switch.digital_frame_hdmi" }
                );
                hass.callService(
                  "automation",
                  "turn_off",
                  { entity_id: "automation.switch_digital_album_control" }
                ) }
            if ( states['switch.digital_frame_hdmi'].state == "off" ) {
                hass.callService(
                  "switch",
                  "turn_on",
                  { entity_id: "switch.digital_frame_hdmi" }
                );
                hass.callService(
                  "automation",
                  "turn_on",
                  { entity_id: "automation.switch_digital_album_control" }
                ) }
          ]]]

Of course, I don't know how relevant this is to you yet, but it seems to me that it should work...