What do you expect the function should do?
What it does here, is that it does set the timer every time the measured value is above some threshold.
What you probably should do is add some check to see if the timer has already been set.
Also what do you think will happen when the temperature fluctuates? (which is normal when controlling a cooling installation based on temperature for example
)
If the temp does cross the value, then you should set some flag that you've seen it crossing the threshold and set your timer.
Maybe the plugin called "level" can help you out here.
I've not used that one myself, but I've seen its code a few times.
Does look like it could help je monitor some set level + applying some hysteresis.