A method for detecting and localizing a valve leak in a piston machine having a shaft 24, where the method includes: - attaching at least one vibration sensor (16', 16", 16"') to a valve block (14', 14", 14"'), of the piston machine; - measuring the vibrations from all vibration sensors (16', 16", 16'"); - attaching a sensor to the piston machine where the sensor is designed to produce, directly or indirectly, an angular shaft position signal for the shaft;-determining, directly or indirectly, an angular shaft position signal for the shaft; and - transforming the vibration signals from all vibration sensors (16', 16", 16"') into one envelope signal representing the instant vibration level,: - using the angular position signal for constructing window functions that pick the envelope signal in selected angular shaft sectors; - using the window functions to find sector-based averages of the vibration level, and - comparing said averages with a critical ambient vibration level to detect and localize leaks in one or two valves, characterized in that the method further includes: - determining the centre angle of the envelope vibration signal by Fourier analysis; - using said centre angle to construct two complementary window functions representing shaft sectors for respective the highest and the lowest vibration level; - using these window functions and the envelope function to find the average signal level in said sectors; and - setting a leak alarm if the highest vibration level exceeds a critical value; and - determining, if the leak alarm is set, the leaky valve as the valve having its closing sector nearest to the window sector of highest vibration level. - attaching at least one vibration sensor (16', 16", 16"') to a valve block (14', 14", 14"'), of the piston machine; - measuring the vibrations from all vibration sensors (16', 16", 16'"); - attaching a sensor to the piston machine where the sensor is designed to produce, directly or indirectly, an angular shaft position signal for the shaft;-determining, directly or indirectly, an angular shaft position signal for the shaft; and - transforming the vibration signals from all vibration sensors (16', 16", 16"') into one envelope signal representing the instant vibration level,: - using the angular position signal for constructing window functions that pick the envelope signal in selected angular shaft sectors; - using the window functions to find sector-based averages of the vibration level, and - comparing said averages with a critical ambient vibration level to detect and localize leaks in one or two valves, characterized in that the method further includes: - determining the centre angle of the envelope vibration signal by Fourier analysis; - using said centre angle to construct two complementary window functions representing shaft sectors for respective the highest and the lowest vibration level; - using these window functions and the envelope function to find the average signal level in said sectors; and - setting a leak alarm if the highest vibration level exceeds a critical value; and - determining, if the leak alarm is set, the leaky valve as the valve having its closing sector nearest to the window sector of highest vibration level. - determining the centre angle of the envelope vibration signal by Fourier analysis; - using said centre angle to construct two complementary window functions representing shaft sectors for respective the highest and the lowest vibration level; - using these window functions and the envelope function to find the average signal level in said sectors; and - setting a leak alarm if the highest vibration level exceeds a critical value; and - determining, if the leak alarm is set, the leaky valve as the valve having its closing sector nearest to the window sector of highest vibration level.