2004 Micra 1.4 SE Codes (P0340)

I have a 2004 micra I just bought and I have noticed that when I start it from cold it idles erratic but is fine once it warms up, and also I noticed engine light in the dash which had the error code: P0340.
If I delete the error code once the car is warm it does not seem to come back on.

I looked through the service history and in 2009 it was recommended to change the throttle body and boost sensor for the problem of "erratic idle from cold"

I am wondering is the problem due to the throttle body, camshaft sensor, or the chain being stretched or something different?

I have the engine light on and it has the error code P0340 only.
I took out the Camshaft sensor and it has a tiny hole in the end that goes into the engine, not sure if it suppose to have this?
Also I noticed that once I clear the error and restart the car there is no error until I gas the car, then it comes back.

Should I replace the camshaft sensor?

Also if the car stalls, then it will not restart (the engine turns) unless I wait for 10 seconds, then it will start. is this due to the sensor?

It has erratic throttle from cold start, but if I gas it, it becomes normal.