Future building energy management systems will have to be capable of adapting to variation in the rate of production of energy from renewable sources. Controllers employing a model predictive control (MPC) framework can optimise and schedule energy usage based on the availability of renewably generated energy.In this paper, an MPC using artificial neural networks (ANNs) was implemented in a residential building. The ANN-MPC was successfully tested and demonstrated good performance predicting the building's energy consumption. The controller was then modified to function as an economic MPC (EMPC) to optimise demand flexibility (i.e., the ability to adapt energy demands to fluctuations in supply). The operational costs of energy usage were associated with this demand flexibility, which was represented by three flexibility indicators: flexibility factor, supply cover factor, and load cover factor. The results from a day-long test showed that these flexibility indicators were maximised (flexibility factor ranged from -0.88 to 0.67, supply cover factor from 0.04 to 0.13, and load cover factor from 0.07 to 0.16) when the EMPC controller's demand flexibility was compared to that of a conventional proportional-integral (PI) controller. The EMPC framework for demand flexibility can be used to regulate on-site energy generation, grid consumption, and grid feed-in and can thus serve as a basis for overall optimisation of the operation of heating systems to achieve greater demand flexibility.