## 3 Methods  
The objective of this work is to develop an energy management system that, using the basic principles of EMPC, minimizes the cost of electricity consumed in a house by optimizing its electrical energy consumption, of which air conditioning is an important part but not the only one. It has been developed for use with a regulated electricity market, in which the price of energy is updated in real time every hour. As starting and design conditions, the analysis is carried out for a house that has an energy input from the grid, a self-consumption system (solar panels), and an energy storage system (batteries). In addition, the structural characteristics of the house envelope are also used as a thermal energy storage system (also called thermal battery). The proposed system has two layers: a planning layer that proposes the optimal distribution of energy sources over a given time horizon that selects when it is best to use grid or stored energy based on energy costs, and a second layer with a real-time control system that regulates the HVAC system for efficient use. First, a system for planning the electrical energy consumption of the house was designed. As inputs to the planning system, predictions of the electrical consumption of the different appliances present in the house are needed. In addition, it is also necessary to forecast the intended use and occupancy of the residence in order to ascertain the necessity for HVAC. HVAC consumption is obtained from the predictions of outdoor atmospheric conditions, such as temperature and solar radiation, for example. Finally, it is also necessary to forecast the variation of energy prices. The data provided by OMIE [53] were used to forecast electricity market prices. OMIE is the Iberian Market Operator that manages the day-ahead and intraday markets based on the criteria of independence, transparency, and objectivity. The forecast model for longer periods used for this work is based on the reference [54]. In this way, the planning system estimates the electricity consumption depending on the time of day based on predictions of occupancy, the consumption of appliances, lighting, etc., along with the prediction of energy demand of the HVAC system. Statistical data provided by Eurostat [55] have been used to predict the consumption of the different electrical appliances, lighting, etc. For the photovoltaic (PV) production estimates, the PVGIS platform [56], widely used in the sector, has been chosen, thanks to which, by entering the orientation and inclination data of the panels, a production curve is obtained for the desired location. Finally, an algorithm based on model predictive control (MPC) has been used for the prediction of the HVAC system consumption, which aims to accurately calculate the energy consumption as a function of time. This control system is based on a thermal model of the house and is fed with predictions based on the time of day that affect the energy demand, such as outdoor temperature, solar radiation, etc. When choosing a source of weather forecasts, there are different sources of information, as well as agencies that provide reliable weather forecasts. In this case, Weatherbit [57] has been chosen, since it contains information on different time scales and weather parameters, such as temperature, humidity, etc. Then, the objective of the system planner is to make decisions on how to distribute the electrical demand within the house among the possible sources that supply it, such as the electrical grid, self-consumption (for example, with solar panels), electric batteries, and the control of the HVAC. The system planner is schematically represented in Figure 1. In the planner's operating scheme, the communication with the grid is bidirectional, taking energy from the grid when necessary or returning energy to the grid in case of surplus. The consumption of electrical appliances and lighting is always an input.