I tried this script and I noticed that doesn't detect properly when the drive is being used and every time I try to run it the drives are spinning down and up.
I use unraid 6.12.6.
After further investigation I changed
stat_new=$(grep "${device}1 " /proc/diskstats | tr -dc "[:digit:]")
to
stat_new=$(grep "${device} " /proc/diskstats | tr -dc "[:digit:]")
In this way it search for ativity on the drive not partition and like this is working properly.