Problem with Regular Expressions and Templating


I have values like this:
AA B1 03 04C4 0186 30F2 28190908190909090908190819090908190908190819090819 55

And I need only the longest word in this “sentence”, so I tried Regular Expressions:
‘value_template: "{{value_json.RfReceived.Data | regex_findall_index(’\S{20,}‘)}}"’

But I get an error (HA does not understand the escaped S)

I tried the solution for this related problem in the template editor:

But HA says unknown error while parsing… even if I copy the suggested solution char by char

I then read the jinja docs but no other solution was obvious to me…

Thanks for your time!



I copied your code and tested it. HA was complaining about some quotes. It looks like you are using some fancy quotes, because when I tested the same code with different quotes, it worked.

value_template: “{{ value_json.RfReceived.Data | regex_findall_index(‘\S{20,}’) }}”

If this doesn’t work, try to make winqle quotes outside and double quotes inside.

I switched the qotes and now it works fine.

Thanks a lot!

PS: It still does not work in the template editor… maybe a bug?

PPS: Oh the fancy quotes on the outside were due to this forum telling me to mark my code with quotes^^

Works fine for me in the template editor, I actually tested it in the template editor.
If you add this to the template editor, does it not work:

{% set test = "AA B1 03 04C4 0186 30F2 28190908190909090908190819090908190908190819090819 55" %}
value_template: "{{ test | regex_findall_index('\S{20,}') }}"

Instead of writing the fancy quotes, just highlight the code you want to format and then press on the </> button at the top, then you will have the code formatted correctly.

Your are right, that works… maybe it was really just the quotes.

Thanks for your time again :slight_smile: