The principal of what you’re doing is wrong. You’re looking at the pin that has the fan sensor connected to it each time the code goes around the void loop cycle. If you happen to be looking at it when the magnet is aligned with the sensor then your counter will be incremented, if the fan is rotating slowly, or the fan stops with the magnet next to the sensor then you’ll get multiple counts, or one count per cycle of the loop (this could be hundreds per second).
The way to do it is to use an interrupt on the pin that your sensor is connected to. When the sensor contacts are made (or broken, depending on your wiring and how you define the interrupt) then you will get one interrupt per revolution. When the ESP detects the interrupt it processes the function you named in your interrupt specification, which will be whet you increment your counter.
As I said before, search for weather station anemometer code. The anemometer works in exactly the same way, with a magnet and a reed switch.