A pressure sensor solves any problems with (some limited amount of) foam, but it still needs to be immune to solids. So some kind of diaphragm open to the environment i.e. no small openings between the sensor outer shell and the diaphragm.
I had a friend talk to me about this. The project was to minimize overall long-term cost of pumping out a sewage holding tank in a lake community by automating the monitoring of the actual level instead of scheduling regular pump-outs based on time. The cost of a pump-out was fixed and independent of the amount pumped out, so the optimal strategy is to wait until the tank is near full before getting it pumped out. I think he already had an enclosed pressure sensor in service.