Template sensor regex 2nd group match

Hi Try to extract the 2 times from an email subject using the IMAP integration.
Typical subject line is:
Power-ups: Opt in to Power up for free at 2:00 PM - 4:00 PM, 26/09/23

First sensor returns “2:00 PM”
2nd unavailiable
Regex looks good in Regex101 - stumped if anyone can offer assistance.
Tried with and without find= & ignorecase=false in the parenthesis.

> - sensor:
>       - name: OctoPowerUpStart
>         state: >
>             "{{ state_attr( 'sensor.octopuspowerup','Subject') 
>             | regex_findall_index("\d+:\d+.[A-Z][A-Z]")}}"
>  - sensor:
>       - name: OctoPowerUpEnd
>         state: >
>             "{{ state_attr( 'sensor.octopuspowerup','Subject') 
>             | regex_findall_index(find="/\d+:\d+.[A-Z][A-Z].*?(\d+:\d+.[A-Z][A-Z])/", ignorecase=false)}}"

Try this version:

  - sensor:
      - name: OctoPowerUpStart
        state: >
          {% set t = state_attr('sensor.octopuspowerup', 'Subject') 
            | regex_findall('(\d+:\d+ [A|P]M)') %}
          {{ t[0] }}

      - name: OctoPowerUpEnd
        state: >
          {% set t = state_attr('sensor.octopuspowerup', 'Subject') 
            | regex_findall('(\d+:\d+ [A|P]M)') %}
          {{ t[1] }}
1 Like

Many thanks & illuminating!

1 Like

Hello

Nice thinking!

Did you develop this further to include date capture? I’d be very interested to see other developments you did with this idea (I assume you charge batteries etc)

Hi PianSom,

Yes have been developing this for a few weeks now. Having a successful test this very afternoon for a ‘PowerUp’ event. The basic use case in my energy supplier encourages use of excess renewables by make electricity zero cost through a rebate.

The sensors used are in the package available on github : akendall1966/Octopowerups: Custom components and automations to take advantage of the free electricity period from Octopus energy (github.com)

I’m quite the novice so plenty of room for improvement in the code!

I don’t have a battery, but have a diverter for water heating, EV, and some electrical space heating I can boost at these times.