In version 0.13 there is an ability to connect roboport to the circuit newrok, and transmit count of free and total robots of each kind (4 outputs).
The basic idea is simple: if there is any damage to our base - construction robot will become busy to repair damaged objects, if count of free construction robots does not equal to count of all construction robots - turn on electricity for turrets.
![Image](https://forums.factorio.com/images/ext/7df779de93a2ca25df6d481ffe445273.jpg)
After attack counter starts count to 1200 (1183 actually), ~10 seconds to turn off again.
This setup is not perfect, I do not like to spend much time with combinators.
Here is blueprint string (with default lamps).