On my computer the problem is that $min_fanval was not set. Up above it only gets set if $fan is set, meaning that fan speed sensor output is used. I believe "min_fanval=100000" needs to be moved above "if [[ -n ${fan} ]]", so it gets set regardless of that condition.