Renaming sensor output doesn't work

Hi,
I got some outputs of a sensor which I want to rename.
I want the output “1” to be “Manual” and “2” to be “Automatic”.
I tried it like this but it doesn’t work.

    sonnen_modus:
     friendly_name: 'Sonnen modus'
     value_template: >-
                     {% set names = {
                                     1: "Manual",
                                     2: "Automatic",
                      } %}
                     
                     {{ names[states.sensor.json_sonnen.attributes["OperatingMode"]] }}    

If it is possible, I would like the other possible outputs to just stay the same (so if the output is “0” I want it to display “0” etc.)

Can anyone help?

Try this version and let us know what it reports.

sonnen_modus:
  friendly_name: 'Sonnen modus'
  value_template: >-
    {{ {1: 'Manual', 2: 'Automatic'}.get(state_attr('sensor.json_sonnen', 'OperatingMode')|int(0), 0) }}

It works! Thanks!

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information refer to Guideline 21 in the FAQ.