Collection template functions
These template functions help you work with collections of data: lists, sets, dictionaries, and JSON. Use them to combine, filter, sort, and reshape data from your entities, API responses, and incoming messages.
Home Assistant extends the Jinja2 template engine with many custom functions, filters, and tests. Each function on this page has its own page with detailed descriptions, parameters, and practical examples.
New to templates? Start with the templating documentation to learn the basics.
-
Get object ibute
attrReads an attribute from an object when the attribute name comes from a variable. -
Split list into es
batchSplits a list into smaller batches of a specified size. -
Merge dictionaries
combineMerges multiple dictionaries into one. -
Sort a dictionary
dictsortSorts a dictionary by its keys or values, returning a list of key/value pairs. -
Set
differenceReturns items in the first list but not in the second (set difference). -
Get item
firstReturns the first item of a list or the first character of a string. -
Flatten nested lists
flattenFlattens nested lists into a single flat list. -
Parse JSON string
from_jsonParses a JSON string into a Python object. -
Group items by attribute
groupbyGroups a list of items by a common attribute, producing a list of (grouper, list) pairs. -
Set ion
intersectReturns items common to both lists (set intersection). -
Get dictionary key/value pairs
itemsReturns the key/value pairs from a dictionary as a list of tuples. -
Get item
lastReturns the last item of a list or the last character of a string. -
Get of a collection
lengthReturns the number of items in a list, characters in a string, or keys in a dictionary. Also available as count. -
Transform list items
mapApplies a filter to each item or extracts an attribute from each item in a list. -
Merge action responses
merge_responseMerges action response dictionaries into a flat list. -
Remove items by test
rejectFilters a list, removing items that pass a given test. The opposite of select. -
Remove items by attribute test
rejectattrFilters a list, removing items where a specified attribute passes a test. The opposite of selectattr. -
Reverse a list or string
reverseReverses the order of items in a list or characters in a string. -
Filter items by test
selectFilters a list, keeping only items that pass a given test. -
Filter items by attribute test
selectattrFilters a list, keeping only items where a specified attribute passes a test. -
Convert to
setConverts an iterable to a set, removing duplicate values. -
Randomly a list
shuffleRandomly shuffles the items in a list. -
Slice a list into sub-lists
sliceSlices a list into a specified number of sub-lists of roughly equal size. -
Sort a list
sortSorts the items in a list, with optional attribute-based and reverse sorting. -
Sum values in a list
sumSums all values in a list, with optional attribute extraction and start value. -
Set symmetric difference
symmetric_differenceReturns items in either list but not in both. -
Serialize to JSON
to_jsonSerializes a value to a JSON string. -
Convert to
tupleConverts an iterable to a tuple. -
Set
unionReturns all unique items from both lists (set union). -
Remove duplicate values
uniqueRemoves duplicate values from a list, keeping only unique items.