Config-template-card help on 'if else if'

I’ve been battling this one for a while, and have it mostly worked out but need some help to close it out.

I have a simple-thermostat card displayed in a YAML lovelace dashboard.

Partial (but workable) config:

          - type: conditional
            conditions:
              - entity: input_boolean.ac_full_auto
                state: 'off'            
            card:
              type: custom:config-template-card
              entities:
                - climate.ac
              variables:
                ac_mode: states['climate.ac'].state
              card:
                type: custom:simple-thermostat
                entity: climate.ac
                header:
                  name: "${ac_mode == 'off' ? 'Manual control - off' : 'Manual control - on'}"

This works… but only gives me “if / else” display of the ac_mode results.

How do I add other ‘else’ options, either:

  1. longhand the if/else last line; or
  2. retain the shorthand style

…and how do I break that last line over multiple lines if needed?

possible ac_mode are cool, heat, fan_only, dry, off

I have been searching for a while but could not find a useful guide to the syntax of using “${ }” style.

OK, my question still stands on the correct way to format within the name statement, and how to use the ‘${ }’ format, but I have worked around this as follows:

          - type: conditional
            conditions:
              - entity: input_boolean.ac_full_auto
                state: 'off'            
            card:
              type: custom:config-template-card
              entities:
                - climate.ac
              variables:
                ac_mode: states['climate.ac'].state
                ac_mode_full: |
                  msg => {
                    if (msg == 'off') {
                        return 'Manual control - off';
                    }
                    else if (msg == 'cool') {
                      return 'Manual control - cooling';
                    }
                    else if (msg == 'heat') {
                      return 'Manual control - heating';
                    }
                    else if (msg == 'dry') {
                      return 'Manual control - drying';
                    }
                    else if (msg == 'fan_only') {
                      return 'Manual control - fan';
                    }
                      return 'Manual Control';
                    }
              card:
                type: custom:simple-thermostat
                entity: climate.ac
                header:
                  name: "${ ac_mode_full(ac_mode) }"

I’d still like to know if there is a better way to achieve this though…

you can make a dictionary, that’s about the only way to optimize this

Thanks Petro.

What is that, and is there a guide how to somewhere?

For my knowledge - what are the different template forms called? e.g. I have had to use ${ } for custom:config-template-card as noted above, but I had to use ‘{% if is_state…’ in the ‘style’ definition of the same card to get variable style control. At the moment I’m working it out by trial and error, but knowing what terms to search for would be a help.

At least I worked it out :slight_smile:

The code you are using is Javascript. There are thousands of tutorials on the internet for this. The code isn’t covered here because it’s a basic language used by pretty much every webpage.

Just google “js dictionary tutorial”

That’s jinja. That’s for templating fields outside the template card. You’re using a hard card. The card itself uses JS for it’s templating, while the card you’re modifying may or may not use jinja templates. Do not confuse the 2. They are different syntax and different code.

1 Like

OK, so that is javascript? I’ll see if I can find out how to extend that code include elsif.

Confirming these two ways both achieve the required outcome (not looked at dictionaries yet):

          - type: conditional
            conditions:
              - entity: input_boolean.ac_full_auto
                state: 'on'
              - entity: climate.ac
                state_not: 'off'
              - entity: input_boolean.ac_auto_monitor
                state: 'on'   
            card:
              type: custom:config-template-card
              entities:
                - climate.ac
              variables:
                ac_mode: states['climate.ac'].state
                ac_mode_full: |
                  msg => {
                    if (msg == 'cool') {
                      return 'Auto climate - cooling';
                    }
                    else if (msg == 'heat') {
                      return 'Auto climate - heating';
                    }
                    else if (msg == 'dry') {
                      return 'Auto climate - drying';
                    }
                    else if (msg == 'fan_only') {
                      return 'Auto climate - fan';
                    }
                      return 'Auto climate';
                    }
              card:
                type: custom:simple-thermostat
                entity: climate.ac
                header:
                  name: "${ ac_mode_full(ac_mode) }"

…or…

          - type: conditional
            conditions:
              - entity: input_boolean.ac_full_auto
                state: 'on'
              - entity: climate.ac
                state_not: 'off'
              - entity: input_boolean.ac_auto_monitor
                state: 'on'   
            card:
              type: custom:config-template-card
              entities:
                - climate.ac
              variables:
                ac_mode: states['climate.ac'].state
              card:
                type: custom:simple-thermostat
                entity: climate.ac
                header:
                  name: "${ac_mode == 'cool' ? 'Auto climate - cooling' 
                         : ac_mode == 'heat' ? 'Auto climate - heating'
                         : ac_mode == 'fan_only' ? 'Auto climate - fan'
                         : ac_mode == 'dry' ? 'Auto climate - drying'
                         : 'Auto climate';}"
2 Likes