MQTT message and regex_search, something is going wrong here

And I can’t figure out what it is.
So I have a MQTT message, and I have this automation that triggers on it. All is well, it triggers, and I get:

trigger:
<fields removed>
  payload_json:
    ocr_text: '[''15-03-2024 12.59.23'', ''PRINE'']'

My automation has the following condition:

condition:
  - "{{ trigger.payload_json['ocr_text'] | regex_search('PRINE') }}"

Which works fine in development tools, it shows as true.
But the automation still fails on this condition, and I’m not sure why. This is the only condition it has, and it shouldn’t fail. I have nothing in the logs.

So I’m a little baffled as to what is causing this.

What is reported in the automation’s trace when it fails? In other words, what does the value of ocr_text look like in the trace?

As an experiment, try this version:

condition:
  - "{{ trigger.payload_json.ocr_text | contains('PRINE') }}"

It basically says this:
Stopped because a condition failed at 15 March 2024 at 12:12:19 (runtime: 0.00 seconds)

If I look in step details and changed variables I see this as per above.

trigger:
  payload_json:
    ocr_text: '[''2024-03-15 12*1141'', ''PRINE'', ''IMOV'']'

I’ll try that variant and report back. Nope still the same message, condition failed.

I wonder if it’s because of the way the condition statement looks like in yaml:

condition:
  - condition: template
    value_template: |-
      condition:
        - "{{ trigger.payload_json.ocr_text | regex_search('PRINE') }}"

I don’t ever recall seeing that format.

Change it to this:

condition:
  - condition: template
    value_template: "{{ trigger.payload_json.ocr_text | regex_search('PRINE') }}"

Let me know if it looks different again in the trace file.

BTW, the impression I had was that the native typing feature was changing the value’s type from string to list. That’s why I suggested to use contains but it now looks like something else may be the culprit.


Post the automation’s trace file (it’s in JSON format).

1 Like

My suspicion too, but it looks like regex_search handles lists:

1 Like

contains handles strings as well. Now that both regex_search and contains have failed to produce the desired result, I think we need to look for clues in the trace file.

1 Like

It works! I added the .ocr_text but I did not see the contains, however I changed the format of the condition as per your suggestion:

condition:
  - condition: template
    value_template: "{{ trigger.payload_json['ocr_text'] | regex_search('PRINE') }}"

And using the initial [‘ocr_text’] and reg_ex method, and it worked.

I have no Idea where that condition came from, maybe I pasted something in there at an earlier stage.

Many many thanks.

1 Like