Template from Developer tool into Dashboard

Hi, can someone help me. I am trying to code a template from Developer Tool

Blockquote

check if this is a flag

{% set A = ‘input_boolean.motion_flag_cuisine’ %}
{% if states(A) in (“on”, “off”) %}
this is a flag
{% else %}
not a flag as far as I know
{% endif %}

This work as desired, if I set a entity which either have a status on/off.
When I set a no existing entity, I got the message “not a flag …”

I am tying to insert this template into a decluttering_templates to determine the label.

Blockquote
decluttering_templates:
actif_flag1:
card:
type: custom:button-card
entity: ‘[[flag]]’

  label: >-
    [[[
    {% set A = 'input_boolean.motion_flag_cuisine' %};
    {% if states(A) in ("on", "off") %};
    return "this is a flag";
    {% else %};
    return "not a flag as far as I  know";
    {% endif %} 
    ]]]  

  show_name: true
  show_label: true

How should I change the code to get it working? Thank you

I don’t use Decuttering Card but Button Card.
Button Card is based on Javascript. So if you simply want to check whether an entity exist, use:


label: |
  [[[
    let A = 'input_boolean.motion_flag_cuisine';
    if (states[A]) return "this is a flag";
    return "not a flag as far as I  know";

If you only want to limit it to entities with on/off state:


label: |
  [[[
    let A = 'input_boolean.motion_flag_cuisine';
    if (['on', 'off'].includes(states[A]?.state)) return "this is a flag";
    return "not a flag as far as I  know";
  ]]]

References:
includes()
optional chaining operator ?.

Thank you, you made my day!

Hi Pedolsky,
Your proposed code work perfectly to set a label in my template (actif_flag_1)
However when using the same code for a template (in actif_flag2), it does not work. I get an error message that the template does not exist.

actif_flag1:
card:
type: custom:button-card
entity: ‘[[flag]]’
label: |
[[[
let A = ‘[[flag]]’;
if (states[A]) return “got it”;
return “no flag”;
]]]
show_name: false
show_label: true
actif_flag2:
card:
type: custom:decluttering-card
template: |
[[[
let A = ‘[[flag]]’;
if (states[A]) return “actif_flag”;
else return “no_flag”;
]]]
variables:
- v_flag: ‘[[flag]]’

When I replace the template code with either “actif_flag” or “no_flag” without the quote. The system find the template and take me there.
It there some other character hidden that could make the results differents

Please format the code. Currently it’s unreadable.

I don’t remember how to do.
Do I copy the code and the select it and apply the Blockcode,
or I press the Blockcode and then enter the code?

cog wheel —> preformatted text

I found out in google between … and …

Here is my code


actif_flag1:
card:
type: custom:button-card
entity: ‘[[flag]]’
label: |
[[[
let A = ‘[[flag]]’;
if (states[A]) return “got it”;
return “no flag”;
]]]
show_name: false
show_label: true
actif_flag2:
card:
type: custom:button-card
template: |
[[[
let A = ‘[[flag]]’;
if (states[A]) return “actif_flag”;
else return “no_flag”;
]]]
variables:
- v_flag: ‘[[flag]]’
show_name: false
show_label: true

No, it has to be backticks ```

lets try again

  actif_flag1:
    card:
      type: custom:button-card
      entity: '[[flag]]'
      label: |
        [[[
          let A = '[[flag]]';
          if (states[A]) return "got it";
          return "no flag";
        ]]]
      show_name: false
      show_label: true
  actif_flag2:
    card:
      type: custom:button-card
      template: |
        [[[
          let A = '[[flag]]';
          if (states[A]) return "actif_flag";
          else return "no_flag";
        ]]]      
      variables:
        - v_flag: '[[flag]]'
      show_name: false
      show_label: true

1 Like

Ok, now I see what you want to achieve.
I’m not aware of any possibility to template the field template. I would ask in the Decluttering Card thread.

In a different section of my code I was using the following

              - type: custom:decluttering-card
                template: |-
                  [[[ var A = 'input_boolean.msg_flag_[[v_subject]]'; 
                    if (A == "N/A") return  "no_flag"; 
                    else return "actif_flag"
                  ]]] 

It works, however I was using the value of the entity not its state (existence)
I am trying to simplify my code, by eliminating a variable, replacing it by existence or not of the entity.
Could you think of something that would make the difference?

That’s a Decluttering Card. It seems that template in Button Card only accepts strings.