You can also do this at a household level. It is called heat recovery drain pipe, basically a heat exchanger that replaces a vertical section of drain pipe. The wastewater heat is used to pre-warm the fresh water going to the water heater. Water falling down a vertical pipe tends to spread out and cling to the inner surface, giving you more surface area for heat transfer.
Last time I priced this system out the installed cost was going to be about $1000, at that price point the payback time is pretty long for a single family home but much better for an apartment building.