Watch this video from This Old House to learn how to install an automatic shut off valve for a water heater.
1 Turn off the water to the water heater.
2 Drain the water heater using an electric pump and garden hose.
3 Shut off the gas, then disconnect the gas line from the water heater.
4 Cut cold-water pipe leading to heater using a tubing cutter; repeat for hot-water pipe.
5 Disconnect flue from top of heater.
6 Lift heater, slide catch pan underneath, then set heater back down.
7 Reconnect gas line, after applying pipe dope to threads.
8 Screw the flue back onto heater.
9 Reconnect the hot-water pipe by soldering on a repair coupling.
10 Extend the overflow pipe with copper tubing down to catch pan.
11 Solder a copper union onto a threaded adapter; thread the adapter onto the automatic shut-off valve.
12 Solder a second threaded adapter onto the cold-water pipe.
13 Use tubing cutter to cut away enough cold-water pipe to install the shut-off valve.
14 Install shut-off valve to the cold-water pipe.
15 Plug sensor into valve; place float in water pan.
16 Turn the gas and water back on.
17 Test the valve by turning on the heater and manually raising the float valve; the heater should automatically shut off.