I am not a mechanic, but our old Ford Escort was doing the identical thing, and it turned out that when the sparks had been replaced a few weeks back, the wiring harness got damaged - I guess it is easy to do. It going wonky while idling was because of the different pattern of vibration while idling.
Luckily for us, the mechanics skipped ordering us a new harness ($400) and were able to hold the thing together with zip-ties.
(Unluckily for us, the car was totalled a few weeks later…)