Skip to content

PiotrMachowski/Home-Assistant-custom-components-Custom-Templates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HACS Default GitHub Latest Release GitHub All Releases Installations Community Forum Ko-Fi buycoffee.to PayPal.Me Revolut.Me

Custom Templates

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 entity
  • ct_state_attr_translated - returns translated value of an attribute of an entity
  • 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
  • ct_is_available - checks if given entity is available
  • ct_dict_merge - Merges two or more dictionaries together.

Usage

ct_state_translated

This function returns translated state of an entity. Second parameter (language) is optional - it defaults to the language configured in Home Assistant.

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

ct_state_attr_translated

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
Attribute: {{ state_attr("automation.example", "mode") }}
Translated en: {{ ct_state_attr_translated("automation.example", "mode", "en") }}
Translated en: {{ "automation.example" | ct_state_attr_translated("mode", "en") }}
Translated nl: {{ ct_state_attr_translated("automation.example", "mode", "nl") }}
Translated nl: {{ "automation.example" | ct_state_attr_translated("mode", "nl") }}
Attribute: single
Translated en: Single
Translated en: Single
Translated nl: Enkelvoudig
Translated nl: Enkelvoudig

ct_translated

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") }}
Translated en: Below horizon
Translated en: Below horizon
Translated nl: Onder de horizon
Translated nl: Onder de horizon

ct_all_translations

This function returns all available translations. Parameter (language) is optional - it defaults to the language configured in Home Assistant.

Input Output
{{ ct_all_translations("en") }}
{
  "component.sun.entity_component._.state.above_horizon": "Above horizon",
  "component.sun.entity_component._.state.below_horizon": "Below horizon"
}

ct_eval

This function evaluates text as a template.

Input Output
{% set template_text = "{{ states('sun.sun') }}" %}
{{ ct_eval(template_text) }}
{{ template_text | ct_eval }}
below_horizon
below_horizon

Optional parameters:

  • variables (dict[string, Any]) - allows adding additional variables to evaluation context
  • parse_result (bool, default: True) - allows to disable result parsing for internal template evaluation
  • pass_context (bool, default: True) - allows to disable passing external context to evaluation of internal template

ct_is_available

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']) }}
unavailable
true
false

ct_dict_merge

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}

Configuration

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.

Installation

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.

Manual

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.

Support

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
  • PayPal
  • Credit card
  • Buy Me a Coffee at ko-fi.com
  • No fees
  • Single or monthly payment
  • buycoffee.to
  • BLIK
  • Bank transfer
  • Postaw mi kawę na buycoffee.to
    PayPal
  • PayPal
  • PayPal Logo
  • No fees
  • Revolut
  • Revolut
  • Credit Card
  • Revolut
  • No fees