Skip to content

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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 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

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

Usage

ct_state_translated

This function returns translated state of an entity.

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.

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.

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.

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

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

Configuration

To use this integration you have to add following config in configuration.yaml:

custom_templates:
  preload_translations:
    - en
    - nl

A list of available language tags is available here, a list of desriptions of language tags is available here.

Section preload_translations should contain a list of languages you want to use with translations-related functions.

Installation

Since version v1.3.0 the minimal supported version of Home Assistant is 2024.2.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