Based on PilzAdam's code.
It still misses some functionality such as a persistent memory and a timer, but that is subject to change. The code runs in a sandbox. Speaking long term this will hopefully replace the old controller.