During eclipses, the spacecraft cannot rely on solar energy and must exploit the power coming from the on-board batteries. Critical for the payload is to maintain its temperature inside a specific predetermined range to avoid degradation and malfunctioning. For XMM-Newton, this is done through the Temperature Closed Loop (TCL) unit, which automatically turns on and off the heaters when the instruments reach the minimum and maximum temperatures, respectively. A possible scenario is the case when more than a single instrument reaches the minimum temperature simultaneously, thus causing a peak in the demand of power from the batteries and threatening their integrity. Such a circumstance may impact the overall mission and a better battery management is therefore required. Here we propose a solution that uses machine learning to optimize the battery consumption during eclipses. We show that after training the algorithm with past data, we are able to predict the temperature profiles of the instruments with good accuracy. These predictions provide the relevant insight to determine the optimal times at which the heaters should be turned on and off, thus improving and optimizing the current eclipse operations. The approach presented here helps overcome the issue of excessively stressing the batteries and, despite being tailored for XMM-Newton, can be extended and applied to other missions as well.