Get translated entity state (in component template)

I know it’s possible to get the state of a given entity in a template like so:

sensor:
  - platform: template
    sensors:
      moon_extra_info:
        ...
        value_template: "{{ state('sensor.moon') }}" 

But this value will the ‘raw’ state string (eg new_moon, waxing_crescent etc). Is it possible to load the translated value (eg from .translations/sensor.[lang].json) for use within these template fields?

It’s obviously possible to re-code a conditional statement into the value_template (eg if new_moon then print X) but that’s duplicating existing code plus it’s infeasible to support all the various translations :slight_smile:

So is the translated state string available in some manner? Thanks!

5 Likes

I’d also love to know how to do this. I’d like to use these translated values in TTS actions in my local language (Dutch).

2 Likes

There is still no answer for this? :slightly_frowning_face: I have searched and still couldn’t find a solution that didn’t involve programming the translation like @davidjb said… Feels like unnecessary work since HA already knows the translations…

There is a PR for translated state but it is still in review. Add state_translated function to jinja templates by PiotrMachowski · Pull Request #65743 · home-assistant/core · GitHub
I think it would be very useful - @frenck

Is there any news on this? As indeed it would be very usefull.
Now we need to add code just for the translation.

You might want to know that there’s now an available integration which does this: GitHub - PiotrMachowski/Home-Assistant-custom-components-Custom-Templates: This integration adds possibility to use new functions in Home Assistant Jinja2 templating engine.

2 Likes

Looks like this is now possible using…
{{ state_translated('sensor.moon') }}
…instead of…
{{ state('sensor.moon') }}

Unfortunately this only translates text states, but it does not change the locale of numeric states, i.e. it does not convert 10.23 to 10,23 in German.

It’s been over 4 years. Is there an update to this?
I’d like to get the relative time of an entities last state change from a template like I see it in the ui