Hi,
it appears to be wasteful that hot or warm water is drained away in sewage pipes without at least recovering some energy from the temperature. To recover the energy different means are possible. Suggestive would be to recover the kinetic energy using some form of wheel or other mechanism. This is only possible for slightly soiled water as the danger of clogging the mechanism is rather great. Technically, the means to recover the kinetic energy is some form of obstacle which will collect slower and heavier substances easily.
To recover the heat energy is easier. By using heat conducting piping for parts of the way a heat transfer can be easily achieved. It suggest itself to use a meandering two metal coating inside the pipe which would in itself convert the heat into a small amount of electricity. Once the sacrifical electrode is used up the pipe will still be intact and therefore would not lead to any impairment or structural weakness of the functionality of the pipe.