Regex in Scripts.yaml "regex_match"

Hello,

forgive my bad English.

I want to use a regex in a script. This returns true or false. If true, a script should be executed.

I have deposited the following script. I had to fight a lot with the excludes but I think they fit now.

  test_4711:
    sequence:
      - condition: and
        conditions:
        - condition: state
          entity_id: device_tracker.googlepixel
          state: 'home'
        - condition: template
          value_template: {{ regex_match(pushtext, ((Tor)(..)(Bayern München)(.+)(\\[)(\\d)(\\])(.)(.+))|((Tor)(..)(.+)(Bayern München )(.+)(\\[)(\\d)(\\])), ignorecase=FALSE)}}
      - service: script.fc_bayern_spielt

But now I still get the following error message and I just can’t get any further.

Testing configuration at /home/homeassistant/.homeassistant
ERROR:homeassistant.util.yaml:while parsing a flow mapping
  in "/home/homeassistant/.homeassistant/scripts.yaml", line 95, column 28
expected ',' or '}', but got '['
  in "/home/homeassistant/.homeassistant/scripts.yaml", line 95, column 85
Failed config
  General Errors: 
    - while parsing a flow mapping
  in "/home/homeassistant/.homeassistant/scripts.yaml", line 95, column 28
expected ',' or '}', but got '['
  in "/home/homeassistant/.homeassistant/scripts.yaml", line 95, column 85

The string I process with the regex is the following:

Tor! Real Madrid - Bayern München 0:[1]

I tried a simpler example and now I get the following error.

Script.yaml:

test_4712:
sequence:
- condition: state
entity_id: device_tracker.googlepixel
state: ‘home’
- condition: template
value_template: “{{ regex_match(pushtext, (Tor), ignorecase=FALSE)}}”
- service: script.fc_bayern_spielt

Log:

Error during template condition: UndefinedError: ‘regex_match’ is undefined

I don’t have a working example but I can point you in the right direction. regex_match is a filter. You are using as a function. Filters only work on objects, so your code should look something like this:

{{ myvalue | regex_match('', '', ignorecase=False) }}

I have no clue how it works like that, just doesn’t make sense.

You need quote marks at either end of the template.

even with those quotes, he’ll get the UndefinedError. I did a few tests in the template editor, only way I could get it to ‘work’ was using it as a filter… Which still doesn’t make sense to me. Usually the regex match returns an object so I would expect it to work as a function, not a filter.

In case it benefits anyone, I was able to get this to work after some playing with it for a (long) while…

Because it is used as a filter, you don’t actually supply the first argument (string) as indicated in the docs because the string is already given by whatever precedes the pipe. In your example above of

{{ myvalue | regex_match('', '', ignorecase=False) }}

This template would return true if you attempted to match ‘myv’ per the below

{{ myvalue | regex_match('myv', ignorecase=False) }}

But would return flase if you attempated to match myx per the below

{{ myvalue | regex_match('myx', ignorecase=False) }}

Hope this helps someone!

4 Likes

Thanks a lot, works like a charm once that part is clarified. Would be good to update the documentation which is quite misleading (I spent a couple of hours trying to figure out how this whole thing works before finding your post) !

Am now using it to parse the output of a REST XML answer (doesn’t propose JSON unfortunately) from openhab 1.