guys. they have an intermittent short in the power button. never "Smack your phone" Phones are very sensitive devices and a technician worth half their salt will not suggest this. it may Temporarily dislodge the intermittant short, but it will cause issues in the long run.
Simply put, the mechanics of the power button are loose. you should replace this button. my store does it regularily. it is a surface mounted component, that needs to be soldered. the part is below.
Please dont hit your phone. this suggestion hurts my brain
https://www.amazon.ca/Power-Switch-Button-Nexus-Optimus/dp/B017OMM8MY/ref=sr_1_1?ie=UTF8&qid=1512681141&sr=8-1&keywords=lg+nexus+5+power+button