Efficient alternatives in energy production and consumption are constantly investigated by increasingly strict policies. In this way, the pollutant emissions that contribute to the greenhouse effect reduce and sustainability of the electricity sector increase. With more than a third of the world's energy consumption, buildings have great potential to contribute these sustainability goals. Additionally, with growing incentives in the Distributed Generation (DG) and Electric Vehicle (EV) industry, it is believed that Smart Buildings (SBs) can be a key in the field of residential energy sustainability in the future. In this work, an energy management system in SBs are developed to reduce the power demanded of a residential building. In order to balance the demand and power provided by the grid, microgrids such as Battery Energy Storage System (BESS), EVs and Photovoltaic Generation panels (PV) are used. Here, a Mixed Binary Linear Programming formulation (MBLP) is proposed to optimize the charge and discharge scheduling of EVs and also BESS. In order to show the efficiency of the model, a case study involving three scenarios and an economic analysis is considered. The results point a 65% reduction in peak load consumption supplied by grid and a 28.4% reduction in electricity consumption costs.