The sensor has to be immediately downstream of the element in order to avoid overheating the wort. The PID cannot control the wort temperature unless the control-feedback loop is very small. You want the sensor as close as possible downstream of the element for best performance.
I also recommend that you include additional thermometers in your wort circuit so that you can monitor temperatures at the top and bottom of your grain bed. That gives you the ability to know when your mash has reached equilibrium after a mash step.