In this study, an optimisation model is developed for two-stage energy management of a residential building to minimise energy cost under monthly power-based tariffs for peak demand and time variable electricity prices. The expected peak demand is determined in the first stage, and then the energy management system minimises the energy cost in the second stage. The optimisation problem of the second stage is solved in a rolling time window for the real-time operation of the flexible energy sources in the building. The optimal charging and discharging of the battery energy system, the charging of the electric vehicle battery, the operation of the heating system and the optimal start times of washing machines and dishwashers are determined close to real-time. The proposed approach allows the user to determine the expected peak during the month ahead and try to keep the peak demand in daily operation below that value using a close to real-time energy management system. The performance of the two-stage approach for demand-side management of a residential building has been validated by a realistic case study.