The new challenge in power systems design and operation is to organize and control smart micro grids supplying aggregation of users and special loads as electric vehicles charging stations. The presence of renewable and storage can help the optimal operation only if a good control manages all the elements of the grid. New models of green buildings and energy communities are proposed. For a real application they need an appropriate and advanced power system equipped with a building automation control system. This article presents an economic model predictive control approach to the problem of managing the electric and heating resources in a smart building in a coordinated way, for the purpose of achieving in real time nearly zero energy consumption and automated participation to demand response programs. The proposed control, leveraging a mixed integer quadratic programming problem, allows to meet manifold thermal and electric users' requirements and react to inbound demand response signals, while still guaranteeing stable operation of the building's electric and thermal storage equipment. The simulation results, performed for a real case study in Italy, highlight the peculiarities of the proposed approach in the joint handling of electric and thermal building flexibility.