Script argument in while loop

Hi, I would like to run the following script in automation with a parameter pointing to the appropriate player entity:

alias: Radio volume fade-in
  - repeat:
        - service: media_player.volume_set
            entity_id: "{{ my_player }}"
            volume_level: "{{ state_attr('my_player', 'volume_level') + 0.01 }}"
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 300
        - condition: numeric_state
          entity_id: "{{ my_player }}" # <- this is wrong
          attribute: volume_level
          below: 0.2
mode: single

While I can set a variable “{{ my_player }}” in the “sequence” section, it causes the following error in the “while” section:

Message malformed: Entity {{ my_player }} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘sequence’][0][‘repeat’][‘while’][0][‘entity_id’]

How should I make it work?

You have to make a template condition, not a numeric state condition.

- condition: template
  value_template: "{{ state_attr(my_player, 'volume_level') < 0.2 }}"

you should be able to shorten it to this as well

      while: "{{ state_attr(my_player, 'volume_level') < 0.2 }}"

this is great. Thank you. And what if I would use target volume level as variable (as script argument) too?
It should be like this?

while: "{{ state_attr(my_player, 'volume_level') < target_vol }}"

Shoud I define target_vol somehow?

You don’t need to define it. But it helps to add a description for it.

Thank you for your help.