Email regex doesn't match

I’m trying to parse some values from an email. The regex works in the template editor and at regexr.com, but in the template, it doesn’t match:

homeassistant  | 2026-09-22 17:56:34.408 ERROR (MainThread) [homeassistant.helpers.sensor] Error rendering state template for sensor.heizung_einheiten: IndexError: list index out of range
homeassistant  | 2026-09-22 17:56:34.409 ERROR (MainThread) [homeassistant.helpers.sensor] Error rendering state template for sensor.heizung_kwh: IndexError: list index out of range

The error means that regex_findall_index() wants to access the index 0, but the list is empty. I.e. there was no match.

At the template, I tried to stay close to IMAP - Home Assistant. The template looks like:

template:
  - trigger:
      - trigger: event
        event_type: "imap_content"
        id: "custom_event"
        event_data:
          sender: "[email protected]"
    sensor:
      - name: "Heizung [Einheiten]"
        unit_of_measurement: "Units"
        state: >
          {{ trigger.event.data["text"]
            | regex_findall_index("(\d+(?:,\d+)?) Einheiten  \(")
            | replace(",", ".") | float }}
      - name: "Heizung [kWh]"
        unit_of_measurement: "kWh"
        state: >
          {{ trigger.event.data["text"]
            | regex_findall_index("Einheiten  \((\d+(?:,\d+)?) kWh")
            | replace(",", ".") | float }}

The relevant content of trigger.event.data["text"] is:

<span style="font-size: 20px; font-weight:700;"> 0 Einheiten &nbsp;(0 kWh**) </span> 

I tried also:

  • Replacing &nbsp; with \s+ (in case the HTML gets rendered somewhere) → still failing
  • Escaping \ like \\

Do you have an idea how the regex should look like?

Have you tried using the striptags filter to strip out the html tags…

template:
  - trigger:
      - trigger: event
        event_type: "imap_content"
        id: "custom_event"
        event_data:
          sender: "[email protected]"
    variables:
      stripped: "{{ trigger.event.data["text"]| striptags }}"
    sensor:
      - name: "Heizung [Einheiten]"
        unit_of_measurement: "Units"
        state: >
          {{ stripped | regex_findall_index("(\d+(?:,\d+)?)\s+Einheiten\s+\(") | replace(",", ".") | float(0) }}
      - name: "Heizung [kWh]"
        unit_of_measurement: "kWh"
        state: >
          {{ stripped | regex_findall_index("Einheiten\s+\((\d+(?:,\d+)?) kWh") | replace(",", ".") | float(0) }}

Thanks for the hint! That didn’t resolve my issue, but it makes the mail content much better readable.

Unfortunately, I couldn’t find the error cause. Instead, using an automation with imap.fetch worked. It seems to convert the HTML &nbsp; to \_ and contains line endings, but I was able to handle this.

This is how the final snippet looks like (a bit strangely formatted, because I edited it via GUI):

actions:
  - action: imap.fetch
    metadata: {}
    data:
      entry: ABC
      uid: '{{ trigger.event.data["uid"] }}'
    response_variable: mail_html
  - variables:
      mail_html_stripped: "{{ mail_html['text'] | striptags | replace('\r\n', '')
        }}"
      heizung_einheiten: "{{ mail_html_stripped\n | regex_findall_index(\"(\\d+(?:,\\d+)?)
        Einheiten.*?\\(\")\n | replace(\",\", \".\") | float }}"
      heizung_kwh: "{{ mail_html_stripped\n | regex_findall_index(\"Einheiten.*?\\((\\d+(?:,\\d+)?)
        kWh\")\n | replace(\",\", \".\") | float }}"