ktos799
(Ktos799)
October 4, 2024, 11:58am
1
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 %}
Fido
(Fido)
October 4, 2024, 3:52pm
2
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 }}
Troon
(Troon)
October 4, 2024, 4:38pm
3
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.
Sir_Goodenough
((SG) WhatAreWeFixing.Today)
October 4, 2024, 11:21pm
5
Hi ktos799 ,
ktos799:
my_number is undefined
Did you actually define it?
Similar to…
variables:
my_number: 389438
``
ktos799
(Ktos799)
October 5, 2024, 8:17am
6
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 }}"
ktos799
(Ktos799)
October 7, 2024, 8:36am
8
error my_number is undefined.
TheFes
(The Fes)
October 7, 2024, 10:22am
9
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 )