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:
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:
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
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.