String template functions
These template functions help you format, transform, and shape text. Use them to build friendly messages, clean up sensor values, or prepare text for dashboards and notifications.
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.
-
Capitalize first character
capitalizeCapitalizes the first character of a string and lowercases the rest. -
Center text in a field
centerCenters a string in a field of a given width, padding with spaces. -
Escape HTML characters
escapeEscapes HTML special characters in a string so they display as literal text. -
Format byte size as human-readable
filesizeformatFormats a byte count as a human-readable file size string like 1.2 MB. -
Force-escape HTML characters
forceescapeEscapes HTML special characters, even on strings already marked as safe. -
Printf-style string ting
formatFormats a string using printf-style placeholders like %s, %d, and %f. -
Indent text
indentIndents text by a given number of spaces, with options for the first line and blank lines. -
Join a list into a string
joinJoins a list of values into a single string with a separator between each element. -
Convert to case
lowerConverts all characters in a string to lowercase. -
Convert number to
ordinalConverts a number to its ordinal string representation (1st, 2nd, 3rd, and so on). -
Pretty-print a value
pprintPretty-prints a value in a readable format, useful for debugging templates. -
Replace occurrences in a string
replaceReplaces all occurrences of a substring with another string. -
Mark as HTML
safeMarks a string as safe HTML so it will not be escaped when rendered. -
Convert to slug format
slugifyConverts a string to a slug format with an optional separator character. -
Strip HTML tags
striptagsRemoves all HTML/XML tags from a string, leaving only the text content. -
Convert to case
titleConverts a string to title case, capitalizing the first letter of each word. -
Strip whitespace
trimStrips leading and trailing whitespace (or specified characters) from a string. -
Truncate text to a length
truncateTruncates a string to a given length, appending an ellipsis if it was shortened. -
Convert to case
upperConverts all characters in a string to uppercase. -
URL-encode a value
urlencodeURL-encodes a dictionary of values for use in HTTP requests. -
Convert URLs to HTML links
urlizeConverts plain text URLs into HTML links. -
Count words in text
wordcountCounts the number of words in a string. -
Wrap text at a line width
wordwrapWraps text at a given line width by inserting line breaks. -
Convert dictionary to HTML attributes
xmlattrConverts a dictionary into an XML/HTML attribute string.