How to change the displayed name of switch state

Currently I am trying some smart home software to integrate my homematic ccu2 and modbus pool control into it.

HomeAssistant is running dockered image: homeassistant/raspberrypi4-homeassistant:latest on a raspberry pi 4 with 4gb (armv7l).

While fiddling with home assistant for the last two days I couldn’t find a way to change the displayed name of a state from a switch. Currently it’s “An” (On), “Aus” (Off) but this doesn’t fit my needs.

This is my code:

- platform: template
  switches:
     anwesenheit_btn:
       value_template: "{{ states.homematic.ccu2.attributes['Haus SV Anwesenheit'] }}"
       friendly_name: 'Anwesenheitsstatus'
       turn_on:
         service: homematic.set_variable_value
         data:
           entity_id: homematic.ccu2
           name: 'Haus SV Anwesenheit'
           value: true
       turn_off:
         service: homematic.set_variable_value
         data:
           entity_id: homematic.ccu2
           name: 'Haus SV Anwesenheit'
           value: false

If false it should be “Abwesend” and if true it should be “Anwesend”. Is it possible to do that?

I tried a different value template but it doesn’t work like that. I guess this only works for displaying sensors and such.

          {%- if state_attr("homematic.ccu2", "Haus SV Anwesenheit") == 0 -%}
            Abwesend
          {%- elif state_attr("homematic.ccu2", "Haus SV Anwesenheit") == 1 -%}
            Anwesend
          {%- else -%}
            Unbekannt
          {%- endif -%}

EDIT: I know it would be better if the presence would be automated but I couldn’t find a suitable solution for the past 4 years.

Your if statement should work as long as state_attr(…) provides indeed a 0 or 1. Did you check that in Entwicklerwerkzeuge -> Vorlage. Delete everything thats shown there (CTRL-A, ENTF) and put in the following: {{ state_attr(“homematic.ccu2”, “Haus SV Anwesenheit”) }}
Is the result what you expect?

If yes this should work:

 value_template: >
  {%- if state_attr("homematic.ccu2", "Haus SV Anwesenheit") == 0 -%}
    Abwesend
  {%- elif state_attr("homematic.ccu2", "Haus SV Anwesenheit") == 1 -%}
    Anwesend
  {%- else -%}
    Unbekannt
  {%- endif -%}

It returns True / False.

If I paste it into the Template editor it returns Abwesend/Anwesend correctly whether I use values or strings (0 / 1 || true / false)

But I don’t get the state anymore if I use the value template.

- platform: template
  switches:
     anwesenheit_btn:
       value_template: >
          {%- if state_attr("homematic.ccu2", "Haus SV Anwesenheit") == 0 -%}
            Abwesend
          {%- elif state_attr("homematic.ccu2", "Haus SV Anwesenheit") == 1 -%}
            Anwesend
          {%- else -%}
            Unbekannt
          {%- endif -%}
       friendly_name: 'Anwesenheitsstatus'
       turn_on:
         service: homematic.set_variable_value
         data:
           entity_id: homematic.ccu2
           name: 'Haus SV Anwesenheit'
           value: true
       turn_off:
         service: homematic.set_variable_value
         data:
           entity_id: homematic.ccu2
           name: 'Haus SV Anwesenheit'
           value: false

No that is not possible. There is no way to change the switch state or displayed state from on/off.

If you want this you would have to use something other than the core switch, e.g. a custom card like the button card.

Disappointing. This is a standard thing. Why would I need to figure out another addon/plugin for such a simple feature. But that’s not your fault! Thank you for the link.

Ahh, just realized that you are trying to use a switch for that. As already been said, that doesn’t work. Another option might be a template sensor if you only want to display the state of the switch (a sensor cannot turn on/off) .

you could also try https://github.com/iantrich/config-template-card
like this:

type: 'custom:config-template-card'
variables:
  - states['sensor.light']
entities:
  - '${vars[0].entity_id}'
card:
  type: light
  entity: '${vars[0].entity_id}'
  name: "${vars[0].state === 'on' ? 'Light On' : 'Light Off'}"

A particular solution depends on a particular place where a user needs it.

  1. For instance, if a user needs a switch entity to be shown in Entities card w/o a toggle and with custom states like “activated/deactivated” - a template-entity-row may be used.
  2. A mentioned button-card may define a custom state.
  3. A mentioned config-template-card can be used in places like Glance card to define a custom state.
  4. And no solution is available to show a custom state in more-info popups.