Most Li ion insertion batteries consist of a porous cathode, a separator filled with electrolyte and an anode, which very often also has some porous structure. The solid part especially in the cathode is usually produced by mixing a powder of the actual active particles, in which Li ions will be intercalated, binder and carbon black to enhance the electronic conductivity of the electrode. As a result the porous structure of the electrodes is very complex, leading to complex potential, ion and temperature distributions within the electrodes. The intercalation and deintercalation of ions cannot be expected to be homogeneously distributed over the electrode due to the different transport properties of electrolyte and active particles in the electrode and the complex three-dimensional pore structure of the electrode. The influence of the final microstructure on the distribution of temperature, electric potential and ions within the electrodes is not known in detail, but may influence strongly the onset of degradation mechanisms. For being able to numerically simulate the transport phenomena, the equations and interface conditions for ion, charge and heat transport within the complex structure of the electrodes and through the electrolyte filled separator are needed. We will present a rigorous derivation of these equations based exclusively on general principles of nonequilibrium thermodynamics. The theory is thermodynamically consistent i.e. it guarantees strictly positive entropy production. The irreversible and reversible sources of heat are derived within the theory. Especially the various contribution to the Peltier heat due to the intercalation of ions are obtained as a result of the theory