I got IndexError: list index out of range when testing in templating:
{{ value | regex_findall_index(‘var webdata_now_p = [0-9]+’)}}
Here is the sensor I’m trying to implement:
sensor:
platform: rest
resource: http://192.168.1.81/status.html
method: GET
name: Actual Solar Power Production
device_class: power
value_template: “{{ value | regex_findall_index(‘var webdata_now_p = [0-9]+’) }}”
verify_ssl: false
unit_of_measurement: W
authentication: basic
username: the_username
password: the_password
force_update: true
The regex search is picky about using double quotes. Try it as a multi line template with double quotes around your search expression (and no quotes outside the template).
I concocted a way to avoid getting the error message in the case where regex_findall_index doesn’t find a match. Basically, it first tests if the value contains the desired pattern. If it does then it extracts it. If it doesn’t, it reports zero (or whatever one wants to use to represent ‘nothing found’).
{% set value = 'abc xyz' %}
{% set pattern = '\d+.?\d+' %}
{{ (value | regex_findall_index(pattern)) if value is search(pattern) else 0 }}
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. It helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.