Specific binary ping sensor

Hello,
I have not stable connection to device. Some ping will be timeout. If 30% of pings will be lost binary sensor set Connected, else set Disconnect.

How I want to do it?