Using 3PDT Relay To Switch Grid and Inverter Output
If you are considering connecting anything that might send power even accidentally into the mains network you want to be on your best electrical behaviour.  

Generally what are used is two switches or contractors that are mechanically (and or electrically) interlocked so that your and the utility supply cannot ever connect to each other simultaneously.  It prevents many dangers.

Check out the following picture search and see if something looks interesting.  There will be DIY, off the shelf (OTS) and custom (PLC if you like) systems to do what you want.

https://www.google.com/search?q=automatic+mains+backup+relay&amp;tbm=isch