Home Assistant Unofficial Reference 2024.12.1
homeassistant.helpers.template Namespace Reference

Classes

class  AllStates
 
class  DomainStates
 
class  HassLoader
 
class  RenderInfo
 
class  ResultWrapper
 
class  StateTranslated
 
class  Template
 
class  TemplateContextManager
 
class  TemplateEnvironment
 
class  TemplateState
 
class  TemplateStateBase
 
class  TemplateStateFromEntityId
 
class  TupleWrapper
 

Functions

None _attach (HomeAssistant hass, Any obj)
 
Any _cached_parse_result (str render_result)
 
None _collect_state (HomeAssistant hass, str entity_id)
 
DomainStates _domain_states (HomeAssistant hass, str name)
 
bool _false (str arg)
 
str _get_area_name (area_registry.AreaRegistry area_reg, str valid_area_id)
 
HassLoader _get_hass_loader (HomeAssistant hass)
 
TemplateState|None _get_state (HomeAssistant hass, str entity_id)
 
TemplateState|None _get_state_if_valid (HomeAssistant hass, str entity_id)
 
TemplateState|None _get_template_state_from_state (HomeAssistant hass, str entity_id, State|None state)
 
bool _is_datetime (Any value)
 
bool _is_list (Any value)
 
bool _is_set (Any value)
 
bool _is_string_like (Any value)
 
bool _is_tuple (Any value)
 
str|None _label_id_or_name (HomeAssistant hass, str label_id_or_name)
 
dict[str, str] _load_custom_templates (HomeAssistant hass)
 
Any _readonly (*Any args, **Any kwargs)
 
str _render_with_context (str template_str, jinja2.Template template, **Any kwargs)
 
State|TemplateState|None _resolve_state (HomeAssistant hass, Any entity_id_or_state)
 
Generator[TemplateState_state_generator (HomeAssistant hass, str|None domain)
 
bool|_T _T (Any value, _T default)
 
bool|_T|object _T (Any value, _T|object default=_SENTINEL)
 
TemplateState _template_state (HomeAssistant hass, State state)
 
TemplateState _template_state_no_collect (HomeAssistant hass, State state)
 
None _to_json_default (Any obj)
 
set[Any] _to_set (Any value)
 
def _to_tuple (value)
 
bool _true (str arg)
 
def add (value, amount, default=_SENTINEL)
 
def arc_cosine (value, default=_SENTINEL)
 
def arc_sine (value, default=_SENTINEL)
 
def arc_tangent (value, default=_SENTINEL)
 
def arc_tangent2 (*args, default=_SENTINEL)
 
Iterable[str] area_devices (HomeAssistant hass, str area_id_or_name)
 
Iterable[str] area_entities (HomeAssistant hass, str area_id_or_name)
 
str|None area_id (HomeAssistant hass, str lookup_value)
 
str|None area_name (HomeAssistant hass, str lookup_value)
 
Iterable[str|None] areas (HomeAssistant hass)
 
Any as_datetime (Any value, Any default=_SENTINEL)
 
timedelta|None as_timedelta (str value)
 
None async_load_custom_templates (HomeAssistant hass)
 
bool async_setup (HomeAssistant hass)
 
None attach (HomeAssistant hass, Any obj)
 
Any average (*Any args, Any default=_SENTINEL)
 
str|bytes base64_decode (str value, str|None encoding="utf-8")
 
str base64_encode (str value)
 
def bitwise_and (first_value, second_value)
 
def bitwise_or (first_value, second_value)
 
def bitwise_xor (first_value, second_value)
 
def closest (hass, *args)
 
def closest_filter (hass, *args)
 
Any config_entry_attr (HomeAssistant hass, str config_entry_id_, str attr_name)
 
str|None config_entry_id (HomeAssistant hass, str entity_id)
 
def cosine (value, default=_SENTINEL)
 
Any device_attr (HomeAssistant hass, str device_or_entity_id, str attr_name)
 
Iterable[str] device_entities (HomeAssistant hass, str _device_id)
 
str|None device_id (HomeAssistant hass, str entity_id_or_device_name)
 
def distance (hass, *args)
 
Iterable[Stateexpand (HomeAssistant hass, *Any args)
 
def fail_when_undefined (value)
 
Iterable[str] floor_areas (HomeAssistant hass, str floor_id_or_name)
 
str|None floor_id (HomeAssistant hass, Any lookup_value)
 
str|None floor_name (HomeAssistant hass, str lookup_value)
 
Iterable[str|None] floors (HomeAssistant hass)
 
def forgiving_as_timestamp (value, default=_SENTINEL)
 
bool|object forgiving_boolean (Any value)
 
def forgiving_float (value, default=_SENTINEL)
 
def forgiving_float_filter (value, default=_SENTINEL)
 
def forgiving_int (value, default=_SENTINEL, base=10)
 
def forgiving_int_filter (value, default=_SENTINEL, base=10)
 
def forgiving_round (value, precision=0, method="common", default=_SENTINEL)
 
def from_json (value)
 
type gen_result_wrapper (type[dict|list|set] kls)
 
bool has_value (HomeAssistant hass, str entity_id)
 
Any iif (Any value, Any if_true=True, Any if_false=False, Any if_none=_SENTINEL)
 
Iterable[str] integration_entities (HomeAssistant hass, str entry_name)
 
bool is_complex (Any value)
 
bool is_device_attr (HomeAssistant hass, str device_or_entity_id, str attr_name, Any attr_value)
 
bool is_hidden_entity (HomeAssistant hass, str entity_id)
 
def is_number (value)
 
bool is_state (HomeAssistant hass, str entity_id, str|list[str] state)
 
bool is_state_attr (HomeAssistant hass, str entity_id, str name, Any value)
 
bool is_template_string (str maybe_template)
 
dict[str, Any]|None issue (HomeAssistant hass, str domain, str issue_id)
 
dict[tuple[str, str], dict[str, Any]] issues (HomeAssistant hass)
 
Iterable[str] label_areas (HomeAssistant hass, str label_id_or_name)
 
Iterable[str] label_devices (HomeAssistant hass, str label_id_or_name)
 
Iterable[str] label_entities (HomeAssistant hass, str label_id_or_name)
 
str|None label_id (HomeAssistant hass, Any lookup_value)
 
str|None label_name (HomeAssistant hass, str lookup_value)
 
Iterable[str|None] labels (HomeAssistant hass, Any lookup_value=None)
 
def logarithm (value, base=math.e, default=_SENTINEL)
 
type[jinja2.Undefined] make_logging_undefined (bool|None strict, Callable[[int, str], None]|None log_fn)
 
Any median (*Any args, Any default=_SENTINEL)
 
list[Any] merge_response (ServiceResponse value)
 
Any min_max_from_filter (Any builtin_filter, str name)
 
def multiply (value, amount, default=_SENTINEL)
 
datetime now (HomeAssistant hass)
 
def ordinal (value)
 
NoReturn raise_no_default (str function, Any value)
 
def random_every_time (context, values)
 
def regex_findall (value, find="", ignorecase=False)
 
def regex_findall_index (value, find="", index=0, ignorecase=False)
 
def regex_match (value, find="", ignorecase=False)
 
def regex_replace (value="", find="", replace="", ignorecase=False)
 
def regex_search (value, find="", ignorecase=False)
 
Any relative_time (HomeAssistant hass, Any value)
 
Any render_complex (Any value, TemplateVarsType variables=None, bool limited=False, bool parse_result=True)
 
bool result_as_boolean (Any|None template_result)
 
def sine (value, default=_SENTINEL)
 
def slugify (value, separator="_")
 
def square_root (value, default=_SENTINEL)
 
Any state_attr (HomeAssistant hass, str entity_id, str name)
 
Any statistical_mode (*Any args, Any default=_SENTINEL)
 
def strptime (string, fmt, default=_SENTINEL)
 
bytes|None struct_pack (Any|None value, str format_string)
 
Any|None struct_unpack (bytes value, str format_string, int offset=0)
 
def tangent (value, default=_SENTINEL)
 
Any time_since (HomeAssistant hass, Any|datetime value, int precision=1)
 
Any time_until (HomeAssistant hass, Any|datetime value, int precision=1)
 
def timestamp_custom (value, date_format=DATE_STR_FORMAT, local=True, default=_SENTINEL)
 
def timestamp_local (value, default=_SENTINEL)
 
def timestamp_utc (value, default=_SENTINEL)
 
str to_json (Any value, bool ensure_ascii=False, bool pretty_print=False, bool sort_keys=False)
 
datetime today_at (HomeAssistant hass, str time_str="")
 
def urlencode (value)
 
datetime utcnow (HomeAssistant hass)
 
def version (value)
 

Variables

dictionary _COLLECTABLE_STATE_ATTRIBUTES
 
 _create_template_state_no_collect = partial(TemplateState, collect=False)
 
string _HASS_LOADER = "template.hass_loader"
 
 _IS_NUMERIC = re.compile(r"^[+-]?(?!0\d)\d*(?:\.\d*)?$")
 
 _LOGGER = logging.getLogger(__name__)
 
 _NO_HASS_ENV = TemplateEnvironment(None)
 
 _regex_cache = lru_cache(maxsize=128)(re.compile)
 
dictionary _RESERVED_NAMES
 
 _SENTINEL = object()
 
 _template_context_manager = TemplateContextManager()
 
int ALL_STATES_RATE_LIMIT = 60
 
int CACHED_TEMPLATE_STATES = 512
 
string DATE_STR_FORMAT = "%Y-%m-%d %H:%M:%S"
 
 default
 
int DOMAIN_STATES_RATE_LIMIT = 1
 
float ENTITY_COUNT_GROWTH_FACTOR = 1.2
 
int EVAL_CACHE_SIZE = 512
 
int MAX_CUSTOM_TEMPLATE_SIZE = 5 * 1024 * 1024
 
int MAX_TEMPLATE_OUTPUT = 256 * 1024
 
tuple ORJSON_PASSTHROUGH_OPTIONS
 
 render_result
 

Detailed Description

Template helper methods for rendering strings with Home Assistant data.

Function Documentation

◆ _attach()

None homeassistant.helpers.template._attach ( HomeAssistant  hass,
Any  obj 
)
private
Recursively attach hass to all template instances in list and dict.

Definition at line 222 of file template.py.

◆ _cached_parse_result()

Any homeassistant.helpers.template._cached_parse_result ( str  render_result)
private
Parse a result and cache the result.

Definition at line 345 of file template.py.

◆ _collect_state()

None homeassistant.helpers.template._collect_state ( HomeAssistant  hass,
str  entity_id 
)
private

Definition at line 1194 of file template.py.

◆ _domain_states()

DomainStates homeassistant.helpers.template._domain_states ( HomeAssistant  hass,
str  name 
)
private

Definition at line 873 of file template.py.

◆ _false()

bool homeassistant.helpers.template._false ( str  arg)
private

Definition at line 340 of file template.py.

◆ _get_area_name()

str homeassistant.helpers.template._get_area_name ( area_registry.AreaRegistry  area_reg,
str  valid_area_id 
)
private
Get area name from valid area ID.

Definition at line 1552 of file template.py.

◆ _get_hass_loader()

HassLoader homeassistant.helpers.template._get_hass_loader ( HomeAssistant  hass)
private

Definition at line 2810 of file template.py.

◆ _get_state()

TemplateState | None homeassistant.helpers.template._get_state ( HomeAssistant  hass,
str  entity_id 
)
private

Definition at line 1228 of file template.py.

◆ _get_state_if_valid()

TemplateState | None homeassistant.helpers.template._get_state_if_valid ( HomeAssistant  hass,
str  entity_id 
)
private

Definition at line 1221 of file template.py.

◆ _get_template_state_from_state()

TemplateState | None homeassistant.helpers.template._get_template_state_from_state ( HomeAssistant  hass,
str  entity_id,
State | None   state 
)
private

Definition at line 1232 of file template.py.

◆ _is_datetime()

bool homeassistant.helpers.template._is_datetime ( Any  value)
private
Return whether a value is a datetime.

Definition at line 2411 of file template.py.

◆ _is_list()

bool homeassistant.helpers.template._is_list ( Any  value)
private
Return whether a value is a list.

Definition at line 2386 of file template.py.

◆ _is_set()

bool homeassistant.helpers.template._is_set ( Any  value)
private
Return whether a value is a set.

Definition at line 2391 of file template.py.

◆ _is_string_like()

bool homeassistant.helpers.template._is_string_like ( Any  value)
private
Return whether a value is a string or string like object.

Definition at line 2416 of file template.py.

◆ _is_tuple()

bool homeassistant.helpers.template._is_tuple ( Any  value)
private
Return whether a value is a tuple.

Definition at line 2396 of file template.py.

◆ _label_id_or_name()

str | None homeassistant.helpers.template._label_id_or_name ( HomeAssistant  hass,
str  label_id_or_name 
)
private
Get the label ID from a label name or ID.

Definition at line 1690 of file template.py.

◆ _load_custom_templates()

dict[str, str] homeassistant.helpers.template._load_custom_templates ( HomeAssistant  hass)
private

Definition at line 2794 of file template.py.

◆ _readonly()

Any homeassistant.helpers.template._readonly ( *Any  args,
**Any  kwargs 
)
private
Raise an exception when a states object is modified.

Definition at line 877 of file template.py.

◆ _render_with_context()

str homeassistant.helpers.template._render_with_context ( str  template_str,
jinja2.Template  template,
**Any   kwargs 
)
private
Store template being rendered in a ContextVar to aid error handling.

Definition at line 2727 of file template.py.

◆ _resolve_state()

State | TemplateState | None homeassistant.helpers.template._resolve_state ( HomeAssistant  hass,
Any   entity_id_or_state 
)
private
Return state or entity_id if given.

Definition at line 1243 of file template.py.

◆ _state_generator()

Generator[TemplateState] homeassistant.helpers.template._state_generator ( HomeAssistant  hass,
str | None   domain 
)
private
State generator for a domain or all states.

Definition at line 1199 of file template.py.

◆ _T() [1/2]

bool | _T homeassistant.helpers.template._T ( Any  value,
_T  default 
)
private

Definition at line 1259 of file template.py.

◆ _T() [2/2]

bool | _T | object homeassistant.helpers.template._T ( Any  value,
_T | object   default = _SENTINEL 
)
private
Try to convert value to a boolean.

Definition at line 1262 of file template.py.

◆ _template_state()

TemplateState homeassistant.helpers.template._template_state ( HomeAssistant  hass,
State  state 
)
private
Return a TemplateState for a state that collects.

Definition at line 176 of file template.py.

◆ _template_state_no_collect()

TemplateState homeassistant.helpers.template._template_state_no_collect ( HomeAssistant  hass,
State  state 
)
private
Return a TemplateState for a state without collecting.

Definition at line 167 of file template.py.

◆ _to_json_default()

None homeassistant.helpers.template._to_json_default ( Any  obj)
private
Disable custom types in json serialization.

Definition at line 2541 of file template.py.

◆ _to_set()

set[Any] homeassistant.helpers.template._to_set ( Any  value)
private
Convert value to set.

Definition at line 2401 of file template.py.

◆ _to_tuple()

def homeassistant.helpers.template._to_tuple (   value)
private
Convert value to tuple.

Definition at line 2406 of file template.py.

◆ _true()

bool homeassistant.helpers.template._true ( str  arg)
private

Definition at line 336 of file template.py.

◆ add()

def homeassistant.helpers.template.add (   value,
  amount,
  default = _SENTINEL 
)
Filter to convert value to float and add it.

Definition at line 1948 of file template.py.

◆ arc_cosine()

def homeassistant.helpers.template.arc_cosine (   value,
  default = _SENTINEL 
)
Filter and function to get arc cosine of the value.

Definition at line 2016 of file template.py.

◆ arc_sine()

def homeassistant.helpers.template.arc_sine (   value,
  default = _SENTINEL 
)
Filter and function to get arc sine of the value.

Definition at line 2006 of file template.py.

◆ arc_tangent()

def homeassistant.helpers.template.arc_tangent (   value,
  default = _SENTINEL 
)
Filter and function to get arc tangent of the value.

Definition at line 2026 of file template.py.

◆ arc_tangent2()

def homeassistant.helpers.template.arc_tangent2 ( args,
  default = _SENTINEL 
)
Filter and function to calculate four quadrant arc tangent of y / x.

The parameters to atan2 may be passed either in an iterable or as separate arguments
The default value may be passed either as a positional or in a keyword argument

Definition at line 2036 of file template.py.

◆ area_devices()

Iterable[str] homeassistant.helpers.template.area_devices ( HomeAssistant  hass,
str  area_id_or_name 
)
Return device IDs for a given area ID or name.

Definition at line 1624 of file template.py.

◆ area_entities()

Iterable[str] homeassistant.helpers.template.area_entities ( HomeAssistant  hass,
str  area_id_or_name 
)
Return entities for a given area ID or name.

Definition at line 1594 of file template.py.

◆ area_id()

str | None homeassistant.helpers.template.area_id ( HomeAssistant  hass,
str  lookup_value 
)
Get the area ID from an area name, device id, or entity id.

Definition at line 1521 of file template.py.

◆ area_name()

str | None homeassistant.helpers.template.area_name ( HomeAssistant  hass,
str  lookup_value 
)
Get the area name from an area id, device id, or entity id.

Definition at line 1559 of file template.py.

◆ areas()

Iterable[str | None] homeassistant.helpers.template.areas ( HomeAssistant  hass)
Return all areas.

Definition at line 1516 of file template.py.

◆ as_datetime()

Any homeassistant.helpers.template.as_datetime ( Any  value,
Any   default = _SENTINEL 
)
Filter and to convert a time string or UNIX timestamp to datetime object.

Definition at line 2122 of file template.py.

◆ as_timedelta()

timedelta | None homeassistant.helpers.template.as_timedelta ( str  value)
Parse a ISO8601 duration like 'PT10M' to a timedelta.

Definition at line 2148 of file template.py.

◆ async_load_custom_templates()

None homeassistant.helpers.template.async_load_custom_templates ( HomeAssistant  hass)
Load all custom jinja files under 5MiB into memory.

Definition at line 2788 of file template.py.

◆ async_setup()

bool homeassistant.helpers.template.async_setup ( HomeAssistant  hass)
Set up tracking the template LRUs.

Definition at line 185 of file template.py.

◆ attach()

None homeassistant.helpers.template.attach ( HomeAssistant  hass,
Any  obj 
)
Recursively attach hass to all template instances in list and dict.

Definition at line 217 of file template.py.

◆ average()

Any homeassistant.helpers.template.average ( *Any  args,
Any   default = _SENTINEL 
)
Filter and function to calculate the arithmetic mean.

Calculates of an iterable or of two or more arguments.

The parameters may be passed as an iterable or as separate arguments.

Definition at line 2250 of file template.py.

◆ base64_decode()

str | bytes homeassistant.helpers.template.base64_decode ( str  value,
str | None   encoding = "utf-8" 
)
Perform base64 decode.

Definition at line 2517 of file template.py.

◆ base64_encode()

str homeassistant.helpers.template.base64_encode ( str  value)
Perform base64 encode.

Definition at line 2512 of file template.py.

◆ bitwise_and()

def homeassistant.helpers.template.bitwise_and (   first_value,
  second_value 
)
Perform a bitwise and operation.

Definition at line 2461 of file template.py.

◆ bitwise_or()

def homeassistant.helpers.template.bitwise_or (   first_value,
  second_value 
)
Perform a bitwise or operation.

Definition at line 2466 of file template.py.

◆ bitwise_xor()

def homeassistant.helpers.template.bitwise_xor (   first_value,
  second_value 
)
Perform a bitwise xor operation.

Definition at line 2471 of file template.py.

◆ closest()

def homeassistant.helpers.template.closest (   hass,
args 
)
Find closest entity.

Closest to home:
    closest(states)
    closest(states.device_tracker)
    closest('group.children')
    closest(states.group.children)

Closest to a point:
    closest(23.456, 23.456, 'group.children')
    closest('zone.school', 'group.children')
    closest(states.zone.school, 'group.children')

As a filter:
    states | closest
    states.device_tracker | closest
    ['group.children', states.device_tracker] | closest
    'group.children' | closest(23.456, 23.456)
    states.device_tracker | closest('zone.school')
    'group.children' | closest(states.zone.school)

Definition at line 1726 of file template.py.

◆ closest_filter()

def homeassistant.helpers.template.closest_filter (   hass,
args 
)
Call closest as a filter. Need to reorder arguments.

Definition at line 1789 of file template.py.

◆ config_entry_attr()

Any homeassistant.helpers.template.config_entry_attr ( HomeAssistant  hass,
str  config_entry_id_,
str   attr_name 
)
Get config entry specific attribute.

Definition at line 1421 of file template.py.

◆ config_entry_id()

str | None homeassistant.helpers.template.config_entry_id ( HomeAssistant  hass,
str  entity_id 
)
Get an config entry ID from an entity ID.

Definition at line 1376 of file template.py.

◆ cosine()

def homeassistant.helpers.template.cosine (   value,
  default = _SENTINEL 
)
Filter and function to get cosine of the value.

Definition at line 1986 of file template.py.

◆ device_attr()

Any homeassistant.helpers.template.device_attr ( HomeAssistant  hass,
str  device_or_entity_id,
str  attr_name 
)
Get the device specific attribute.

Definition at line 1403 of file template.py.

◆ device_entities()

Iterable[str] homeassistant.helpers.template.device_entities ( HomeAssistant  hass,
str  _device_id 
)
Get entity ids for entities tied to a device.

Definition at line 1333 of file template.py.

◆ device_id()

str | None homeassistant.helpers.template.device_id ( HomeAssistant  hass,
str  entity_id_or_device_name 
)
Get a device ID from an entity ID or device name.

Definition at line 1384 of file template.py.

◆ distance()

def homeassistant.helpers.template.distance (   hass,
args 
)
Calculate distance.

Will calculate distance from home to a point or between points.
Points can be passed in using state objects or lat/lng coordinates.

Definition at line 1796 of file template.py.

◆ expand()

Iterable[State] homeassistant.helpers.template.expand ( HomeAssistant  hass,
*Any  args 
)
Expand out any groups and zones into entity states.

Definition at line 1290 of file template.py.

◆ fail_when_undefined()

def homeassistant.helpers.template.fail_when_undefined (   value)
Filter to force a failure when the value is undefined.

Definition at line 2220 of file template.py.

◆ floor_areas()

Iterable[str] homeassistant.helpers.template.floor_areas ( HomeAssistant  hass,
str  floor_id_or_name 
)
Return area IDs for a given floor ID or name.

Definition at line 1499 of file template.py.

◆ floor_id()

str | None homeassistant.helpers.template.floor_id ( HomeAssistant  hass,
Any  lookup_value 
)
Get the floor ID from a floor name.

Definition at line 1467 of file template.py.

◆ floor_name()

str | None homeassistant.helpers.template.floor_name ( HomeAssistant  hass,
str  lookup_value 
)
Get the floor name from a floor id.

Definition at line 1481 of file template.py.

◆ floors()

Iterable[str | None] homeassistant.helpers.template.floors ( HomeAssistant  hass)
Return all floors.

Definition at line 1461 of file template.py.

◆ forgiving_as_timestamp()

def homeassistant.helpers.template.forgiving_as_timestamp (   value,
  default = _SENTINEL 
)
Filter and function which tries to convert value to timestamp.

Definition at line 2112 of file template.py.

◆ forgiving_boolean()

bool | object homeassistant.helpers.template.forgiving_boolean ( Any  value)

Definition at line 1255 of file template.py.

◆ forgiving_float()

def homeassistant.helpers.template.forgiving_float (   value,
  default = _SENTINEL 
)
Try to convert value to a float.

Definition at line 2339 of file template.py.

◆ forgiving_float_filter()

def homeassistant.helpers.template.forgiving_float_filter (   value,
  default = _SENTINEL 
)
Try to convert value to a float.

Definition at line 2349 of file template.py.

◆ forgiving_int()

def homeassistant.helpers.template.forgiving_int (   value,
  default = _SENTINEL,
  base = 10 
)
Try to convert value to an int, and raise if it fails.

Definition at line 2359 of file template.py.

◆ forgiving_int_filter()

def homeassistant.helpers.template.forgiving_int_filter (   value,
  default = _SENTINEL,
  base = 10 
)
Try to convert value to an int, and raise if it fails.

Definition at line 2367 of file template.py.

◆ forgiving_round()

def homeassistant.helpers.template.forgiving_round (   value,
  precision = 0,
  method = "common",
  default = _SENTINEL 
)
Filter to round a value.

Definition at line 1915 of file template.py.

◆ from_json()

def homeassistant.helpers.template.from_json (   value)
Convert a JSON string to an object.

Definition at line 2536 of file template.py.

◆ gen_result_wrapper()

type homeassistant.helpers.template.gen_result_wrapper ( type[dict | list | set]  kls)
Generate a result wrapper.

Definition at line 285 of file template.py.

◆ has_value()

bool homeassistant.helpers.template.has_value ( HomeAssistant  hass,
str  entity_id 
)
Test if an entity has a valid value.

Definition at line 1881 of file template.py.

◆ iif()

Any homeassistant.helpers.template.iif ( Any  value,
Any   if_true = True,
Any   if_false = False,
Any   if_none = _SENTINEL 
)
Immediate if function/filter that allow for common if/else constructs.

https://en.wikipedia.org/wiki/IIf

Examples:
    {{ is_state("device_tracker.frenck", "home") | iif("yes", "no") }}
    {{ iif(1==2, "yes", "no") }}
    {{ (1 == 1) | iif("yes", "no") }}

Definition at line 2687 of file template.py.

◆ integration_entities()

Iterable[str] homeassistant.helpers.template.integration_entities ( HomeAssistant  hass,
str  entry_name 
)
Get entity ids for entities tied to an integration/domain.

Provide entry_name as domain to get all entity id's for a integration/domain
or provide a config entry title for filtering between instances of the same
integration.

Definition at line 1340 of file template.py.

◆ is_complex()

bool homeassistant.helpers.template.is_complex ( Any  value)
Test if data structure is a complex template.

Definition at line 259 of file template.py.

◆ is_device_attr()

bool homeassistant.helpers.template.is_device_attr ( HomeAssistant  hass,
str  device_or_entity_id,
str  attr_name,
Any   attr_value 
)
Test if a device's attribute is a specific value.

Definition at line 1439 of file template.py.

◆ is_hidden_entity()

bool homeassistant.helpers.template.is_hidden_entity ( HomeAssistant  hass,
str  entity_id 
)
Test if an entity is hidden.

Definition at line 1853 of file template.py.

◆ is_number()

def homeassistant.helpers.template.is_number (   value)
Try to convert value to a float.

Definition at line 2375 of file template.py.

◆ is_state()

bool homeassistant.helpers.template.is_state ( HomeAssistant  hass,
str  entity_id,
str | list[str]  state 
)
Test if a state is a specific value.

Definition at line 1860 of file template.py.

◆ is_state_attr()

bool homeassistant.helpers.template.is_state_attr ( HomeAssistant  hass,
str  entity_id,
str  name,
Any  value 
)
Test if a state's attribute is a specific value.

Definition at line 1868 of file template.py.

◆ is_template_string()

bool homeassistant.helpers.template.is_template_string ( str  maybe_template)
Check if the input is a Jinja2 template.

Definition at line 272 of file template.py.

◆ issue()

dict[str, Any] | None homeassistant.helpers.template.issue ( HomeAssistant  hass,
str  domain,
str  issue_id 
)
Get issue by domain and issue_id.

Definition at line 1453 of file template.py.

◆ issues()

dict[tuple[str, str], dict[str, Any]] homeassistant.helpers.template.issues ( HomeAssistant  hass)
Return all open issues.

Definition at line 1446 of file template.py.

◆ label_areas()

Iterable[str] homeassistant.helpers.template.label_areas ( HomeAssistant  hass,
str  label_id_or_name 
)
Return areas for a given label ID or name.

Definition at line 1699 of file template.py.

◆ label_devices()

Iterable[str] homeassistant.helpers.template.label_devices ( HomeAssistant  hass,
str  label_id_or_name 
)
Return device IDs for a given label ID or name.

Definition at line 1708 of file template.py.

◆ label_entities()

Iterable[str] homeassistant.helpers.template.label_entities ( HomeAssistant  hass,
str  label_id_or_name 
)
Return entities for a given label ID or name.

Definition at line 1717 of file template.py.

◆ label_id()

str | None homeassistant.helpers.template.label_id ( HomeAssistant  hass,
Any  lookup_value 
)
Get the label ID from a label name.

Definition at line 1674 of file template.py.

◆ label_name()

str | None homeassistant.helpers.template.label_name ( HomeAssistant  hass,
str  lookup_value 
)
Get the label name from a label ID.

Definition at line 1682 of file template.py.

◆ labels()

Iterable[str | None] homeassistant.helpers.template.labels ( HomeAssistant  hass,
Any   lookup_value = None 
)
Return all labels, or those from a area ID, device ID, or entity ID.

Definition at line 1640 of file template.py.

◆ logarithm()

def homeassistant.helpers.template.logarithm (   value,
  base = math.e,
  default = _SENTINEL 
)
Filter and function to get logarithm of the value with a specific base.

Definition at line 1959 of file template.py.

◆ make_logging_undefined()

type[jinja2.Undefined] homeassistant.helpers.template.make_logging_undefined ( bool | None  strict,
Callable[[int, str], None] | None   log_fn 
)
Log on undefined variables.

Definition at line 2736 of file template.py.

◆ median()

Any homeassistant.helpers.template.median ( *Any  args,
Any   default = _SENTINEL 
)
Filter and function to calculate the median.

Calculates median of an iterable of two or more arguments.

The parameters may be passed as an iterable or as separate arguments.

Definition at line 2279 of file template.py.

◆ merge_response()

list[Any] homeassistant.helpers.template.merge_response ( ServiceResponse  value)
Merge action responses into single list.

Checks that the input is a correct service response:
{
    "entity_id": {str: dict[str, Any]},
}
If response is a single list, it will extend the list with the items
    and add the entity_id and value_key to each dictionary for reference.
If response is a dictionary or multiple lists,
    it will append the dictionary/lists to the list
    and add the entity_id to each dictionary for reference.

Definition at line 2153 of file template.py.

◆ min_max_from_filter()

Any homeassistant.helpers.template.min_max_from_filter ( Any  builtin_filter,
str  name 
)
Convert a built-in min/max Jinja filter to a global function.

The parameters may be passed as an iterable or as separate arguments.

Definition at line 2227 of file template.py.

◆ multiply()

def homeassistant.helpers.template.multiply (   value,
  amount,
  default = _SENTINEL 
)
Filter to convert value to float and multiply it.

Definition at line 1937 of file template.py.

◆ now()

datetime homeassistant.helpers.template.now ( HomeAssistant  hass)
Record fetching now.

Definition at line 1890 of file template.py.

◆ ordinal()

def homeassistant.helpers.template.ordinal (   value)
Perform ordinal conversion.

Definition at line 2526 of file template.py.

◆ raise_no_default()

NoReturn homeassistant.helpers.template.raise_no_default ( str  function,
Any  value 
)
Log warning if no default is specified.

Definition at line 1906 of file template.py.

◆ random_every_time()

def homeassistant.helpers.template.random_every_time (   context,
  values 
)
Choose a random value.

Unlike Jinja's random filter,
this is context-dependent to avoid caching the chosen value.

Definition at line 2580 of file template.py.

◆ regex_findall()

def homeassistant.helpers.template.regex_findall (   value,
  find = "",
  ignorecase = False 
)
Find all matches using regex.

Definition at line 2453 of file template.py.

◆ regex_findall_index()

def homeassistant.helpers.template.regex_findall_index (   value,
  find = "",
  index = 0,
  ignorecase = False 
)
Find all matches using regex and then pick specific match index.

Definition at line 2448 of file template.py.

◆ regex_match()

def homeassistant.helpers.template.regex_match (   value,
  find = "",
  ignorecase = False 
)
Match value using regex.

Definition at line 2421 of file template.py.

◆ regex_replace()

def homeassistant.helpers.template.regex_replace (   value = "",
  find = "",
  replace = "",
  ignorecase = False 
)
Replace using regex.

Definition at line 2432 of file template.py.

◆ regex_search()

def homeassistant.helpers.template.regex_search (   value,
  find = "",
  ignorecase = False 
)
Search using regex.

Definition at line 2440 of file template.py.

◆ relative_time()

Any homeassistant.helpers.template.relative_time ( HomeAssistant  hass,
Any  value 
)
Take a datetime and return its "age" as a string.

The age can be in second, minute, hour, day, month or year. Only the
biggest unit is considered, e.g. if it's 2 days and 3 hours, "2 days" will
be returned.
If the input datetime is in the future,
the input datetime will be returned.

If the input are not a datetime object the input will be returned unmodified.

Note: This template function is deprecated in favor of `time_until`, but is still
supported so as not to break old templates.

Definition at line 2606 of file template.py.

◆ render_complex()

Any homeassistant.helpers.template.render_complex ( Any  value,
TemplateVarsType   variables = None,
bool   limited = False,
bool   parse_result = True 
)
Recursive template creator helper function.

Definition at line 235 of file template.py.

◆ result_as_boolean()

bool homeassistant.helpers.template.result_as_boolean ( Any | None  template_result)
Convert the template result to a boolean.

True/not 0/'1'/'true'/'yes'/'on'/'enable' are considered truthy
False/0/None/'0'/'false'/'no'/'off'/'disable' are considered falsy
All other values are falsy

Definition at line 1277 of file template.py.

◆ sine()

def homeassistant.helpers.template.sine (   value,
  default = _SENTINEL 
)
Filter and function to get sine of the value.

Definition at line 1976 of file template.py.

◆ slugify()

def homeassistant.helpers.template.slugify (   value,
  separator = "_" 
)
Convert a string into a slug, such as what is used for entity ids.

Definition at line 2682 of file template.py.

◆ square_root()

def homeassistant.helpers.template.square_root (   value,
  default = _SENTINEL 
)
Filter and function to get square root of the value.

Definition at line 2064 of file template.py.

◆ state_attr()

Any homeassistant.helpers.template.state_attr ( HomeAssistant  hass,
str  entity_id,
str  name 
)
Get a specific attribute from a state.

Definition at line 1874 of file template.py.

◆ statistical_mode()

Any homeassistant.helpers.template.statistical_mode ( *Any  args,
Any   default = _SENTINEL 
)
Filter and function to calculate the statistical mode.

Calculates mode of an iterable of two or more arguments.

The parameters may be passed as an iterable or as separate arguments.

Definition at line 2308 of file template.py.

◆ strptime()

def homeassistant.helpers.template.strptime (   string,
  fmt,
  default = _SENTINEL 
)
Parse a time string to datetime.

Definition at line 2210 of file template.py.

◆ struct_pack()

bytes | None homeassistant.helpers.template.struct_pack ( Any | None  value,
str  format_string 
)
Pack an object into a bytes object.

Definition at line 2476 of file template.py.

◆ struct_unpack()

Any | None homeassistant.helpers.template.struct_unpack ( bytes  value,
str  format_string,
int   offset = 0 
)
Unpack an object from bytes an return the first native object.

Definition at line 2494 of file template.py.

◆ tangent()

def homeassistant.helpers.template.tangent (   value,
  default = _SENTINEL 
)
Filter and function to get tangent of the value.

Definition at line 1996 of file template.py.

◆ time_since()

Any homeassistant.helpers.template.time_since ( HomeAssistant  hass,
Any | datetime  value,
int   precision = 1 
)
Take a datetime and return its "age" as a string.

The age can be in seconds, minutes, hours, days, months and year.

precision is the number of units to return, with the last unit rounded.

If the value not a datetime object the input will be returned unmodified.

Definition at line 2633 of file template.py.

◆ time_until()

Any homeassistant.helpers.template.time_until ( HomeAssistant  hass,
Any | datetime  value,
int   precision = 1 
)
Take a datetime and return the amount of time until that time as a string.

The time until can be in seconds, minutes, hours, days, months and years.

precision is the number of units to return, with the last unit rounded.

If the value not a datetime object the input will be returned unmodified.

Definition at line 2655 of file template.py.

◆ timestamp_custom()

def homeassistant.helpers.template.timestamp_custom (   value,
  date_format = DATE_STR_FORMAT,
  local = True,
  default = _SENTINEL 
)
Filter to convert given timestamp to format.

Definition at line 2074 of file template.py.

◆ timestamp_local()

def homeassistant.helpers.template.timestamp_local (   value,
  default = _SENTINEL 
)
Filter to convert given timestamp to local date/time.

Definition at line 2090 of file template.py.

◆ timestamp_utc()

def homeassistant.helpers.template.timestamp_utc (   value,
  default = _SENTINEL 
)
Filter to convert given timestamp to UTC date/time.

Definition at line 2101 of file template.py.

◆ to_json()

str homeassistant.helpers.template.to_json ( Any  value,
bool   ensure_ascii = False,
bool   pretty_print = False,
bool   sort_keys = False 
)
Convert an object to a JSON string.

Definition at line 2546 of file template.py.

◆ today_at()

datetime homeassistant.helpers.template.today_at ( HomeAssistant  hass,
str   time_str = "" 
)
Record fetching now where the time has been replaced with value.

Definition at line 2589 of file template.py.

◆ urlencode()

def homeassistant.helpers.template.urlencode (   value)
Urlencode dictionary and return as UTF-8 string.

Definition at line 2677 of file template.py.

◆ utcnow()

datetime homeassistant.helpers.template.utcnow ( HomeAssistant  hass)
Record fetching utcnow.

Definition at line 1898 of file template.py.

◆ version()

def homeassistant.helpers.template.version (   value)
Filter and function to get version object of the value.

Definition at line 2059 of file template.py.

Variable Documentation

◆ _COLLECTABLE_STATE_ATTRIBUTES

dictionary homeassistant.helpers.template._COLLECTABLE_STATE_ATTRIBUTES
private
Initial value:
1 = {
2  "state",
3  "attributes",
4  "last_changed",
5  "last_updated",
6  "context",
7  "domain",
8  "object_id",
9  "name",
10 }

Definition at line 116 of file template.py.

◆ _create_template_state_no_collect

homeassistant.helpers.template._create_template_state_no_collect = partial(TemplateState, collect=False)
private

Definition at line 1191 of file template.py.

◆ _HASS_LOADER

string homeassistant.helpers.template._HASS_LOADER = "template.hass_loader"
private

Definition at line 104 of file template.py.

◆ _IS_NUMERIC

homeassistant.helpers.template._IS_NUMERIC = re.compile(r"^[+-]?(?!0\d)\d*(?:\.\d*)?$")
private

Definition at line 107 of file template.py.

◆ _LOGGER

homeassistant.helpers.template._LOGGER = logging.getLogger(__name__)
private

Definition at line 93 of file template.py.

◆ _NO_HASS_ENV

homeassistant.helpers.template._NO_HASS_ENV = TemplateEnvironment(None)
private

Definition at line 3215 of file template.py.

◆ _regex_cache

homeassistant.helpers.template._regex_cache = lru_cache(maxsize=128)(re.compile)
private

Definition at line 2429 of file template.py.

◆ _RESERVED_NAMES

dictionary homeassistant.helpers.template._RESERVED_NAMES
private
Initial value:
1 = {
2  "contextfunction",
3  "evalcontextfunction",
4  "environmentfunction",
5  "jinja_pass_arg",
6 }

Definition at line 109 of file template.py.

◆ _SENTINEL

homeassistant.helpers.template._SENTINEL = object()
private

Definition at line 94 of file template.py.

◆ _template_context_manager

homeassistant.helpers.template._template_context_manager = TemplateContextManager()
private

Definition at line 2724 of file template.py.

◆ ALL_STATES_RATE_LIMIT

int homeassistant.helpers.template.ALL_STATES_RATE_LIMIT = 60

Definition at line 127 of file template.py.

◆ CACHED_TEMPLATE_STATES

int homeassistant.helpers.template.CACHED_TEMPLATE_STATES = 512

Definition at line 152 of file template.py.

◆ DATE_STR_FORMAT

string homeassistant.helpers.template.DATE_STR_FORMAT = "%Y-%m-%d %H:%M:%S"

Definition at line 95 of file template.py.

◆ default

homeassistant.helpers.template.default

Definition at line 130 of file template.py.

◆ DOMAIN_STATES_RATE_LIMIT

int homeassistant.helpers.template.DOMAIN_STATES_RATE_LIMIT = 1

Definition at line 128 of file template.py.

◆ ENTITY_COUNT_GROWTH_FACTOR

float homeassistant.helpers.template.ENTITY_COUNT_GROWTH_FACTOR = 1.2

Definition at line 160 of file template.py.

◆ EVAL_CACHE_SIZE

int homeassistant.helpers.template.EVAL_CACHE_SIZE = 512

Definition at line 153 of file template.py.

◆ MAX_CUSTOM_TEMPLATE_SIZE

int homeassistant.helpers.template.MAX_CUSTOM_TEMPLATE_SIZE = 5 * 1024 * 1024

Definition at line 155 of file template.py.

◆ MAX_TEMPLATE_OUTPUT

int homeassistant.helpers.template.MAX_TEMPLATE_OUTPUT = 256 * 1024

Definition at line 156 of file template.py.

◆ ORJSON_PASSTHROUGH_OPTIONS

tuple homeassistant.helpers.template.ORJSON_PASSTHROUGH_OPTIONS
Initial value:
1 = (
2  orjson.OPT_PASSTHROUGH_DATACLASS | orjson.OPT_PASSTHROUGH_DATETIME
3 )

Definition at line 162 of file template.py.

◆ render_result

homeassistant.helpers.template.render_result

Definition at line 293 of file template.py.