Default an appliance to off (no power) when on generator power and on when on utility power
What you need is a critical-loads subpanel
This is an ordinary subpanel, in which the critical-load circuits are permanently moved.  (this isn't as annoying as it sounds).  The ATS automatically switches its source between utility and generator.
As Ed Beal notes, it's unusual to put a large load like hot water on a backup generator for a couple of reasons: First, tanked electric water heaters are well-insulated, and will hold heat for 12-24 hours. So it's simply not a problem except for long outages. Second, they are one of four enormous loads in your house, the others being electric dryer, electric range and A/C.  Because of their large size, they are generally excluded from critical loads, because they require such a large generator, and wipes out any ability to use a portable generator.
Generally an ATS (automatic transfer switch) implies permanently installed generator, since it defeats the purpose if the ATS can't auto-start the generator, and that's not generally supported with portables.
Pad generators can easily be sized to pick up any planned load, and when an owner is committed to supporting some or all of these large loads, that's what they do.  NEC requires a load calculation be done to assure the generator is large enough.
You may be looking for a load shed device
However, NEC has an exception - when doing your load calculation, you can exclude loads which are on automatic &quot;load shed&quot; devices.
A load-shed device is an interruptor, which monitors the input power, looking for sagging voltage, slowing AC frequency or other signs of the generator bogging down.  They then drop out their connected load.
What they do not make, unfortunately, is a smart &quot;load balancer&quot; that draws off only the excess capacity of the generator; it would have to &quot;dim&quot; the water heater, but that would work stupendously well with water heaters.  Be a disaster with a motor load.  Too bad this is vaporware... the problem is how to sense when the generator is at peak performance.  Generators don't exactly have an output signal for that.
For what it's worth, water heaters are well insulated.  Once they reach target temperature, the water will stay hot for as long as a day, even with the power off.
To answer your question, you need a contactor.
Which is a big relay. And I would use either a DPDT contactor with an interlock, or a 3PDT contactor.   2 poles are for water heater power; the third is to allow a &quot;hold-on&quot; feature.
Here are the bones of it. The common goes to the water heater.  The NC (connected when de-energized) contacts go to the utility (i.e. its normal 30A breaker in the main panel).  At its quiescent state, it's on utility.  Its NO (connected when energized) contacts, you have several choices  there.  The coil contacts likewise; you have choices there as well.
The reason to make NC (coil off) the utility side is so that coil doesn't have to get held &quot;on&quot; 24x7x364.  Wasteful and unnecessary vampire load, that!
How to wire that contactor's coil to manually switch onto generator load is complicated, and I have an easier way.
Or, you could run the water heater on 120V
Which lowers its power usage to a very practical amount for a small generator, only ~1200 watts.
It takes the water heater 4 times as long to recover, but it works.  You can crunch the numbers easily enough, thanks to a convenient unit called BTU.   (note that HVAC equipment uses this unit incorrectly.)

1 BTU heats 1 pound of water 1 degree F.
1 KWH (1000 watts for 1 hour) equals 3410 BTU.
250 pounds of water in a 30-gallon water tank.

Go look at the nameplate (take the ONE largest wattage, don't add them, they don't run simultaneously) and punch it into the above, you'll see full recovery at nameplate wattage takes around an hour.  Okay, that's on utility.
On half voltage, the water heater's power will be 1/4 of that. Recovery will take 4 times as long.   Expect ~1200 watts give or take. &quot;Well gosh, that's well within the range of a common 120V circuit!&quot;
So instead of a 240V/30A breaker, it goes to a 120V/15A breaker.   With the contactor we just discussed, the NO side is connected: 1 wire goes to hot of the critical-loads sub, and the other wire goes to neutral of that same sub.
We still need to get the contactor coil to pick up.  We don't want it picking up on utility (and the critical-loads sub will be hot on utility mode)... so we can either take coil power hotshot off the generator side of the critical-loads panel interlock... or, here's what I'd do.
I'd get a plain old Intermatic mechanical timer... the kind you twist.  I would feed the contactor coil through that.  It will work and run down the clock whether power is on or off.   Basically whenever you set the timer, the water heater will be taking 120V off the critical-loads subpanel instead of 240V off the utility-only subpanel.
Since a typical water heater can fully recover in &lt;&lt;4 hours (run the math above), I wouldn't bother with a timer longer than that.  You set the timer AFTER you use hot water, so it can recover, and the next person can &quot;just use it&quot;.