Include translation output in templating sensors

Hi,

I do have quite some sensors that are templates, and that put out text, eg:

  somecustomsensor:
    value_template: >
      {% if _something__ %}
        Locked
      {% else %}
        Unlocked
      {% endif %}

So far, so good.
Is there a possibility to format the output in such a way that (depending on the “Language” setting of HA) a different text is presented?
Something like this:

  somecustomsensor:
    value_template: >
      {% if _something__ %}
        [en]Locked
        [de]Verriegelt
      {% else %}
        [en]Unlocked
        [de]Entriegelt
      {% endif %}

So I (with “English” as the language setting) will get Locked, whereas another user (with “German” as the language setting) will see Verriegelt.

Couldn’t find anything in this direction.

Thanks,

Thomas

I thought states were already translated in dashboards depending on your language setting?

States from “proper” sensors may be translated, but not when I write the english words into the template as a string. That string is written exactly as entered, no translation is happening on its own.

I think where some translation happens, its a binary state that HA is converting in text according to the device class? So a “true” in the device class window is then translated into “open” (or another word depending on language setting). At least that’s what I’m guessing.

Did you find out how to test on set language in a template sensor?
I have a similar problem, which I partly solved using a template select sensor. But how can I select the mapper to be used dependent on the selected language?

My case: I have a sensor from the ism7mqtt Addon ( GitHub - b3nn0/hassio-addon-ism7mqtt: HomeAssistant Addon for running ism7mqtt to fetch data from Wolf heaters ) which returns the operating state of the Wolf heatpump in German. I’d like to display this in the language, selected in the General Settings. Or if not available, in English as default.

So far, I made two template select-sensors, one for English and one for Dutch. Here the English one:

template:    
  - select:
      - name: "Operation mode (ENG)"
        unique_id: operation_mode_translated_to_english
        state: >
          {% set opt = states('sensor.wolf_cha_0x3_270050_betriebsart_heizgeraet') %}
          {% set mapper = {
            'ODU Test': 'ODU Test',
            'Test': 'Test',
            'Frostschutz HK': 'Frost protection heating', 
            'Frostschutz Warmwasser': 'Frost protection hot water',
            'Durchfluss gering': 'Limited flow',
            'Vorwärmung': 'Pre-heating',
            'Abtaubetrieb': 'Defrosting',
            'Antilegionellenfunktion': 'Antilegionalla',
            'Warmwasser': 'Hot water',
            'WW-Nachlauf': 'Hot water caster',
            'Heizbetrieb': 'Heating',
            'HZ-Nachlauf': 'Heating caster',
            'Aktive Kühlung': 'Active cooling',
            'Kaskade': 'Cascade',
            'GLT': 'GLT',
            'Standby': 'Standby',
            'Pump Down': 'Pump down', 
            'Nachlauf K': 'Nachlauf K',
            'Undefiniert': 'Undefined',
            'Pool': 'Pool'
          }%}
          {{ mapper.get(opt) }}

and the Dutch one:

      - name: "Operation mode (NL)"
        unique_id: operation_mode_translated_to_dutch
        state: >
          {% set opt = states('sensor.wolf_cha_0x3_270050_betriebsart_heizgeraet') %}
          {% set mapper = {
            'ODU Test': 'ODU Test',
            'Test': 'Test',
            'Frostschutz HK': 'Vorstbescherming verwarming', 
            'Frostschutz Warmwasser': 'Vorstbescherming warm water',
            'Durchfluss gering': 'Beperkte flow',
            'Vorwärmung': 'Pre-heating',
            'Abtaubetrieb': 'Ontdooien',
            'Antilegionellenfunktion': 'Antilegionalla',
            'Warmwasser': 'Warm water',
            'WW-Nachlauf': 'Warm water naloop',
            'Heizbetrieb': 'Verwarming',
            'HZ-Nachlauf': 'Verwarming naloop',
            'Aktive Kühlung': 'Actieve koeling',
            'Kaskade': 'Cascade',
            'GLT': 'GLT',
            'Standby': 'Standby',
            'Pump Down': 'Pomp down', 
            'Nachlauf K': 'Naloop K',
            'Undefiniert': 'Ongdefinieerd',
            'Pool': 'Zwembad'
          }%}
          {{ mapper.get(opt) }}

What is outstanding, is to select the mapper, something like:

{% if selectedLanguage = 'NL' %}
  mapper 'NL'
{% else %}
  mapper 'ENG'
{% endif %}

How do I test on set language in a template sensor?