Description
Finally got my NAS box buttoned up and noticed that the fan comes on for about 30 second and then off for a minute endlessly.
I think your algorithm is a bit too simple. You need more like a PID controller (at least a PI).
https://en.wikipedia.org/wiki/PID_controller. Right now you just have the P part. Without at least integral control you get the kind of behavior I am experiencing at the low end.
It might be too that the fan should stay on just a tad continuously at 39 to avoid this cycling on and off. Or possibly the initial on temp (>39) should be set higher the the off temp (39)
To this end I suggest you make it easier to change the algorithm outside the code (separate function in it's own file). That way it's easier for others to write/modify their own algorithm.
I see there are some basic PID codes out there even for LUA. Anyway you should check into this as a simple proportional controller rarely works well in the real world.