Python methods you can use in templates
Templates are built on top of Python, and many standard Python methods are available to you. You call them the same way you would in Python: put a dot after the value, then the method name and parentheses. These methods are not listed in the template functions reference because they come from Python itself, but they solve a lot of common tasks.
This page is a cheat sheet of the ones that come up most often in Home Assistant templates. When in doubt, open the template editor and try it.
String methods
Everything Home Assistant stores as text supports these. Since entity states are always text (until you convert them), these work directly on states() results.
split
Splits a piece of text into a list at each occurrence of a separator.
{{ "light.living_room".split(".") }}
['light', 'living_room']
Useful for breaking entity IDs into their domain and name, splitting comma-separated strings, or pulling words out of a sentence.
replace
Replaces one piece of text with another.
{{ "light.living_room".replace("_", " ") }}
light.living room
lower, upper, title, capitalize
Change the case of a piece of text.
Lower: {{ "Living Room".lower() }}
Upper: {{ "Living Room".upper() }}
Title: {{ "living room".title() }}
Capitalize: {{ "living room".capitalize() }}
Lower: living room
Upper: LIVING ROOM
Title: Living Room
Capitalize: Living room
strip
Removes whitespace from the start and end of a piece of text.
'{{ ' hello world '.strip() }}'
'hello world'
startswith, endswith
Check whether a piece of text begins or ends with another piece of text.
{{ "sensor.outdoor_temperature".startswith("sensor.") }}
{{ "image.jpg".endswith(".jpg") }}
True
True
These are very handy for filtering entity IDs by domain.
find, index, count
find returns the position of a piece of text, or -1 if not found. index does the same but raises an error when not found. count counts how many times it appears.
{{ "sensor.outdoor_temperature".find("outdoor") }}
{{ "hello world hello".count("hello") }}
7
2
join
Joins a list into a single piece of text using a separator.
{{ ', '.join(['apples', 'oranges', 'pears']) }}
apples, oranges, pears
Note the order: you call .join() on the separator, passing the list in. There is also a join filter that reads more naturally: ['apples', 'oranges', 'pears'] | join(', ').
format
Inserts values into a piece of text using placeholders.
{{ "Temperature: {} {}".format(22.5, "°C") }}
Temperature: 22.5 °C
Dictionary methods
Dictionaries show up in entity attributes, JSON responses, and action responses.
items
Iterate over a dictionary’s key-value pairs.
{% for key, value in {"a": 1, "b": 2}.items() %}
{{ key }} = {{ value }}
{% endfor %}
a = 1
b = 2
keys, values
Get only the keys or only the values.
{% set data = {"temp": 22.5, "humidity": 54} %}
Keys: {{ data.keys() | list }}
Values: {{ data.values() | list }}
Keys: ['temp', 'humidity']
Values: [22.5, 54]
get
Fetch a value by key, with a fallback if the key is missing. This is safer than bracket lookup, which errors on missing keys.
{% set data = {"name": "Frenck"} %}
Name: {{ data.get("name", "unknown") }}
Age: {{ data.get("age", "unknown") }}
Name: Frenck
Age: unknown
When a key name conflicts with a dict method
If a dictionary has a key with the same name as a dict method (like values, keys, items, or get), dot notation returns the method, not the value. This commonly happens when parsing API responses.
{% set response = {"status": "ok", "values": [1, 2, 3]} %}
{{ response['values'] }}
[1, 2, 3]
Use bracket notation (response['values']) when a key might collide with a method. It always reaches the dictionary value first.
Datetime methods
When you have a datetime (for example, from now()), you can reach into its parts or format it.
Accessing parts
Hour: {{ now().hour }}
Minute: {{ now().minute }}
Weekday: {{ now().weekday() }}
Day: {{ now().day }}
Hour: 14
Minute: 30
Weekday: 5
Day: 4
weekday() returns Monday as 0 through Sunday as 6. Use isoweekday() if you prefer Monday as 1 through Sunday as 7.
Formatting with strftime
strftime formats a datetime using format codes. It is covered in detail on the Working with dates and times page, but here is the short version:
{{ now().strftime('%A, %B %-d, %Y') }}
Saturday, April 4, 2026
Parsing with strptime
The reverse of strftime. Parses a piece of text into a datetime using a format string. See strptime.
Number methods
Numbers have a few helpful methods.
is_integer
Check whether a floating-point number has no decimal part.
{{ (10.0).is_integer() }}
{{ (10.5).is_integer() }}
True
False
When to use methods vs filters
Both work for many tasks. Pick whichever reads better:
-
Methods (with a dot) come from Python.
"text".upper(),data.get("key"). -
Filters (with a pipe) come from the template engine and Home Assistant.
"text" | upper,data | default("key").
Filters chain more naturally when you have several transformations. Methods can be clearer for a single transformation or when the method returns something unusual. The template functions reference lists all the filters Home Assistant provides.
Next steps
- If you’re looking for a specific transformation, check the template functions reference first.
- For template debugging, see Debugging templates.
- For date and time formatting in particular, read Working with dates and times.