Name: based on status of state

Trying to get the name to change on a button card based on the status of the switch it is controlling.

for example when the switch in is on, it would display heat is enabled, when switched off it would display A/C is enabled.

I already built a sensor that shows those messages in its state and works like it should just need to know how to use that sensors state in the name part of the button thanks

It’s here,

well that did not work

entity: switch.tp_link_smart_switch_e720
hold_action:
  action: more-info
icon: 'mdi:power'
name: >-
  [[[ if (switch.tp_link_smart_switch_e720.state on) return "Heat is Enabled";
  else return "A/C is Enabled"]]]
show_icon: true
show_name: true
tap_action:
  action: toggle
type: custom:button-card

You have no condition between the object and status in the ‘if’ statement. Make sure you also copy the way its written out, I’ve had a bit of an issue getting stuff to work using that, and it will not generate any errors in the log when you do get it wrong.

Paste the whole thing into the developer template page and see what it is evaluated to.

this is the template that was on the link you posted

name: '[[[ if (entity.state > 42) return "Above 42"; else return "Below 42" ]]]'
name: >
  [[[
    if (entity.state > 42)
      return "Above 42;
    else
      return "Below 42;
  ]]]

tried both ways and it saves to what I posted with the >- in the line

this is from the dev template

entity: switch.tp_link_smart_switch_e720
hold_action:
  action: more-info
icon: 'mdi:power'
name: [[[ if (switch.tp_link_smart_switch_e720.state on) return "Heat is Enabled"; else return "A/C is Enabled"]]]
show_icon: true
show_name: true
tap_action:
  action: toggle
type: entity-button

I tried inserting a = sign between .state and on still did not work same result with is

this is what I got to work in my sensor and it works fine

{% if is_state('switch.tp_link_smart_switch_e720', 'on') %}
            Heat is Enabled
          {% else %}
            A/C is Enabled
          {% endif %}!"

but won’t work in the button entity name:

Yeah, if its not being processed in the development template page, its not working properly. I’ve found the javascript side of this to be temperamental. This not work?

entity: switch.tp_link_smart_switch_e720
hold_action:
  action: more-info
icon: 'mdi:power'
name: '[[[ if (switch.tp_link_smart_switch_e720.state == 'on') return "Heat is Enabled"; else return "A/C is Enabled" ]]]'
show_icon: true
show_name: true
tap_action:
  action: toggle
type: entity-button

Yeah template sensor above is Jinja, so it won’t work in the name template as that is Javascript.

nope tried in both dev template and on the button entitiy even with the == still does not work

replace the entity with one of yours and try it out in your dev template let me know the results

I’ll get a copy of my config tonight, as I have done this previously and it worked fine. There must be something that’s not quite right.

Looks like i havent used it for name, it’s label,

                  - type: "custom:button-card"
                    template: timer_status
                    entity: timer.alarm1_repeat
                    show_state: false
                    show_label: true
                    label: >
                      [[[
                        if (entity.state == 'idle') return "idle";
                        return states['sensor.timer_remaining_hms_alarm_1'].state;
                      ]]]

One with if, elif and then else,

                    label: >
                        [[[
                          if (entity.state == 0) return "Today";
                          else if (entity.state == 1) return "Tomorrow";
                          else return "in "+entity.state+" days";
                        ]]]