Custom:button-card modify change state text based on state

Hello,
I have a problem to customize the status text depending on the status.

it looks like this with the following configuations:

show_state: true
show_name: false

and:

show_state: false
show_name: true

Screenshot 2023-09-24 141342

My code:

type: custom:button-card
entity: binary_sensor.hmip_swdo_0000d99c879_state
size: 50%
show_state: true
show_name: false
secondary_info: none
layout: icon_state
state_color: true
tap_action:
  action: none
hold_action:
  action: none
styles:
  card:
    - font-size: 11px
    - font-weight: bold
    - border-radius: 5px
name: |
  [[[
    if (entity.state === 'locked')
      return 'Auf';
    else
      return 'Zu';
  ]]]
state:
  - value: 'on'
    styles:
      icon:
        - color: orange
  - value: 'off'
    styles:
      icon:
        - color: '#298A08'
  - value: unavailable
    style:
      - opacity: 0.4
      - color: red

The goal is, to change the status text from the respective status to my own words, like: “Zu” and “Auf” (it fits better).
Any ideas? I would be grateful for any help.
Thanks in advance.
Have a nice day

Welcome to the forums!

You are using locked instead of on.

Oh, i´m sorry. This was from the attempts about the name.
But i want to change the string for ‘state’, not for ‘name’.
Instead of the standard output, i would like to output my own string as state. Maybe like ‘state: Auf’.
I can’t get it to work.

This for example does not work:

state:
  - value: |
        [[[
          if (entity.state === 'on')
            return 'Auf';
          else
            return 'Zu';
        ]]]

Thank you for your answer


type: custom:button-card
entity: binary_sensor.loggiatur
triggers_update: all
show_state: true
state_display: |
  [[[
    if (entity.state === 'on')
      return 'Auf';
    else
      return 'Zu';
  ]]]

Shorthand notation:


show_state: true
state_display: |
  [[[
    return (entity.state === 'on')
      ? 'Auf'
      : 'Zu';
  ]]]

1 Like

This is the solution.
Thanks a lot!

Thanks for sharing cause it had helped me to.

I have needed more or less exactly that showing an individual state instead of the sensors value.

I have an entity which can have 4 states called internationally
“off, now, minpv, pv” which describe the way how solar surplus power can be used to charge a car or whatever auxiliary device like a heatpump or immersive heater (iron bar in water cylinder).
now stands for the fastet way
pv stands for pure surplus electric from your roof alon
pvmin for the idea to add the smalles amount of grid power to be able to charge the car

Makes a huge difference if I can use 3000 W to charge a car from surplus cause that price is 7 cent while the price from the grid will vary over the day from rarely 20 cents to 55 cent in the morning and evening hours, which will be explained at the end.

SOLUTION

expanding your code to 4 states with 4 messages

state_display: |
  [[[
    if (entity.state === 'pv')
      return 'PV pur';
    else
      if (entity.state === 'pvmin')
        return 'PV + min. Netz';
      else
        if (entity.state === 'now')
          return 'VOLLGAS';
        else
          return 'KEIN LADEPROGRAMM';
  ]]]

At the end only the last line below the icon has changed but that is the important one for the users who wanna charge the car as cheap as possible

image

It says: “PV pur” aka “charging by pure surplus power only”

Difference: driving a car for 25% of the usual electricity prices within its range

Technical explanation

Therefore pvmin can be very usefull if your surplus is just 2500 Watt. The charger would not charger unless you allow to add 500 W from the grid. Assuming that would prolong your charges 1hour in the morning and 1 hour in the evening the difference would be astonishing if we take 6 kWh to charge
2 h x 2500 W = 5000 Wh = 5 kWh fed into the grid for 35 cents
but charging the car during cheapest night tarif at 25 cents x 6 kWh = 150 cents
115 cents or 400% more expensive this way to feed in and charge

In case of pv min mode it would be a 6 kWh charge again 2 hours
5000 Wh from the roof and 1000 Wh from the grid = 35 cents + depending on the tarif 30 - 50 cents or in total 65 cents to 85 cents
which is lot less than 150 cents.

“Financial orientation”
Even the most efficient Diesel are using 5 Liter Diesel per hkm which means burning the equivalent of 5 L x 9,8 kWh / L = 49 kWh for 100 km
Price is 160 Cent per Liter so 800 Cent or 8€ per 100 km

The EV will need 15 kWh per 100 km but the price will depend if you can surplus charge at home. Pure surplus charge would mean 15 x 7 = 105 cent per 100 km
fully charged during night from grid 15x 25 Cent = 375 cent per 100 km

That means you can drive for 1 / 8th of the price for Diesel up to 50%
And that is a lot to save - not to mention the lower emissions and close to no brakepad wear and tear which leads to particles in the city streets aka the dust and dirt cause the EV is slowing down by activating the engine as generator to recuperate energy from the moving mass into electricity which is stored in the battery for later use. The more you hit the brake the stronger the generator will act until you reach its limits and beyond that the genuine brakes will start their action at a very cold stage so you have a lot more reserves.

Thanks a lot - has helped me cause our elderly parents in their 80s are not so good in understanding acronyms like pv, pvmin, now and off
Now they can make the right choice to charge pure from the sun or if they are in a hurry “now” with full speed no matter what ( takes up to 5 hours to be fully charged from 0, but who drives his ICE to 0 km and miss the gas station ).