Find all regex matches: regex_findall

The regex_findall template filter finds all occurrences of a regular expression (regex) pattern in a string and returns them as a list. A regular expression is a special text pattern that lets you describe what you are looking for. While regex_search just tells you whether a pattern exists, regex_findall collects every match and gives them all back to you.

This is useful when you need to extract multiple pieces of data from a text value. For example, you might want to pull all numbers out of a sensor’s state string, extract all IP addresses from a log message, or collect all entity IDs mentioned in a text attribute. The result is a list, so you can count the matches, loop through them, or pick a specific one by index.

Usage

Here’s how to use this template function. Copy any example and adjust it to your setup.

As a filter
{{ "Living room 23.5C, Bedroom 19.8C" | regex_findall("\\d+\\.\\d+") }}
Result (listAn ordered collection of values, like a list of entity IDs or a list of numbers. Written with square brackets in templates, for example [1, 2, 3].)
['23.5', '19.8']

Function signature

The signature is a technical summary of this template function. It shows the name of the function, the values (called parameters) it accepts, and what type of data each parameter expects (for example, a piece of text or a number).

Function parameters that have a = with a value after them are optional. If you leave them out, the default value shown is used automatically. Function parameters without a default are required.

regex_findall(
    value: str,
    find: str = "",
    ignorecase: bool = False,
) -> list[str]

Function parameters

The following parameters can be provided to this function.

value string Required

The string to search within for all matches of the regex pattern.

find string Required

The regular expression pattern to search for. All non-overlapping matches are returned.

ignorecase boolean (Optional, default: false)

Set to true to make the search case-insensitive.

Counting matches

Since the result is a list, you can use the length filter to count how many matches were found.

TemplateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more]: Count the number of errors
{{
  "error at 10:15, error at 11:30, error at 12:45"
  | regex_findall("error") | length
}}
Result (integerA whole number without decimal places, like 1, 42, or -5. Used for counts, indices, and whole values.)
3

Good to know

  • Returns an empty list when nothing matches, not None or an error.
  • Capturing groups change the output: with one group, you get strings; with multiple groups, you get tuples.
  • A literal dot in the pattern must be escaped as \., since . matches any character in regex.

Try it yourself

Ready to test this? Open Developer tools > Template, paste the example into the Template editor, and watch the result update on the right. Edit the values to see how the function adapts to your own entitiesAn entity represents a sensor, actor, or function in Home Assistant. Entities are used to monitor physical properties or to control other entities. An entity is usually part of a device or a service. [Learn more].

More examples

Real scenarios where this function comes up in automations and templates. Copy any example and adapt it to your setup.

Extract all numbers from a sensor state

Pull every number out of a text-based sensor value for processing.

TemplateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more]
{% set values = "Power: 150W, Voltage: 230V, Current: 0.65A"
   | regex_findall("\\d+\\.?\\d*") %}
{{ values }}
Result (listAn ordered collection of values, like a list of entity IDs or a list of numbers. Written with square brackets in templates, for example [1, 2, 3].)
['150', '230', '0.65']

List all matching words

Find all words in a string that start with a capital letter.

TemplateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more]
{{ "The Living Room Light is On" | regex_findall("[A-Z][a-z]+") }}
Result (listAn ordered collection of values, like a list of entity IDs or a list of numbers. Written with square brackets in templates, for example [1, 2, 3].)
['The', 'Living', 'Room', 'Light', 'On']

Loop through extracted values

Extract all temperature values from a multi-sensor string and display them.

TemplateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more]
{% set temps = "Kitchen 21C, Hall 19C, Office 22C"
   | regex_findall("(\\w+) (\\d+)C") %}
{% for room, temp in temps %}
  {{ room }}: {{ temp }}C
{% endfor %}
Result (stringA piece of text, like a name, message, or entity ID. In templates, wrap strings in quotes, like "living_room" or "lights are on".)
Kitchen: 21C
Hall: 19C
Office: 22C

Still stuck?

The Home Assistant community is quick to help: join Discord for real-time chat, post on the community forum with your template and expected result, or share on our subreddit /r/homeassistant.

Tip

AI assistants like ChatGPT or Claude can also explain or fix templates when you describe what you want in plain language.

Related template functions

These functions work well alongside this one: