If condition on field value inside script

Hello,

I’m trying to write a simple script that sends a message to my phone, the message being chosen in function of a field value.

Here is my script template:

> sequence:
>   - if:
>       - condition: template
>         value_template: "{{ trigger }} == 'test' "
>     then:
>       - action: notify.mobile_app_sm
>         metadata: {}
>         data:
>           message: "{{ trigger }} ok"
>     else:
>       - action: notify.mobile_app_sm
>         metadata: {}
>         data:
>           message: "{{ trigger }} not ok"
> fields:
>   trigger:
>     selector:
>       text:
>         multiline: false
>         multiple: false
>     name: Trigger
>     required: true

The problem is that I can’t make the if condition work, when I enter “test” on my field, I always receive the “test not ok” message…

How can I test the field value on this script?

Thanks in advance for the help,

Best regards,
Marc

Everything outside the delimiters is a string… so when you pass your input to the script (let’s say you pass “test”)… what is rendered as the value for value_template is the string "test == 'test' ". That string is neither a true boolean expression nor the template rendering the string True, which is the requirement for the condition to pass.

Your comparison operator and string need to be inside the delimiters for it to be a boolean expression.

value_template: "{{ trigger == 'test' }}"

FWIW, I would avoid using “trigger” as your variable name since that is already used internally in automations and trigger-based template sensors.

Hello Didgeridrew,

Thank you for the reply.

I manage to try the solution last night, it works !!