A weird bug: fan control gets enabled only a few seconds after it's been requested for the first time

[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv
Fan control is not supported
[birdie@localhost ~]$ fannv

  Attribute 'GPUFanControlState' (localhost.localdomain:0[gpu:0]) assigned value 1.

  Attribute 'GPUTargetFanSpeed' (localhost.localdomain:0[fan:0]) assigned value 25.

Between the first and the last fannv invocations 10 to 15 seconds pass.

Here’s the script itself:

#! /bin/bash

speed=25
test -n "$1" && speed="$1"

# Set fan speed to 25% or what's given as an argument
result=`nvidia-settings -a "[gpu:0]/GPUFanControlState=1" | grep "assigned value 1"`
if [ -n "$result" ]; then
        nvidia-settings -a "[gpu:0]/GPUFanControlState=1" -a "[fan:0]/GPUTargetFanSpeed=$speed"
else
        echo "Fan control is not supported"
fi

GTX 1060 with NVIDIA drivers 381.22.

Edit: it turns out sometimes the output of nvidia-settings is split between two lines, so now I check for “value 1” instead of “assigned value 1” and everything works fine.