Put the electronics in an IP44 box (like this https://www.aliexpress.com/item/32840552018.html?), making a slit to expose the sensor. then waterproof that slit using hotglue, silicon paste or something like it.
Fully enclosed casings will always get moisture in (especially if you try to mess around with hot glue), and that moisture has no way out. That's a common problem.For permanently powered projects the inverted bucket design (waterproof container with some big openings in the bottom) works wonders. You may need some mesh to close off the openings to keep insects out.