The idea was to close the transistor at times when the voltage is above a certain value. In order to solve this problem, I took a zener diode, which will pass a voltage higher than the permissible value, and a p-channel mosfet to stop charging the capacitor with voltage.
If you have any interesting tidbits for correction, I will be glad to hear them. I built the circuit experimentally, unfortunately, I do not know how to calculate anything more complicated than resistors...