Custom Templates

I have created a custom integration that adds two new templates to HA template engine:

  • ct_state_translated - translates state of an entity to a desired language using translations made for dashboards
  • ct_translated - returns translation for a given key
  • ct_all_translations - returns all available translations (that can be used with ct_translated)
  • ct_eval - evaluates text as a template

This integration is a results of problems with adding state_translated functionality to HA core (PR - any help is welcome here).

Examples:

Input Output
State: {{ states("sun.sun") }}
Translated en: {{ ct_state_translated("sun.sun", "en") }}
Translated en: {{ "sun.sun" | ct_state_translated("en") }}
Translated nl: {{ ct_state_translated("sun.sun", "nl") }}
Translated nl: {{ "sun.sun" | ct_state_translated("nl") }}
State: below_horizon
Translated en: Below horizon
Translated en: Below horizon
Translated nl: Onder de horizon
Translated nl: Onder de horizon
Translated en: {{ ct_translated("component.sun.state._.below_horizon", "en") }}
Translated en: {{ "component.sun.state._.below_horizon" | ct_translated("en") }}
Translated nl: {{ ct_translated("component.sun.state._.below_horizon", "nl") }}
Translated nl: {{ "component.sun.state._.below_horizon" | ct_translated("nl") }}
Translated en: Below horizon
Translated en: Below horizon
Translated nl: Onder de horizon
Translated nl: Onder de horizon
{{ ct_all_translations("en") }}
{
  "component.sun.state._.above_horizon": "Above horizon",
  "component.sun.state._.below_horizon": "Below horizon"
}
{% set template_text = "{{ states('sun.sun') }}" %}
{{ ct_eval(template_text) }}
{{ template_text | ct_eval }}
below_horizon
below_horizon
4 Likes

This one is really great. I watched the original PR for core during the last months and it is a pity, that it was not possible there, because it is so essential for the UI.

1 Like

Unfortunately it seems that it’s not possible to merge original PR without major refactoring :frowning:

Came here just to say THANK YOU.
Started testing!

1 Like

Thanks for this, I was awaiting for something like that!

Related to this, it is possible to get the translated value for a string, in templates? Something like {{ "off"|translated }} which renders as désactivé , for example.

No, it’s not possible. Translated values are taken from HA UI translations and they are linked to entities. It should be possible provide translations for strings that are translation keys though

Ok, I have added this feature:

{{ ct_translated("component.binary_sensor.state._.off", "en") }}   -> Off
{{ "component.binary_sensor.state._.off" | ct_translated( "en") }} -> Off
2 Likes

Amazing! I think that’s good enough, I had some template sensors which has three states, open, closed and off, and I wanted a way to get translated values from HA for that.

Any guidance about where to find these translations keys in the HA’s source code? Thanks!

I have also added ct_all_translations template that returns all available translations :wink:

Came here to say thanks for the ct_eval function! exactly what I am looking for!

1 Like

How have you used it? :wink:

Have not tried this “ct_eval” function yet - but I hope I will be able to use it in these scenarios:

  1. Pass a template into “decluttering-card” - where it is used inside a template.
  2. Same for a blueprint.
1 Like

Im writing up my blueprint now - it works great for what I wanted - a declarative template linked to a switch state. E.g. when the template is true, turn switch/light on, when goes false, turn it off. Whilst this is quite easy to do with automations, it is annoying having to define the template twice etc etc.

You can see my post here: Template Switch Blueprint - Blueprints Exchange - Home Assistant Community (home-assistant.io)

1 Like

@megakid that’s a nice idea to pass a template without enclosing curly braces. I found HA to be quite eager to evaluate templates (even nested ones like {{ "{{ states('sun.sun')" }}), which prevented me from using it in some cases, so I might try to use your approach :+1:

A simple example:

  decl_test_var_template_into_template:
    default:
      - VALUE: >-
          {{states('sun.sun')}}
    card:
      type: markdown
      content: |-
        {% set STATE = ct_eval("[[VALUE]]") -%}
        Current state is {{STATE}}
        Zone state is {{states('zone.home')}}

Using:

type: vertical-stack
cards:
  - type: custom:decluttering-card
    template: decl_test_var_template_into_template
  - type: custom:decluttering-card
    template: decl_test_var_template_into_template
    variables:
      - VALUE: yyy
  - type: custom:decluttering-card
    template: decl_test_var_template_into_template
    variables:
      - VALUE: '{{states(''person.ildar'')}}'
  - type: custom:decluttering-card
    template: decl_test_var_template_into_template
    variables:
      - VALUE: >-
          {% if is_state('input_boolean.test_boolean','on') -%}
          {{states('person.ildar')}} {%- else -%}
          {{states('sensor.processor_use')}} {%- endif %}

image

But I faced some problem (so far it seems to be minor), please have a look:


Update: found a reason of this bug, it is in the "custom:decluttering-card".

Update: found a workaround.

1 Like

Piotr, could you drop here a link to the core PR you created to add these extensions?
Cannot find it myself(((

It’s in the first comment :slight_smile: (it contains only state_translated)

1 Like

Hi Piotr,
thanks for this amazing templates, it works great.
Only one note. After upgrade to HA Core 2023.4.4 is many of translation missing. Concretly for me was missing component.weather.state translations. When I downgrade to 2023.4.2 it works again fine.

That’s interesting, I’ll check it out

1 Like

These stopped working too:

{{ "component.sun.state._.below_horizon" | ct_translated("en") }}
{{ "component.sun.state._.below_horizon" | ct_translated("ru") }}

Gives this:


This gives only English variant:

Translated en: {{ ct_state_translated("sun.sun", "ru") }}

although I have this:

custom_templates:
  preload_translations:
    - en
    - ru