Our water heater is a long way from the kitchen, and we waste a lot of water waiting for hot water to the sink. There is a 20a circuit breaker that powers the dishwasher and disposer. The dishwasher will continue to use the hot water from the main water heater. We would like to add a 2 gallon undercounter water heater to our hot water pipe - rated 1500W. Is there anything I can buy that will not allow the water heater to run when the dishwasher is on? The water heater is just for small tasks, but there is not enough power to run both at the same time.
You could run the power to the dishwasher and disposal through a current sensor and contacts on the sensor in series with a coil on a relay. The power to the water heater could be taped ahead of the sensor, and connected through "normally closed" contacts on the relay. When current is detected in the sensor the contacts on the sensor would close, energizing the coil on the relay opening the N.C. contacts, breaking power to the water heater.
Have you considered that the dishwasher will likely use more than 2 gallons of water; if it does and you have switched off the water heater, all you will get is the two gallons of hot and limitless cold water.
I took a quick look at under the sink hot water heaters on Amazon and discovered they all seem to require being hard-wired to a Ground Fault Circuit Interrupter (GFCI) circuit. What that means is:
Per the National Electrical Code a Ground Fault Circuit Interrupter (GFCI) Circuit Breaker is required to feed your current outlet.
Your existing outlet is already fed from a 20 amp GFCI circuit breaker, then you can hardwire the water heater upstream of your conventional 120V socket.
Having two GFCI protection devices wired in series is not beneficial and will probably be problematic. Therefore, if the existing outlet is a GFCI 120V socket you will need to replace it with a conventional 120V socket; $5 for a good one. The required GFCI circuit breaker will provide ground fault protection to all loads which are correctly connected to it.
You might want to consider a larger tank size; 8 gallons are available and seems a better choice and the additional per gallon costs are much lower.
On-Demand tankless heaters would be more efficient, however, they require 240 VAC and higher amperage which probably means running two new 10 AWG and a single 12 AWG ground wire from your load panel. If you choose this option you would instead install a two circuit ganged GFCI circuit breaker instead of a single circuit. A benefit for the time and expense is you would have the benefit of instant and limitless hot water at your sink.
If you own the home and are handy you may be able to do all of this yourself, assuming local ordinances permit that and you file for a permit and licensed electrician inspection. Otherwise I would recommend hiring a licensed electrician to give you an estimate first; think of that $1200 government COVID benefit check coming soon Then if you have a permit and inspection, an electrical fire in the new wiring it will be covered by your home-owners insurance. If you do it yourself without the permit/inspection, then maybe not so much! BTW although I am a degreed electrical engineer, I am not a licensed electrician. They are different skill sets, so please consult an electrician.
Because of the NEC Code requirements of installing a GFCI Circuit Breaker, I would not recommend you wire a switch. You only need to size that new GFCI circuit breaker to accommodate both the water heater and the dishwasher. If you can find the amperage requirements of your dishwasher use that number. If you are sure the heater will not come on, you could subtract 5 amps from the dishwasher's amperage before adding the tanked/tankless hot water heater amperage Add the tank/tankless water heater AND dishwater amps to determine the required minimum amp rating of the GFCI circuit breaker.