Custom button card - alternate value

Hi all, hopefully a simple one. I use custom button cards in my dashboard. I have a door sensor that the state shows as ‘on’ or ‘off’. I am looking to have the button card show alternate text (i.e. Closed instead of On, or Open instead of Off)

here is my code

image

and here is the current button, note it says off instead of closed

image

Any ideas from all the smart people on this community?
thanks

state_display 	string 	optional 	On 	Override the way the state is displayed. Supports templates, see templates

Taken from the Custom:button_card Docs, so it seems you can template it
Maybe Something like

{% if is_state('sensor.my_sensor', 'off')%} Closed
{% endif %}

Thou if-statements is normally used with multiple choices , i.e with “else”

{% if is_state('sensor.my_sensor', 'off')%} Closed
{% else %} Open
{% endif %}

EDIT: Sorry apparently you have to use other syntax in Custom:Button_Card, Cris B’s solution seems more obvious to go for

Does your binary sensor have a device_class: door? If so, the “on/off” should translate to “closed/open” in your native language.

Animation

1 Like

thanks @koying. Ok this is very weird, its already a device class of door. So i looked at the device as per your instructions, in the native device info its showing ‘open’ and ‘closed’ but i have to use ‘on’ and ‘off’ in the yaml when creating the button card? its a ring door sensor coming through via MQTT. I am still a bit of a novice at this point so dont know why its coming through as on and off instead of open and closed. Tried changing my yaml to closed but it does not recognise that?

looks like others have the same issue (mine used to read open and closed) Door Binary Sensor not showing open/close

In yaml, and templates in general, switches and binary sensors states are always “on” and “off”.

Depending on their classes, that will translate to, e.g., “opened” and “closed” for display purposes, but not if you are using the entity state in a template in cards supporting this.

You could use a conditional card to show a button hardcoded to say “Closed” and another conditional card to say “Open”. Here’s an example of this I did today, it’s using lovelace_gen but the pattern is applicable to your use case.

{% set gen = _global.kg_list[0] %}
type: horizontal-stack
cards:
  - type: conditional
    conditions:
      - entity: sensor.{{gen}}_generator_state
        state: "Standby"
    card:
      type: button
      show_state: false
      show_name: true
      show_icon: false
      name: Generator Standby
      theme: green-background
      tap_action:
        action: navigate
        navigation_path: "/lovelace-kg/generator"
  - type: conditional
    conditions:
      - entity: sensor.{{gen}}_generator_state
        state: "Running"
    card:
      type: button
      show_state: false
      show_name: true
      show_icon: false
      name: Generator Running
      theme: blue-background
      tap_action:
        action: navigate
        navigation_path: "/lovelace-kg/generator"

thanks @koying for the info, its a shame as this used to work perfectly before an update some time ago.

thanks @PeteRage , think this may be going a bit beyond my current skills…might just have to live with the on/off for now. thank you anyway.

Use state_display as i mentioned above, it’s as simple as that.(should be)
There are lots of examples on this forum(i.e " Fun with Custom:Button Card", and the main Topic for the Custom:button_card)


state_display: |
   [[[ 
     if (entity.state == 'on') return 'Search and you will find'; return 'Nothing';
     ]]]

Note: For most such sensors and switches i don’t show “state”, as i can see that by the color and/or icon

2 Likes

Hi @boheme61, thanks for the guidance.

Thank you for this, after a bit of playing around i got it working using your solution. Thank you again!

Really appreciate the help
Darren.

move the 3 end “brackets” so they are inline with above “if” , as showed in my example

you was too quick with the reply :slight_smile: , got it working now, thanks again

1 Like