Help with custom button-card with conditions using Java

Good morning.

In my custom button card:

[...]

state_display: |
  [[[
    if (entity.state == 'off')
      return "Spenta";
    else
      if (entity.state == 'on')
        return "Accesa";
      else  
        return "Non disponibile";
  ]]]

This working fine.

Why doesn’t this work?

[...]
state_display: |
  [[[
    switch (entity.state)
      case 'off':
        return "Spenta";
      case 'on':
        return "Accesa";
      default:  
        return "Non disponibile";
  ]]]

Question n° 2

[...]

state_display: |
  [[[
    if (entity.state == 'off')
      return "Spenta";
    else
      if (entity.state == 'on')
        return "Accesa";
      else  
        return "Non disponibile";
  ]]]

I need to manage not entity.state but

[...]

state_display: |
  [[[
    if (states('input_boolean.lastricosolare_abilitazionepostazioneidroponica5') == 'off')
      return "Spenta";
    else
      if (states('input_boolean.lastricosolare_abilitazionepostazioneidroponica5') == 'on')
        return "Accesa";
      else  
        return "Non disponibile";
  ]]]

Is possible?

Question n° 3

In my dashboard I’ve created a master bottom that enables several other buttons and therefore inhibits their use if deactivated.
Currently the secondary buttons all have:

[...]
tap_action:
  action: toggle

If the master button is disabled, I need to transform it into:

[...]
tap_action:
  action: none

but from the documentation of the card it seems to me that I understand that the tap_action do not support javascript.
How can I fix?

Because you’re missing brackets.

    switch (entity.state) {
      case 'off':
        return "Spenta";
      case 'on':
        return "Accesa";
      default:  
        return "Non disponibile";
    }

FYI this is javascript not java.

q2

states['input_boolean.lastricosolare_abilitazionepostazioneidroponica5'].state

q3 is not possible because you can’t template the tap action section.

Thanks for your attention.
You are always very quick and professional in your replies.
Excuse me but I need to understand one thing:

From custom button card manual:

The template rendering uses a special format. All the fields where template is supported also support plain text. To activate the templating feature for such a field, you’ll need to enclose the javascript function inside 3 square brackets: [[[ javascript function here ]]]

From W3school site:

if (condition) {
// block of code to be executed if the condition is true
}

Have brackets.

But:

[...]

state_display: |
  [[[
    if (entity.state == 'off')
      return "Spenta";
    else
      if (entity.state == 'on')
        return "Accesa";
      else  
        return "Non disponibile";
  ]]]

Don’t have brackets and working.

From W3school site:

switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

Have brackets.

But:

[...]
state_display: |
  [[[
    switch (entity.state)
      case 'off':
        return "Spenta";
      case 'on':
        return "Accesa";
      default:  
        return "Non disponibile";
  ]]]

without brackets not working

That’s a shorthand if statement. JS allows this on if statements, that doesn’t mean you can do that on a switch case.

1 Like

Many tanks Petro!

Solution for q3 here

Hi, how do I insert a template in the custom_fields field?

I would need this template, to display a sentence based on the state of a sensor.

This is the template I would like to use:

{% if is_state('alarm_control_panel.alarmo', 'armed_home') %}
          The alarm is activated         
 {% else %}
          Alarm off
 {% endif %}

This is the part of the button-card where I would like to insert it:

custom_fields:
  title: |
    ALARM
  title_2: |
    --> HERE <--

Thanks

You can’t use jinja, you have to use JS. The template you posted is Jinja.

Thank you @petro for your reply, but unfortunately I don’t know how to use JS, I’ve always used Jinja. Can you help me?

Edit: I tried to make some attempts taking inspiration from the previous messages, but I always had errors


title_2: |
  [[[
    if ('alarm_control_panel.alarmo.state' == 'armed_home')
      return "The alarm is activated";
    else  
        return "Alarm off";
  ]]]