Check for substring in string in automation IF

Can please someone help me with this.
In an automation, I have a variable named sqlanswer filled with a very short json formatted SQL server reply.
The reply might be:
{'result': [{'onlyrfid': 0}]}

Now I need an if-then to check if this variable contains a simple substring, in this case “onlyrfid”. No need to parse any JSON.

For the hell of it I could not find a proper syntax. I was going with:

          - if:
              - condition: template
                value_template: "{{ 'onlyrfid' in sqlanswer }}"
            then:
              - some_great_command:

but that does not work. There certainly is some substring in string syntax. Can you help?

It’s getting weirder and weirder :slight_smile:
This is the string in variable sqlanswer I am testing: {'result': [{'onlyrfid': 0}]}

I found out that the code "{{ 'onlyrfid' in sqlanswer }}" shouldn’t be far off from what I want.

But it results in FALSE, while "{{ 'result' in sqlanswer }}" gets me the wanted TRUE!!!
How is this possible?

          - if:
              - condition: template
                value_template: "{{ 'onlyrfid' in sqlanswer | string}}"
            then:
              - some_great_command:

i am suspecting that your sqlanswer is not actually a string…

1 Like

It is a dict with an “result” as the key and an object as value.
The object contains another dict with “onlyrfid” as key and 0 as value.

2 Likes

You guys where absolutely right, I was not dealing with a simple string. | string conversion did exactly what I needed, but I also see now the power in having much more than just a string already at hand.

Thank you!