Custom Templates

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

Today I try update HA Core to version 2023.4.6 with the same result.
Translations still not working :frowning:

I know, I’ll check it out soon :frowning:

Fixed!

1 Like

Piotr, unfortunately does not work:

Log:

2023-05-03 13:40:08.328 WARNING (MainThread) [custom_components.custom_templates] No translation found for key: fcomponent.sun.state._.below_horizon
2023-05-03 13:40:17.447 WARNING (MainThread) [custom_components.custom_templates] No translation found for key: fcomponent.sun.state._.below_horizon
2023-05-03 13:40:48.984 WARNING (MainThread) [custom_components.custom_templates] No translation found for key: fcomponent.sun.state._.below_horizon
2023-05-03 13:40:48.996 WARNING (MainThread) [custom_components.custom_templates] No translation found for key: fcomponent.sun.state._.below_horizon

configuration.yaml:

custom_templates:
  preload_translations:
    - en
    - ru

Seems to be the latest version, installed by HACS:

{
  "domain": "custom_templates",
  "name": "Custom Templates",
  "codeowners": ["@PiotrMachowski"],
  "documentation": "https://github.com/PiotrMachowski/Home-Assistant-custom-components-Custom-Templates",
  "iot_class": "calculated",
  "issue_tracker": "https://github.com/PiotrMachowski/Home-Assistant-custom-components-Custom-Templates/issues",
  "requirements": [],
  "version": "v1.2.0"
}

It works, but the translation key in HA has been changed. Use component.sun.entity_component._.state.above_horizon instead

1 Like

Thanks a lot, that was a reason:

Its works perfect! Thank you for release, Piotr!