Regex assistance needed for template sensor based on Imap_email_content

Hi,

Hope someone is able to assist me with regex issue. So I have got imap_email_content configured, and I am able to see the email sensor as well as email body under its attribute, all good! What I am trying to do is to get three template sensors created based on the highlighted below.

Relevant part of email message look like:

Dear Customers,
The daily operation of your power station as follows:
User ID:
xxxxxxxx
Daily generation:
26.8kWh
Month generation:
215.10000000000002kWh
Total generation:
668.6999999999999kWh
Daily income:
0.0
Total income:
0.0
CO2 reduction:
666.69kg
Number of alarms:
0
Date:
2022/01/08

The following code is what I have been using, unfortunately not working.
template:

  • sensor:
    • name: “Daily Generation”
      unit_of_measurement: “kWh”
      state: >
      {{ state_attr(‘sensor.solar_power_email’,‘body’)
      | regex_findall_index("\*Daily generation:\n([0-9]+)\.([0-9]+)kWh") }}
    • name: “Monthly Solax Generation”
      unit_of_measurement: “kWh”
      state: >
      {{ state_attr(‘sensor.solar_power_email’,‘body’)
      | regex_findall_index("\*Month generation:\n([0-9]+)\.([0-9]+)kWh") }}
    • name: “Total Solax Generation”
      unit_of_measurement: “kWh”
      state: >
      {{ state_attr(‘sensor.solar_power_email’,‘body’)
      | regex_findall_index("\*Total generation:\n([0-9]+)\.([0-9]+)kWh") }}

I suggest you use regex101.com to help compose an appropriate regex pattern.

For example, try this version:

template:
  - sensor:
      - name: 'Daily Generation'
        unit_of_measurement: 'kWh'
        state: >
          {{ state_attr('sensor.solar_power_email', 'body')
            | regex_findall_index("^Daily generation:\n(\d+.\d+)kWh") }}
      - name: 'Monthly Solax Generation'
        unit_of_measurement: 'kWh'
        state: >
          {{ state_attr('sensor.solar_power_email', 'body')
            | regex_findall_index("^Month generation:\n(\d+.\d+)kWh") }}
      - name: 'Total Solax Generation'
        unit_of_measurement: 'kWh'
        state: >
          {{ state_attr('sensor.solar_power_email', 'body')
            | regex_findall_index("^Total generation:\n(\d+.\d+)kWh") }}

NOTE

If it fails to work then it means regex_findall_index only searches a single line, not multiple lines.

Unfortunately not working. :frowning: Still showing unavailable for the template sensor.

Then it implies that regex_findall_index does not support searching through more than a single line of text at a time. (Or there is some other error that prevented it from working.)

You may have noticed in regex101 that each regex pattern ends with /gm. The m means to search through multiple lines. As far as I know, these options, which determine how the search should be performed, aren’t accessible to users of regex_findall_index and your results suggest that it is not using the m option.