Caution
This custom integration tampers with internal code of Home Assistant which might cause some unforeseen issues (especially after HA updates).
If you encounter any problems related to templating engine or translations try uninstalling this integration before raising an issue in Home Assistant repository.
This integration adds possibility to use new functions in Home Assistant Jinja2 templating engine:
ct_state_translated
- returns translated state of an entityct_state_attr_translated
- returns translated value of an attribute of an entityct_translated
- returns translation for a given keyct_all_translations
- returns all available translations (that can be used withct_translated
)ct_eval
- evaluates text as a templatect_is_available
- checks if given entity is availablect_dict_merge
- Merges two or more dictionaries together.
This function returns translated state of an entity. Second parameter (language) is optional - it defaults to the language configured in Home Assistant.
Input | Output |
---|---|
|
|
This function returns translated value of an attribute of an entity. Third parameter (language) is optional - it defaults to the language configured in Home Assistant.
Input | Output |
---|---|
|
|
This function returns translation for a given key. You can use ct_all_translations
to check available keys.
Second parameter (language) is optional - it defaults to the language configured in Home Assistant.
Input | Output |
---|---|
Translated en: {{ ct_translated("component.sun.entity_component._.state.below_horizon", "en") }}
Translated en: {{ "component.sun.entity_component._.state.below_horizon" | ct_translated("en") }}
Translated nl: {{ ct_translated("component.sun.entity_component._.state.below_horizon", "nl") }}
Translated nl: {{ "component.sun.entity_component._.state.below_horizon" | ct_translated("nl") }} |
|
This function returns all available translations. Parameter (language) is optional - it defaults to the language configured in Home Assistant.
Input | Output |
---|---|
|
{
"component.sun.entity_component._.state.above_horizon": "Above horizon",
"component.sun.entity_component._.state.below_horizon": "Below horizon"
} |
This function evaluates text as a template.
Input | Output |
---|---|
{% set template_text = "{{ states('sun.sun') }}" %}
{{ ct_eval(template_text) }}
{{ template_text | ct_eval }} |
|
Optional parameters:
variables
(dict[string, Any]
) - allows adding additional variables to evaluation contextparse_result
(bool
, default:True
) - allows to disable result parsing for internal template evaluationpass_context
(bool
, default:True
) - allows to disable passing external context to evaluation of internal template
This function checks if given entity has an available state.
By default, the following states are treated as not available: unknown
, unavailable
, <empty_text>
, None
.
It is possible to override this list by providing a second argument.
Input | Output |
---|---|
{{ states('sensor.invalid') }}
{{ ct_is_available('sensor.invalid') }}
{{ ct_is_available('sensor.invalid', ['', 'unknown']) }} |
|
This function will merge one or more dictionaries (mappings) together into a single dictionary. If any key is shared between two or more dictionaries, the value of the key will be the last value passed.
Input | Output |
---|---|
{% set dict_1 = {'a':1,'b':2,'c':3} %}
{% set dict_2 = {'d':4,'e':5,'f':6} %}
{% set dict_3 = {'b':7,'d':8,'g':9} %}
{{ ct_dict_merge(dict_1, dict_1) }}
{{ ct_dict_merge(dict_1, dict_2) }}
{{ ct_dict_merge(dict_2, dict_3) }}
{{ ct_dict_merge(dict_1, dict_2, dict_3) }} |
{'a': 1, 'b': 2, 'c': 3}
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
{'d': 8, 'e': 5, 'f': 6, 'b': 7, 'g': 9}
{'a': 1, 'b': 7, 'c': 3, 'd': 8, 'e': 5, 'f': 6, 'g': 9} |
To use this integration you have to add following config in configuration.yaml
:
-
Without additional languages:
custom_templates:
-
With additional languages:
custom_templates: preload_translations: - en - nl
A list of available language tags is available here, a list of descriptions of language tags is available here.
Section preload_translations
should contain a list of languages you want to use with translations-related functions.
If it is not provided only a language provided in HA config will be loaded.
Since version v1.4.0 the minimal supported version of Home Assistant is 2024.5.0.
Using HACS (recommended)
This integration can be installed using HACS.
To do it search for Custom Templates
in Integrations section.
Download custom_templates.zip and extract its contents to config/custom_components/custom_templates
directory:
mkdir -p custom_components/custom_templates
cd custom_components/custom_templates
wget https://github.com/PiotrMachowski/Home-Assistant-custom-components-Custom-Templates/releases/latest/download/custom_templates.zip
unzip custom_templates.zip
rm custom_templates.zip
Finally, restart Home Assistant and configure the integration.
If you want to support my work with a donation you can use one of the following platforms:
Platform | Payment methods | Link | Comment |
---|---|---|---|
Ko-fi |
|
|
|
buycoffee.to |
|
||
PayPal |
|
|
|
Revolut |
|
|