Field not registering any value despite default

Hello,

I am running into a problem with my script where for some reason my field (Wait for Test) says it is not defined. Despite having a default value. I am not sure what the problem is as other fields in other projects work as normal. Thank you for your attention

alias: Cover Time Test
sequence:
  - action: logbook.log
    data:
      name: Cover Debug
      message: "wait_for_test value is: {{ wait_for_test }}"
      entity_id: sensor.logger
  - action: counter.reset
    metadata: {}
    data: {}
    target:
      entity_id:
        - counter.test_helper_warnings
        - counter.test_helper_failure
        - counter.test_helper_success
  - variables:
      entities: >
        {{ states.cover | map(attribute='entity_id') | select('string') | list
        }}
      full_time: 0
  - action: counter.set_value
    metadata: {}
    data:
      value: "{{ entities | length }}"
    target:
      entity_id: counter.test_helper_total_number_of_devices
  - repeat:
      count: "{{ entities | length }}"
      sequence:
        - variables:
            current_entity_id: "{{ entities[repeat.index - 1] }}"
        - if:
            - condition: template
              value_template: |
                {{ state_attr(current_entity_id, 'current_position') != None }}
          then:
            - action: logbook.log
              data:
                name: Cover Debug
                message: >
                  Testing {{ current_entity_id }} with position: {{
                  state_attr(current_entity_id, 'current_position') }}.
                entity_id: sensor.logger
          else:
            - action: logbook.log
              data:
                name: Cover Warning
                message: >
                  {{ current_entity_id }} is missing the 'current_position'
                  attribute.
                entity_id: sensor.logger
            - action: counter.increment
              metadata: {}
              data: {}
              target:
                entity_id: counter.test_helper_warnings
        - target:
            entity_id: "{{ current_entity_id }}"
          action: cover.open_cover
        - delay:
            seconds: 1
  - delay:
      minutes: "{{ wait_for_test}}"
    enabled: false
  - action: logbook.log
    metadata: {}
    data:
      name: Cover Debug
      entity_id: sensor.logger
      message: Finished Waiting
  - variables:
      start_time: "{{ now().timestamp() }}"
  - repeat:
      count: "{{ entities | length }}"
      sequence:
        - variables:
            current_entity_id: "{{ entities[repeat.index - 1] }}"
        - action: logbook.log
          metadata: {}
          data:
            name: Cover Debug
            entity_id: sensor.logger
            message: "Closing {{ current_entity_id }} "
        - target:
            entity_id: "{{ current_entity_id }}"
          action: cover.close_cover
        - delay:
            seconds: 1
  - action: logbook.log
    data:
      name: Cover Debug
      message: "wait_for_test value is: {{ wait_for_test }}"
      entity_id: sensor.logger
  - repeat:
      count: "{{ entities | length }}"
      sequence:
        - action: logbook.log
          metadata: {}
          data:
            name: Cover Debug
            entity_id: sensor.logger
            message: Inside the finishing loop
        - variables:
            current_entity_id: "{{ entities[repeat.index - 1] }}"
        - action: logbook.log
          data:
            name: Cover Debug
            message: "wait_for_test value is: {{ wait_for_test }}"
            entity_id: sensor.logger
        - variables:
            timeout_duration: >
              "00:{{ '0' if wait_for_test < 10 else '' }}{{ wait_for_test
              }}:00" 
        - action: logbook.log
          data:
            name: Cover Debug
            message: "timeout_duration value is: {{ timeout_duration }}"
            entity_id: sensor.logger
        - wait_template: "{{ state(current_entity_id) == 'closed' }}"
          continue_on_timeout: true
          timeout: "{{ timeout_duration }}"
        - action: logbook.log
          data:
            name: Cover Debug
            message: "state value is: {{ state(current_entity_id) }}"
            entity_id: sensor.logger
        - variables:
            time_to_test: >
              {{ max(0, (states[entity_id].last_changed -
              start_time).total_seconds()) }}
        - action: logbook.log
          metadata: {}
          data:
            name: Cover Debug
            entity_id: sensor.logger
            message: "Time to Test {{time_to_test}} "
        - if:
            - condition: template
              value_template: "{{ time_to_test == 0 }}"
          then:
            - action: logbook.log
              metadata: {}
              data:
                name: Warning Debug
                entity_id: sensor.logger
                message: >-
                  {{current_entity_id}} has experienced a problem with closing
                  (Maybe you didnt give it enough time?) 
          else:
            - action: logbook.log
              metadata: {}
              data:
                name: Cover Debug
                entity_id: sensor.logger
                message: "current entity id {{current_entity_id }} "
            - if:
                - condition: template
                  value_template: |
                    {{ time_to_test is defined and 
                       118 <= time_to_test <= 122 }}
              then:
                - action: logbook.log
                  data:
                    name: Cover Warning
                    message: >
                      {{ current_entity_id }} seems to have a default value of 2
                      minutes.
                    entity_id: sensor.logger
                - action: counter.increment
                  target:
                    entity_id: counter.test_helper_warnings
                  data: {}
              else:
                - action: counter.set_value
                  metadata: {}
                  data:
                    value: >-
                      {{ counter.test_helper_code_counter + (time_to_test | int)
                      }}
                  target:
                    entity_id: counter.test_helper_code_counter
  - repeat:
      count: "{{ entities | length }}"
      sequence:
        - action: logbook.log
          metadata: {}
          data:
            name: Cover Debug
            entity_id: sensor.logger
            message: "Testing the times to see an outlier "
        - variables:
            current_entity_id: "{{ entities[repeat.index - 1] }}"
        - if:
            - condition: template
              value_template: |
                {{ time_to_test is defined and
                   ((states('counter.test_helper_code_counter') | int) / entities | length) -
                   acceptable_margin <= time_to_test <=
                   ((states('counter.test_helper_code_counter') | int) / entities | length) +
                   acceptable_margin }}
          then:
            - action: logbook.log
              data:
                name: Cover Debug
                message: >
                  {{ current_entity_id }} has a normal test time of {{
                  time_to_test }} seconds.
          else:
            - action: logbook.log
              data:
                name: Cover Warning
                message: >
                  {{ current_entity_id }} has an abnormal test time of {{
                  time_to_test }} seconds.
            - action: counter.increment
              target:
                entity_id: counter.test_helper_warnings
              data: {}
description: >-
  Tests if the time for a cover to open/close is within an acceptable range,
  logs warnings for missing attributes or abnormal values.
fields:
  acceptable_margin:
    selector:
      number:
        min: 1
        max: 120
    name: Acceptable Margin
    description: How many seconds can the value deviate from the average
    default: 30
    required: true
  wait_for_test:
    selector:
      number:
        min: 1
        max: 30
    name: Wait for Test
    description: >-
      How many minutes do you want bettewen steps of testing (should be the
      value of the largest blind closing time)
    default: 5
    required: true

Hello Rodrigo_Baiao,

This is where the actual error message would be Sooo helpful…

My guess it it’s happening in the 4th repeat (repeat[3]) because in there you are using the variables pulled from the fields out of scope. They are in an inner repeat loop and have no value there.
That’s my guess anyway…

Sorry, I didnt include the error message because it was simply that I couldnt execute the script due to Unknown Error. I do know it is for the Wait for Test Variable if that helps, I realized I forgot to include that in the original message

OK, but you might have missed the point. It’s a scope problem. The variable in the inner repeat is different scope than the one in the field, so they are not the same.

You may have to use namespace, or create a helper or another way to create a global in order to refer to that in the inner repeat loop.

Trigger based template sensor to store global variables.

You defined it in the fields, but that doesn’t mean it’s supplied when you don’t populate the variable.

The default here

is only for the UI, it does not actually populate wait_for_test with a value. If you want to do that, you need to use wait_for_test | default(5) in your template code

I was not aware of that, I thought default would set it as a default value and not just an UI thing. Thank you for the help