Reusable templates for values and icons (e.g. for translation)

Hi,

I have a window sensor (Homematic) that returns values like open, tilted and closed. I’m using the following configuration to work with this sensor:

      fk1:
        friendly_name: "Linker Fensterflügel"
        value_template: >-
          {% set state = states('sensor.srh1_state') %}
          {% if state == 'open' %}
            Geöffnet
          {% elif state == 'tilted' %}
            Gekippt
          {% elif state == 'closed' %}
            Geschlossen
          {% else %}
            ?
          {% endif %}
        icon_template: >-
          {% set state = states('sensor.srh1_state') %}
          {% if state == 'open' %}
            mdi:window-open
          {% elif state == 'tilted' %}
            mdi:window-open
          {% elif state == 'closed' %}
            mdi:window-closed
          {% else %}
            mdi:help
          {% endif %}

Basically, what I’m doing here is to translate the internal state values into my native language (German) along with changing the icon. All of this makes it “nicer” to use for persons accessing the frontend.

In general this works just fine. However, I’m now looking into ways to make this template re-usable, as I have many identical sensors and don’t want to repeat all of those lines in many places. Also, when I decide to change the icon and/or translation, I have to touch it in many places.

Is there a way to define this template more generically and invoke it by passing in the variable that should be evaluated? What is the recommended way to implement something like this for additional sensors (e.g. fk2). What do I need to look for in the documentation? Couldn’t find something like that in the chapter about templating.

First, you wont’ be able to make this reusable. You can use the template editor to have it write the yaml for you, then copy/paste it into your yaml files.

Second, you wouldn’t need these templates for translation if you used binary_sensors device class or cover device classes utilizing the template integrations for either of those domains.

Thank you @petro, for your reply. I have some follow-up questions, though:

I’m not sure if I understand you correctly. So there is no way to make this re-usable (i.e. “parameterizable”)? That’s unfortunate, but okay, let’s assume that this is correct.

What is this good for? I consider myself a power user and I’m absolutely okay to edit YAML files. I would like to touch the web interface and/or any editors as little as possible. My problem is the redundancy that I have to create here and that I don’t like that kind of redundancy - regardless of whether I have to do it in a text file or in an editor.

Okay, I will have to look more closely into those device classes. From my (current) understanding, a binary sensor is not sufficient (because it has three (or four) states: opened, closed, tilted, unknown.

The cover device class on the other hand, seems to be able to control covers, at least that is what the documentation suggests:

Home Assistant can give you an interface to control covers such as rollershutters, blinds, and garage doors.

In my case I don’t want to control anything, I just try to get the status. The documentation also mentions:

Defines an action to open the cover. If open_cover is specified, close_cover must also be specified. At least one of open_cover and set_cover_position must be specified.

That doesn’t sound helpful in my case. But I will have a closer look/read. If you have more specific hints/references, I wouldn’t mind to get some additional help of course :-).