How can I get the translation of a sensor state?

I set up an automation to send me a notification whenever my vacuum cleaner changes its “error” state to one of the many pre-configured errors. I got the German translation running in Home Assistant, so I would have expected that message to be also in German. Instead my automation sends me the original English state which results in a language mix in my notification.This is what I have:

{{ states.sensor.vacuum_error.state }}}

Result: “none”

I also tried:

{{ state_attr('sensor.vacuum_error.state', 'friendly_name') }

Result: “None” (with a capital “N”).

German translation in Home Assistant is “Keine”, the entity shows these translated entries everywhere except in the YAML file. So is there a way to get this done without messing around too much and just taking the translations that are already in the system?

Try this

{{ states('sensor.vaccum_error') }}

you spelled vacuum wrong in your first example, and your second you spelled the entity_id wrong.

{{ states.sensor.vacuum_error.state }}
{{ states('sensor.vacuum_error') }}
{{ state_attr('sensor.vacuum_error', 'friendly_name') }}

Still “none” in English.

Sorry, my original template had the name of my entity in German, I wanted to use a more generic one for my example, “vaccum” was a typo. :sweat_smile:

Result: )>

Result: none

Result: Vacuum error *

  • name of the entity, not the state

This behaviour is expected. For example, if I want a message with someone’s home state by using the following code


Juhuu, Pedolsky ist wieder {{ states('person.pedolsky') }}!

the result is not „Juhuu, Pedolsky ist wieder zuhause!“ but „Juhuu, Pedolsky ist wieder home!“
You have to translate it for notifications in German.

1 Like

And how do you do that? With if […] then for each state? :confused: I was hoping there would be an additional parameter to have the translation in the current language.

Just a few examples, this list is pretty long:

None if that exists in the backend. The frontend handles translations. That means you have to translate things for your notifications by hand.

1 Like

I would use a dictionary in my notification. Here an example for translated weather conditions:


trigger:
  - platform: state
    entity_id: sensor.zuhause_condition ## openweathermap integration

action:
  - service: notify.gmail
    data:
      title: Das Wetter
      message: |
        {% set wetter = {'sunny': 'Sonnig',
                  'windy': 'Windig',
                  'windy-variant': 'Stürmisch',
                  'clear-night': 'Klare Nacht',
                  'partlycloudy': 'Tw. bewölkt',
                  'cloudy': 'Bewölkt',
                  'overcast': 'Bedeckt',
                  'fog': 'Neblig',
                  'hail': 'Hagelig',
                  'lightning': 'Gewittrig',
                  'lightning-rainy': '?',
                  'pouring': 'Starkregen',
                  'rainy': 'Regnerisch',
                  'snowy-rainy': 'Schneeregen',
                  'snowy': 'Schnee'} %}
         {% set bedingungen = trigger.to_state.state %}
         Heute wird es {{ wetter[bedingungen] if bedingungen in wetter.keys() else '?' }} 

2 Likes

Okay, thank you, at least I know that now. Time for a little {% if %} … {% elif %} {% else %} I guess. :grinning_face_with_smiling_eyes: Starting to learn the syntax, maybe a good time to practice.

Thank you @pedolsky, might try that! :+1:

This is my solution now, based on your translation @pedolsky

service: notify.notify
data:
  title: Gerätefehler
  message: >-
   Der Staubsauger hat ein Problem erkannt:  
   {% set error_message = {'lidar_blocked': 'Lidar blockiert',
   'bumper_stuck': 'Stoßfänger klemmt',
   'wheels_suspended': 'Räder aufgehängt',
   'main_brush_jammed': 'Hauptbürste eingeklemmt',
    'side_brush_jammed': 'Seitenbürste eingeklemmt',
   'wheels_jammed': 'Räder klemmen',
   'robot_trapped': 'Roboter gefangen',
   'no_dustbin': 'Kein Mülleimer',
   'low_battery': 'Niedriger Batteriestatus',
   'charging_error': 'Ladefehler',
   'battery_error': 'Batteriefehler',
   'wall_sensor_dirty': 'Wandsensor verschmutzt',
   'robot_tilted': 'Roboter umgekippt',
    'side_brush_error': 'Fehler Seitenbürste',
   'fan_error': 'Lüfterfehler',
    'vertical_bumper_pressed': 'Vertikaler Stoßfänger gedrückt',
    'dock_locator_error': 'Roboter findet die Ladestation nicht',
    'return_to_dock_fail': 'Rückkehr zur Ladestation fehlgeschlagen',
    'nogo_zone_detected': 'Sperrzone erkannt',
   'vibrarise_jammed': 'VibraRise blockiert',
   'robot_on_carpet': 'Roboter auf Teppich',
   'filter_blocked': 'Filter verstopft',
   'invisible_wall_detected': 'Unsichtbare Wand erkannt',
   'cannot_cross_carpet': 'Teppich kann nicht überquert werden',
   'internal_error': 'Interner Fehler', '': '', '': ''} %}
   {% set error_state = states('sensor.vacuum_error') %} 
   {% if error_state in error_message %} {{ error_message[error_state] }}
   {% else %} {{ error_state }}
   {% endif %}
alias: Benachrichtigung

I excluded several states from the trigger like “none”, “unknown” and “unavailable” since I got another automation for devices being offline. I also made sure that a status that I didn’t include would be displayed but in English. Just in case there is an update where the state names change or if I made a mistake.

I got all the states’ names from simply choosing them from the list and then checking the YAML file, one by one. I didn’t find an easier way. If there is one, let me know.

1 Like

You could use state_translated() which gives you the state in the language to which HA is configured.

1 Like

Which wasn’t available 9 months ago when the topic was created :slight_smile:

2 Likes