Stumbling fan if playing video on Plex in a converted quality (Software PWM issue)

If playing a video on Plex with choosing a lower quality the fan starts running slowly and continue in a stumbling mode as long Plex is converting, the conversion mode is indicated by the shaded portion of the movie progress bar.

When playback starts the fan starts spinnig and continues stumbling slowly spinning as long as conversion takes, which is in short movies like this one far before the end of playback.

This strange fan behaviour does not correlate to CPU temps as I trigger on HDD temps instead of CPU temps and to be 100% sure I put in fan.py dc=1.0 before pin.write(dc) which is 0 duty.

As I have the Pi5 with DietPi without UI in ssh terminal mode, so far I do not have other options to investigate deeper.
Linux pi5 6.6.47+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.6.47-1+rpt1 (2024-09-02) aarch64 GNU/Linux

My recent best guess ist that the SD .ts conversion process interferes with fan.py PWM generation (self.thread = threading.Thread(target=self.tr, daemon=True)).

Feels weird, but is clearly reproducable just choosing not the original quality …

Regards
Michael

Disclaimer: This demonstrates only a temporary workaround, waiting for hardware PWM on Pi5 gets ready

The issue is that on expected fanless operation, the fan stumbles and spins slowly when Plex plays a reduced quality video that requires conversion.
You might have noticed as well that sometimes suddenly the fan starts spinnig for a fraction of a second without any reason.

I found a way to workaround it by forcing the software PWM to 0 for the case of zero duty.
For that I modified the tr function in fan.py as

    def tr(self):
        while True:
           if self.value[1] > self.period_s * .9:
                self.line.set_value(0)
                time.sleep(0.1)
           else:
                self.line.set_value(1)
                time.sleep(self.value[0])
                self.line.set_value(0)
                time.sleep(self.value[1])

To proove the solution you could use self.period_s * 1.1 instead of self.period_s * .9 meaning that the fixing if clause will never be executed and the function behaves as in origin.

Regards
Michael

Everything you do - you do so at your own risk