3_14
(Piotr Machowski)
February 1, 2023, 9:40am
1
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
arganto
February 1, 2023, 11:03am
2
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
3_14
(Piotr Machowski)
February 1, 2023, 11:12am
3
Unfortunately it seems that it’s not possible to merge original PR without major refactoring
Came here just to say THANK YOU.
Started testing!
1 Like
ov1d1u
(Ovidiu D.)
February 3, 2023, 4:09pm
5
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.
3_14
(Piotr Machowski)
February 3, 2023, 4:21pm
6
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
3_14
(Piotr Machowski)
February 3, 2023, 7:35pm
7
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
ov1d1u
(Ovidiu D.)
February 4, 2023, 8:05am
8
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!
3_14
(Piotr Machowski)
February 4, 2023, 3:06pm
9
I have also added ct_all_translations
template that returns all available translations
megakid
(James)
February 17, 2023, 9:17pm
10
Came here to say thanks for the ct_eval function! exactly what I am looking for!
1 Like
Have not tried this “ct_eval” function yet - but I hope I will be able to use it in these scenarios:
Pass a template into “decluttering-card” - where it is used inside a template.
Same for a blueprint.
1 Like
megakid
(James)
February 18, 2023, 11:10pm
13
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
3_14
(Piotr Machowski)
February 18, 2023, 11:30pm
14
@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
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 %}
But I faced some problem (so far it seems to be minor), please have a look:
opened 09:49AM - 20 Feb 23 UTC
closed 01:14PM - 20 Feb 23 UTC
Consider this example:
```
{% set TEMPLATE = "
{% if true -%}
{{states… ('zone.home')}}
{%- else -%}
{{states('sun.sun')}}
{%- endif %}
" %}
currently is {{ct_eval(TEMPLATE)}}
```
which works fine:
![image](https://user-images.githubusercontent.com/71872483/220069737-176fa946-671b-4b79-961c-e1194298ae41.png)
![image](https://user-images.githubusercontent.com/71872483/220069866-ac7b3e6d-707b-43cc-a4ca-b3a5ca83abe8.png)
Now let's try to use it with a `custom:declutering-card`:
```
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')}}
```
```
- 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 true -%}
{{states('zone.home')}}
{%- else -%}
{{states('sun.sun')}}
{%- endif %}
- type: custom:decluttering-card
template: decl_test_var_template_into_template
variables:
- VALUE: "{% if true -%}
{{states('zone.home')}}
{%- else -%}
{{states('sun.sun')}}
{%- endif %}"
- type: custom:decluttering-card
template: decl_test_var_template_into_template
variables:
- VALUE: >-
{% if true -%}
{{states('zone.home')}}
{%- else -%}
{{states('sun.sun')}}
{%- endif %}
```
where the last call gives an error:
![image](https://user-images.githubusercontent.com/71872483/220070894-77908da1-5730-48cf-b489-cf0ef5e00236.png)
i.e. this is accepted:
```
- VALUE: >-
{% if true -%}
{{states('zone.home')}}
{%- else -%}
{{states('sun.sun')}}
{%- endif %}
```
and this is NOT (although it works in Dev tools -> Template):
```
- VALUE: >-
{% if true -%}
{{states('zone.home')}}
{%- else -%}
{{states('sun.sun')}}
{%- endif %}
```
Could it be an issue for this integration or to the `decluttering-card` ?
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(((
3_14
(Piotr Machowski)
February 27, 2023, 5:50pm
17
It’s in the first comment (it contains only state_translated
)
home-assistant:dev
← PiotrMachowski:feature-jinja-translations
opened 03:26AM - 05 Feb 22 UTC
<!--
You are amazing! Thanks for contributing to our project!
Please, DO N… OT DELETE ANY TEXT from this template! (unless instructed).
-->
## Proposed change
<!--
Describe the big picture of your changes here to communicate to the
maintainers why we should accept this pull request. If it fixes a bug
or resolves a feature request, be sure to link to that issue in the
additional information section.
-->
This pull requests adds possibility to translate entity's state in jinja templates. This feature makes it much easier for users to send notifications including entities' states (e.g. weather status), use TTS, present data in markdown cards and probably much more.
code:
```
{{ state_translated("sun.sun", "en") }}
{{ state_translated("sun.sun", "nl") }}
```
result:
```
Below horizon
Onder de horizon
```
To achieve this functionality some small changes had to be done in translations loading process: now it is possible to preload state translations to cache for specified languages:
```yaml
homeassistant:
preload_translations:
- en
- nl
```
This functionality can be added to UI as well later.
## Type of change
<!--
What type of change does your PR introduce to Home Assistant?
NOTE: Please, check only 1! box!
If your PR requires multiple boxes to be checked, you'll most likely need to
split it into multiple PRs. This makes things easier and faster to code review.
-->
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [x] New feature (which adds functionality to an existing integration)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [x] Code quality improvements to existing code or addition of tests
## Additional information
<!--
Details are important, and help maintainers processing your PR.
Please be sure to fill out additional details, if applicable.
-->
- ~~This PR fixes or closes issue: fixes #~~
- ~~This PR is related to issue:~~
- Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/23573
## Checklist
<!--
Put an `x` in the boxes that apply. You can also fill these out after
creating the PR. If you're unsure about any of them, don't hesitate to ask.
We're here to help! This is simply a reminder of what we are going to look
for before merging your code.
-->
- [x] The code change is tested and works locally.
- [x] Local tests pass. **Your PR cannot be merged unless tests pass**
- [x] There is no commented out code in this PR.
- [x] I have followed the [development checklist][dev-checklist]
- [x] The code has been formatted using Black (`black --fast homeassistant tests`)
- [x] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [x] Documentation added/updated for [www.home-assistant.io][docs-repository]
If the code communicates with devices, web services, or third-party tools: `n/a`
- [ ] The [manifest file][manifest-docs] has all fields filled out correctly.
Updated and included derived files by running: `python3 -m script.hassfest`.
- [ ] New or updated dependencies have been added to `requirements_all.txt`.
Updated by running `python3 -m script.gen_requirements_all`.
- [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
- [ ] Untested files have been added to `.coveragerc`.
The integration reached or maintains the following [Integration Quality Scale][quality-scale]:
<!--
The Integration Quality Scale scores an integration on the code quality
and user experience. Each level of the quality scale consists of a list
of requirements. We highly recommend getting your integration scored!
-->
- [x] No score or internal
- [ ] 🥈 Silver
- [ ] 🥇 Gold
- [ ] 🏆 Platinum
<!--
This project is very active and we have a high turnover of pull requests.
Unfortunately, the number of incoming pull requests is higher than what our
reviewers can review and merge so there is a long backlog of pull requests
waiting for review. You can help here!
By reviewing another pull request, you will help raise the code quality of
that pull request and the final review will be faster. This way the general
pace of pull request reviews will go up and your wait time will go down.
When picking a pull request to review, try to choose one that hasn't yet
been reviewed.
Thanks for helping out!
-->
To help with the load of incoming pull requests:
- [ ] I have reviewed two other [open pull requests][prs] in this repository.
[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone+-status%3Afailure
<!--
Thank you for contributing <3
Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/en/development_checklist.html
[manifest-docs]: https://developers.home-assistant.io/docs/en/creating_integration_manifest.html
[quality-scale]: https://developers.home-assistant.io/docs/en/next/integration_quality_scale_index.html
[docs-repository]: https://github.com/home-assistant/home-assistant.io
1 Like
Anteres
(Michal Jehlář)
April 14, 2023, 7:27am
18
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.
3_14
(Piotr Machowski)
April 14, 2023, 8:38am
19
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