Energy management in the residential sector contributes to energy system dispatching and security with the optimal use of renewable energy systems (RES) and energy storage systems (ESSs) and by utilizing the main grid based on its state. This work focuses on optimal energy flow, ESS parameters, and energy consumption scheduling based on demand response (DR) programs. The primary goals of the work consist of minimizing electricity costs while simultaneously extending the lifetime of ESSs in conjunction with extracting maximum benefits throughout their operational lifespan and reducing CO2 emissions. Effective ESS and photovoltaic (PV) energy usage prices are modeled and an efficient energy flow management algorithm is presented, which considers the life cycle of the ESSs including batteries, electrical vehicles (EVs) and the efficient use of the PV system while reducing the cost of energy consumption. In addition, an optimization technique is employed to obtain the optimal ESS parameters including the size and depth of discharge (DOD), considering the installation cost, levelized cost of storage (LCOS), winter and summer conditions, energy consumption profile, and energy prices. Finally, an optimization technique is applied to obtain the optimal energy consumption scheduling. The proposed system provides all of the possibilities of exchanging energy between EV, battery, PV system, grid, and home. The optimization problem is solved using the particle swarm optimization algorithm (PSO) in MATLAB with an interval time of one minute. The results show the effectiveness of the proposed system, presenting an actual cost reduction of 28.9% and 17.7% in summer and winter, respectively, compared to a base scenario. Similarly, the energy losses were reduced by 26.7% in winter and 22.3% in summer, and the EV battery lifetime was extended from 9.2 to 19.1 years in the winter scenario and from 10.4 to 17.7 years in the summer scenario. The integrated system provided a financial contribution during the operational lifetime of EUR 11,600 and 7900 in winter and summer scenarios, respectively. The CO2 was reduced by 59.7% and 46.2% in summer and winter scenarios, respectively.