How to use " |replace" to manipulate sensor output?

Hey,

I have a sensor (door open / door closed) of a dishwasher. This sensor provides “off” when the door is closed and “on” when the door is open. I want as output the German terms instead of the English.

Example:

  • on–>“offen”
  • off–>“geschlossen”

For this I tried the following (used the command “replace”):

{{ states ('binary_sensor.geschirrspuler_door') | replace("on", "offen") | replace("off", "geschlossen")}}

But this does not work. Namely, it generates the following:

  • If door is open: “offen”
  • If door is close: “off”

Is there any documentation to “replace” or a better way to adapt the output to another language?

Besides that, one more question. Where is the best place to read up on the syntax of the commands as a beginner?

Thank you very much for your input.

The problem is that “offen” includes “off”, which is then further processed in the next replace, but I can’t explain your results. You can test this in DevTools->Templates:

{{ "on" | replace("on", "offen") | replace("off", "geschlossen")}} -> geschlossenen
{{ "off" | replace("on", "offen") | replace("off", "geschlossen")}} -> geschlossen

A better way to do it is to define a dictionary and do a lookup:

{% set map = {'on': 'offen', 'off': 'geschlossen'} %}
{{ map[states ('binary_sensor.geschirrspuler_door')] }}

Regarding documentation:
https://jinja.palletsprojects.com/en/3.1.x/templates/

{{ iif(is_state('binary_sensor.geschirrspuler_door', 'on'), 'offen', 'geschlossen') }}

Thank you - works. A nice solution that is easy to follow.

1 Like

Thank you very much!
This brings me further. :grinning: