Delay in script is ignored?

Here is a script that is supposed to lower the volume of a dfplayer from 30 to 0 in 200ms steps. It should take six seconds total. But, when the script is executed, the volume goes straight to zero with no delay.

Do I have an error in my script logic?

script:
  - id: fadeout
    then:
      - repeat:
          count: 30
          then:
            - lambda: |-
                static int volume = 30;
                volume -= 1;
                if (volume < 0) volume = 0;
                id(dfplayer1).set_volume(volume);
            - delay: 200ms

The static is only initialized the first time. So a second call of the script volume will already be 0.

Two possible options:
Initiate the volume to 30 on the first loop (iteration == 0). But this would mean vlume always starts at 30.

Of store the volume more general for example in en global or a number component. And use it here.

Using a global fixed it. Works perfectly now.