Template Sensor with regex_findall_index not working

Hello @123 and team,

I’m trying to extract only numeric value from a binary sensor. I’ve followed this articled and even though Developer Tools > Template confirms proper matching, sensor doesn’t work and causes HA fail on boot.

Any assistance will be highly appreciated.

Thanks


Configuration on sensor.yaml

- platform: template
  name: miles_to_service
  value_template: "{{ state_attr('binary_sensor.330i_condition_based_services', 'oil distance') | regex_findall_index('\d*') }}"
  friendly_name: Miles to Service
  unit_of_measurement: 'mi'
  icon_template: mdi:oil



Try:

| regex_findall_index('\d+')

Edit: also are you using the automation UI?

It may not support regex.

I’ve experienced a similar issue in the past where the single-quotes, delimiting the regex pattern, appeared to cause a problem for Home Assistant (when used in a sensor’s template).

Try this version (it wraps the regex pattern with double-quotes):

- platform: template
  name: miles_to_service
  value_template: >
    {{ state_attr('binary_sensor.330i_condition_based_services', 'oil distance') | regex_findall_index("\d*") }}
  friendly_name: Miles to Service
  unit_of_measurement: 'mi'
  icon_template: mdi:oil
1 Like

Hello @123

Now it works. IDK why HA/yaml is so weird with quotation marks.

Thanks for your assistance

Hello @123, even though configurator doesn’t give an error now HA is reporting invalid config for the sensor.

Only issue i see from template is

  value_template: >
    3803

Any ideas?

Thanks

The single quotes aren’t the problem. The problem is that when the template is enclosed in double quote characters, the escape characters (i.e., the backslashes) are being consumed by the YAML parser, so they’re gone by the time the template is rendered. (Or, in this case, the YAML string parser doesn’t like \d in the middle of a string.) So you need to escape the escape characters.

Also, in this case, as @tom_l suggested, apparently the * “zero or more” character isn’t known, so the + “one or more” character needs to be used instead.

So, putting it all together:

- platform: template
  sensors:
    miles_to_service:
      value_template: "{{ state_attr('binary_sensor.330i_condition_based_services', 'oil distance') | regex_findall_index('\\d+') }}"
      friendly_name: Miles to Service
      unit_of_measurement: mi
      icon_template: mdi:oil

Alternatively, you can just swap the single and double quote characters:

- platform: template
  sensors:
    miles_to_service:
      value_template: '{{ state_attr("binary_sensor.330i_condition_based_services", "oil distance") | regex_findall_index("\d+") }}'
      friendly_name: Miles to Service
      unit_of_measurement: mi
      icon_template: mdi:oil

which works because (apparently) backslash characters inside single quotes are not treated as special escape characters (and so in this case you don’t need to escape them! :smiley:)

EDIT: I had completely looked past the other issues with the config. Thanks to @petro for pointing those out! I’ve updated this post accordingly, especially since it was marked as the solution.

1 Like

Hello,

Still reporting invalid config.

/config/sensor.yaml

- platform: template
  name: miles_to_service
  value_template: "{{ state_attr('binary_sensor.330i_condition_based_services', 'oil distance') | regex_findall_index('\\d+') }}"
  friendly_name: Miles to Service
  unit_of_measurement: mi
  icon_template: mdi:oil

Thanks

If this is a template sensor, you’re missing many key elements. This should work.

- platform: template
  sensors:
    miles_to_service:
      value_template: "{{ state_attr('binary_sensor.330i_condition_based_services', 'oil distance') | regex_findall_index('\\d+') }}"
      friendly_name: Miles to Service
      unit_of_measurement: mi
      icon_template: mdi:oil

Yep, that too! :laughing: Funny how you look past obvious things when focused on the original question.

Yep, that’s why I made the post. I figured you guys were knee deep in poo

1 Like

eh, I don’t really deserve the solution… I put zero effort into it.

Brother, U rock… that works perfectly. Thank you so much.

@123 @pnbruckner , thanks you too guys.

1 Like

You are right. @pnbruckner you got it.