Regex in value template not working for MQTT

Hi all, i have an image detector that sends me a string via MQTT.

The MQTT message is: ‘person (98.99%) | person (96.92%) |’

I then have the following code tested in the “developer tools template” page:


{% set value = 'person (98.99%) | person (96.92%) |' %}

Filtered value is: {{ value|regex_findall_index('\((.*?)\%') }}

Result is:


Filtered value is: 98.99

Now i go then and create a MQTT sensor:

# Front yard camera
- platform: mqtt
  name: "image detector front yard confidence"
  state_topic: "BlueIris/motion_frontyard"
  value_template: {{ value | regex_findall_index('\((.*?)\%') }}

Then i get the following message from the “check configuration option”

  in "/config/sensors/others.yaml", line 12, column 20
expected ',' or '}', but got '?'
  in "/config/sensors/others.yaml", line 12, column 56

line 12 corresponds to the value_template line. I do have other MQTT sensors on the same file working well.

Can anyone help to understand why this works on the test developer page, but not in this sensor? what am i doing wrong?

Enlose the value template in double quotes:

value_template: "{{ value | regex_findall_index('\((.*?)\%') }}"

And if you have a multi-line template, you can also use multi-line notation without quotes:

value_template: >-
    {{ value | regex_findall_index('\((.*?)\%') }}

This one works !! thanks. Can you explain the “>-” and why is needed in this case?

value_template: >-
    {{ value | regex_findall_index('\((.*?)\%') }}

Just adding the quotes doesn’t work for the record

the >- will make the yaml parser interpret every following indented line as a string. This is why no quotes are needed. Its just a way of incorporating multi-line strings into yaml.

The single line with quotes should have worked, its the most used approach, so that’s a bit strange. Do you mind copying and pasting the whole sensor config for the single line template that fails here so we can see why it fails?

Here is the two sensors that i have on MQTT:

the first one works, the second one doesn’t (error details on original post)

# Front yard camera
- platform: mqtt
  name: "image detector front yard result"
  state_topic: "BlueIris/motion_frontyard"
  value_template: '{{ value.split(" ")[0] }}'

- platform: mqtt
  name: "image detector front yard confidence"
  state_topic: "BlueIris/motion_frontyard"
  value_template:  "{{ value | regex_findall_index('\((.*?)\%') }}"

regex_findall_index can be picky about having to use double quotes inside the brackets. Solution is to use singe quotes outside the whole template and double quotes inside or use double quotes inside and a multi-line template.

See: Template Sensor with regex_findall_index not working

Seems like the only reasonable explanation.

Definitely picky

This works on the template tester but NOT in sensor code:

{{ value | regex_findall_index("\((.*?)\%") }}

This is the only combination that works in the sensor code: BUT adds ‘’ to the result

'{{ value | regex_findall_index("\((.*?)\%") }}'

The only combination that works is:

   value_template: >-
       {{ value | regex_findall_index('\((.*?)\%') }}

I would assume this may be caused by the backslashes. When ever you use backslashes in normal quotes, it’s called escaping. So the regex in the single line template is most likely getting the regex ((.*?)% instead of \((.*?)\%. Try adding a second backslash to the template for the inline one to see if it works.

  value_template:  "{{ value | regex_findall_index('\\((.*?)\\%') }}"

As a side note, why are you searching for the ( %? Wouldn’t you just want to find any number?

I have a similiar problem.
Thats my MQTT Message:

{
    "code": 0,
    "msg": "",
    "data": {
        "ipPublic": [
            "100.200.300.400"
        ],
...

I’m trying to get the IP Address between ["…"].

It does return an “Unknown” on my Dashboard.

Can someone help me?

you don’t need regex, just treat it like json

{{ value_json.data.ipPublic[0] }}
1 Like

Thank you. That works.