Thank you, this has been very helpful indeed.
Just wanted to share my story in the hopes that it helps anyone who has tried this thread's suggestions with limited success:
When this issue first happened to me I came across this message board. The cause made sense to me and the trick of laying phone on its side and lightly tapping worked (sometimes). So I thought, I should just take it to a professional, to clean or replace the power switch, and I'll be fine.
Imagine my surprise when not one, but two independent repair guys tell me it's absolutely not the power button (the first cleaned it, the second tried replacing it). Neither of them could tell me the underlying issue, they thought it could be firmware which made absolutely no sense to me.
Rather than doing a factory reset, I went to a third technician and asked him to dig deeper. Turns out the problem was that two resistors leading to the power switch were blown. He replaced those resistors and now the phone works normally again. I paid £30 but it was well worth it.