How does a script field default work?

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?

Hi phil_xyz,

If I were to guess, you are setting the minimum to 1, then defaulting to 0. maybe the problem. I have used default in a field successfully so not sure the issue here.

i believe the default in a script field is just for the ui. ie, when you invoke your script via the ui, and the ui presents the field, what should it show in the ui as the default.

the default is not what the field attains if you omit it from the call.

Sir Goodenough. Doh. Setting default 0 with min 1 was pretty dumb but unfortunately wasn’t the problem.

armedad, yes, that makes sense - i checked and the default does populate the ui for the calling script. Thanks.