Run a script until a sensor is not in running status

Hey guys,

im struggling a little bit, or better said i have no idea how i could realize it.

I created a bash script which checks the plex cpu load if it reaches a specified value to spinup my hdd:

 #!/bin/bash
 
 # ######### Settings ##################
 cpu_threshold=10 # Disks spin up if Plex container's CPU load exceeds this value
 # #####################################
 #
 # ######### Script ####################
 while true; do
     plex_cpu_load=$(docker stats --no-stream | grep -i plex | awk '{sub(/%/, "");print $3}')
     if awk 'BEGIN {exit !('$plex_cpu_load' > '$cpu_threshold')}'; then
         echo "Container's CPU load exceeded threshold"
         echo "Spin up disk /dev/sda"
         dd if=/dev/sda bs=4096 count=1 of=/dev/null iflag=direct
     fi
 done

My plan was to execute this via home assistant if [sensor.docker_plex_state] = running .

I added my bash script to /config/scripts/ and added it to shellcommand.yaml

 plex_spinup_disk: /bin/bash /config/scripts/plex_spinup_hdd.sh

and scripts.yaml

 plex_spinup_disk:
   alias: plex_spinup_disk
   sequence:
   - service: shell_command.plex_spinup_disk

and this is my automation:

 description: ""
 trigger:
   - platform: state
     entity_id:
       - sensor.docker_plex_state
     to: running
 condition: []
 action:
   - service: script.plex_spinup_disk
     data: {}
 mode: single

I’m aware that home assistant has a repeat until option but it should only run as long and not be repeated until… thatswhy im confused.

Perhaps someone has a idea.

Greetings

Dany