Condition in template

Why is this a ‘number is undefined’ error? How to use condition in template?

alias: My script
sequence:
  - variables:
      number: 5
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: bla bla bla
    enabled: >
      {% if number > 7 %}
        true
      {% else %}
        false
      {% endif %}

I am guessing, but number can be as reserved name. I suggest change name to ‘my_number’.

 {% if my_number > 7 %}
    {{ true }}
  {% else %}
    {{ false }}
  {% endif %}

if you need true/false result you can do this

{{ my_number > 7 }}

Have you put that code in Developer Tools / Template?

If so, that won’t work — only the enabled section is a Jinja template, which is what that tool evaluates. The rest of your code is YAML config.

If you need more help, please give full details of what you’re trying to do.

No

my_number is undefined

Hi ktos799,

Did you actually define it?
Similar to…

variables:
  my_number: 389438
``

Yes

This is good. message = 389438

alias: My script
sequence:
  - variables:
      my_number: 389438
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: "{{my_number}}"
    enabled: "{{2 > 1 }}"

This error:

alias: My script
sequence:
  - variables:
      my_number: 389438
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: "{{my_number}}"
    enabled: "{{my_number > 1 }}"
alias: My script
sequence:
  - variables:
      my_number: 389438
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: "{{ my_number }}"
    enabled: "{{ my_number | float(0) > 1 }}"

error my_number is undefined.

How are you testing this? By running the script, or in developer tools > templates?

I can reproduce this, the enabled parameter on an action doesn’t seem to have access to the variables created in the script, also tested using the variables section on top level, this also doesn’t work.

my_script:
  alias: My script
  variables:
    my_number: 389438
  sequence:
    - action: notify.persistent_notification
      metadata: {}
      data:
        message: "{{ my_number }}"
      enabled: "{{ my_number | float(0) > 1 }}"

You might want to create a issue on this.

I think the same applies to the for field. (see this issue)