I’m not getting the results I expect when setting a default for a script field. I expected that in the absence of a non-required field the default would be used. However, the following results in the field being undefined:
alias: Test Automation
trigger: []
condition: []
action:
- action: script.test_script
alias: Test Script
sequence:
- action: logbook.log
metadata: {}
data:
name: numcheck
message: >-
{% if nomber is defined %}
{{ nomber }}
{% else %}
{{ 'not defined' }}
{% endif %}
fields:
nomber:
selector:
number:
min: 1
max: 10
name: nomber
default: 0
result: numcheck not defined
I found a solution (Undefined variable with default value?) which is to override the field with a variable that sets a default:
alias: Test Script
sequence:
- action: logbook.log
metadata: {}
data:
name: numcheck
message: >-
{% if nomber is defined %}
{{ nomber }}
{% else %}
{{ 'not defined' }}
{% endif %}
fields:
nomber:
selector:
number:
min: 1
max: 10
name: nomber
default: 0
variables:
nomber: "{{ nomber | default(0) }}"
result: numcheck 0
I don’t understand why the variable is necessary. Looks like my expectation was wrong. But it begs the question what exactly is the field default for?