Regex help with IMAP email parsing

I have this email, that has this exact data I am trying to pull into sensors for energy use and cost:

Latest Meter Reading: Saturday, March 1, 2025, 5:00 AM



Yesterday

Energy usage: 43 kWh

Estimated cost: $7



Last 2 Days

Energy usage: 97 kWh

Estimated cost: $15

My code (which is basically the same code example in the IMAP docs) seems to work for each sensor in the regex101 tester, but I get Unknown in the sensors. The email is showing up in the imap sensor as it should, but the individual kWh and Cost sensors are showing up as Unknown:

sensor:
      - name: "Yesterday Energy Use"
        unit_of_measurement: "kWh"
        state: >
          {{ trigger.event.data["text"]
            | regex_findall_index("\*Yesterday\n\nEnergy usage: (\d+) kWh") }}
      - name: "Yesterday Cost"
        unit_of_measurement: "$"
        state: >
          {{ trigger.event.data["text"]
            | regex_findall_index("\*Yesterday\n\nEnergy usage: (\d+) kWh\n\nEstimated cost: \D(\d+)") }}
      - name: "Billing Cycle Energy Use"
        unit_of_measurement: "kWh"
        state: >
          {{ trigger.event.data["text"]
            | regex_findall_index("\*Last \d+ Days\n\nEnergy usage: (\d+) kWh") }}
     - name: "Billing Cycle Cost"
        unit_of_measurement: "$"
        state: >
          {{ trigger.event.data["text"]
            | regex_findall_index("\*Last \d+ Days\n\nEnergy usage: (\d+) kWh\n\nEstimated cost: \D(\d+)") }}

Any ideas on what I have messed up and don’t understand?

If you remove the \* you have at the start of all regexes then it should work.

Tried it…but it still doesn’t populate with the info, just unknown.

Tracked down the error I am seeing now on the sensors…it says:

Error rendering state template for sensor.yesterday_energy_use: IndexError: list index out of range
Error rendering state template for sensor.yesterday_cost: IndexError: list index out of range
Error rendering state template for sensor.billing_cycle_energy_use: IndexError: list index out of range
Error rendering state template for sensor.billing_cycle_cost: IndexError: list index out of range

That’s what’s reported when regex_findall_index fails to find a match. In other words, the regex pattern you created isn’t finding a match in the received string.

Try this version which employs the index parameter of regex_findall_index.

sensor:
      - name: "Yesterday Energy Use"
        unit_of_measurement: "kWh"
        state: >
          {{ trigger.event.data["text"]
            | regex_findall_index("Energy usage: (\d+)", index=0) }}
      - name: "Yesterday Cost"
        unit_of_measurement: "$"
        state: >
          {{ trigger.event.data["text"]
            | regex_findall_index("Estimated cost: \$(\d+)", index=0) }}
      - name: "Billing Cycle Energy Use"
        unit_of_measurement: "kWh"
        state: >
          {{ trigger.event.data["text"]
            | regex_findall_index("Energy usage: (\d+)", index=1) }}
     - name: "Billing Cycle Cost"
        unit_of_measurement: "$"
        state: >
          {{ trigger.event.data["text"]
            | regex_findall_index("Estimated cost: \$(\d+)", index=1) }}

NOTE

Be advised that regex_findall_index is unforgiving. If the match fails by even just one character the result will be an error message (and no way to suppress it without additional checking).