Access Localized State Strings

Hi all,

I am collecting some values from different sensors into a new template sensor to be able to beautify the presented information. The sensors are coming from the Miele integration and provide localized state strings, here “Baumwolle” in German:

entity

I now have the issue that I cannot access those localized state strings in the template sensor. I can access either the original state (“cottons”) or the Localized attribute in English (“Cottons”) as it is shown in the Developer Tools States page:

My result:
tile

I guess I am missing something here, but I could not find anything to change the behaviour. Is the picking of the right Localized attribute happening within the frontend only?

    - name: washing_machine_program_info
      state: >
        {% if is_state('sensor.washing_machine_status', 'off') %}
          Ausgeschaltet
        {% else %}
          {{ state_attr('sensor.washing_machine_programm', 'Localized') }}
        {% endif %}
      icon: mdi:washing-machine
      attributes:
        icon_color: >
          {% if is_state('sensor.washing_machine_status', 'running') %}
            blue
          {% elif is_state('washing_machine_status', 'Program ended') %}
            green
          {% elif is_state('sensor.washing_machine_status', 'off') %}
            disabled
          {% else %}
            red
          {% endif %}
        program_stage: >
          {{ iif(has_value('sensor.washing_machine_programmabschnitt'), state_attr('sensor.washing_machine_programmabschnitt', 'Localized'), ' ')}}
        program_end: >
          {{ iif(has_value('sensor.washing_machine_ende_um'), 'Ende um ' + states('sensor.washing_machine_ende_um'), ' ')}}
        program_remaining: >
          {% if is_state('sensor.washing_machine_status', 'off') %}
            {{ ' ' }}
          {% else %}
            {{ iif(has_value('sensor.washing_machine_verbleibende_zeit'), states('sensor.washing_machine_verbleibende_zeit') + ' min verbleibend', ' ')}}
          {% endif %}

Servus,
I found the solution and wanted to add it since I could not find it easily by googling.
There is a method called “state_translated”. So just replace the states(“sensor.name”) call with state_translated(“sensor.name”) and it will use the locale you have configured for HA.
Hope this helps.

6 Likes

I’m using the Miele integration too and arrived here because of the same problem you are facing. Nevertheless, I can’t really figure out how to replicate the card you took the screenshot from the template code you pasted? Could you explain how do you apply that code as a template to the card? The structure doesn’t seem to be the typical one to be put into configuration.yaml file.

This is the adapted code I’m doing from yours:

- name: washing_machine_program_info
      state: >
        {% if is_state('sensor.washing_machine_status', 'off') %}
          Off
        {% else %}
          {{ state_translated('sensor.washing_machine_programm') }}
        {% endif %}
      icon: mdi:washing-machine
      attributes:
        icon_color: >
          {% if is_state('sensor.washing_machine_status', 'running') %}
            blue
          {% elif is_state('washing_machine_status', 'Program ended') %}
            green
          {% elif is_state('sensor.washing_machine_status', 'off') %}
            disabled
          {% else %}
            red
          {% endif %}
        program_stage: >
          {{ iif(has_value('sensor.washing_machine_program'), state_translated('sensor.washing_machine_program'), ' ')}}
        program_end: >
          {{ iif(has_value('sensor.washing_machine_finish_at'), 'Finish at ' + states('sensor.washing_machine_finish_at'), ' ')}}
        program_remaining: >
          {% if is_state('sensor.washing_machine_status', 'off') %}
            {{ ' ' }}
          {% else %}
            {{ iif(has_value('sensor.washing_machine_remaining_time'), states('sensor.washing_machine_remaining_time') + ' min left', ' ')}}
          {% endif %}

It seems that state_translated is not working if the state is a timestamp.

Is there any timestamp translation working with Jinsa2, or does this need to be done manually by macro, Python or something else?

Sorry, this is a bit old already, I just wanted to share the full answer.

This is the template sensor:

template:
  - sensor:
      - name: washing_machine_program_info
        state: >
          {% if is_state('sensor.waschmaschine', 'off') %}
            Ausgeschaltet
          {% else %}
            {{ state_translated('sensor.waschmaschine_programm') }} {{ states('sensor.waschmaschine_solltemperatur') | float(0) | round(0) }} {{ state_attr('sensor.waschmaschine_solltemperatur', 'unit_of_measurement') }}
          {% endif %}
        icon: mdi:washing-machine
        attributes:
          icon_color: >
            {% if is_state('sensor.waschmaschine', 'in_use') %}
              blue
            {% elif is_state('sensor.waschmaschine', 'waiting_to_start') %}
              blue
            {% elif is_state('sensor.waschmaschine', 'program_ended') %}
              green
            {% elif is_state('sensor.waschmaschine', 'off') %}
              disabled
            {% else %}
              red
            {% endif %}
          program_name: >
            {% if is_state('sensor.waschmaschine', 'off') %}
              Ausgeschaltet
            {% else %}
              {{ state_translated('sensor.waschmaschine_programm') }}
            {% endif %}
          settings: >
            {% if is_state('sensor.waschmaschine', 'off') %}
              {{ ' ' }}
            {% else %}
              {{ states('sensor.waschmaschine_solltemperatur') | float(0) | round(0) }} °C • {{ states('sensor.waschmaschine_schleuderdrehzahl') }} U/min
            {% endif %}
          program_stage: >
            {% if is_state('sensor.waschmaschine', 'waiting_to_start') %}
              {{ 'Starte in: ' + states('sensor.waschmaschine_start_in') }}
            {% elif is_state('sensor.waschmaschine', 'in_use') %}
              {{ state_translated('sensor.waschmaschine_programmphase') }}
            {% else %}
              {{ ' ' }}
            {% endif %}
          program_remaining: >
            {% if is_state('sensor.waschmaschine', 'in_use') %}
              {{ states('sensor.waschmaschine_verbleibende_zeit') + ' min verbleibend' }}
            {% else %}
              {{ ' ' }}
            {% endif %}
          program_end: >
            {% if is_state('sensor.waschmaschine', 'in_use') %}
              {{ 'Ende um ' + states('sensor.waschmaschine_ende_um') }}
            {% else %}
              {{ ' ' }}
            {% endif %}

and this is the mushroom template card to create the visual above

type: custom:mushroom-template-card
entity: sensor.dishwasher_program_info
icon: "{{ state_attr(entity, 'icon') }}"
icon_color: "{{ state_attr(entity, 'icon_color') }}"
primary: "{{ states(entity) }}"
secondary: "{{ state_attr(entity, 'program_stage') }}"