How can I safely use traps in my settlements?

The way I set up traps so that they are only activated during an attack is to use a Siren.

The Siren wont allow power to pass through it unless it is turned on. Conveniently, in the event of enemies attacking, it is turned on automatically, and allows power to pass through it.

Simply route all your power from your generators through a Siren, then to the traps. The traps wont activate until there is an attack.

I like to combine it with a gate that automatically closes during an attack (again using a Siren and Logic Gates) so that enemies are outside with the traps and my settlers are inside.

Automatically closing gate instructions. This requires the Contraptions DLC:

Attach a power line to one input of a NAND gate, and a line from a Siren attached to a power source on the other. Attach the NAND gate's output to the node on the gate.

The gate will be powered and open until an attack happens, when the Siren is turned on allowing power through. This causes the NAND gate to turn off, stopping power to the gate and causing it to close. When the attack ends, the siren turns off, stopping the power from it. This turns the NAND gate back on, opening the gate.