Regex Template Help

Hi everyone. I‘m trying to get data from a sensor and set it by using regex.

- platform: template
     sensors:
     smartmeter_total:
         unit_of_measurement: "kWh"
         value_template: >
           {% if states('sensor.smartmeter_data')|regex_search("1-0:1\.8\.0\*255") %}
             {{ states('sensor.smartmeter_data')|regex_findall_index("\((\d*\.\d*)\)") | float }}
           {% else %}<sup>Text</sup>
             {{ states('sensor.smartmeter_total') | float }}
           {% endif %}

The if statement works fine. My issue is in the line of regex_findall_index. It‘s not returning the value I expect. This is the raw output of the data: 1-0:1.8.0*255(00507.4541*kWh)
I want 507.4541 to be written as the sensor state.

Can someone help me with the expression?

Thanks

Not sure if you’re still trying to figure this out, but I came across this while trying to figure out how to do a regex inside a template and figured I would drop an answer.

Since there would be no matches where there are digits followed by a ) you would remove that escaped \) and just use \((\d*\.\d*). This worked for me in the template tester:

{% set str = "1-0:1.8.0*255(00507.4541*kWh)" %}

{{ str|regex_findall_index("\((\d*\.\d*)") | float}}

---> 507.4541

That’s no solution! You want to get digits and search for ‘(’ as ‘helper’ or startpoint ?

Normally that finds
00507.4541 and only because of the filter float you get the ‘right’ result. That’s no solution per se. (how many filters you wanna use in a more complicated scenario ?)

Use this instead:

{{ str|regex_findall_index("[\d]{3}\.[\d]{4}")}}

That finds a sequence which includes 3 digits + point + 4 digits