This paper proposes a computationally efficient building energy management algorithm for demand response that can serve as a grid-ancillary system. The controller aims to regulate flexible loads and intelligent switches, complying with the utility's request. The control algorithm dynamically optimizes the load's configuration of the building. This optimization is based on the required power consumption level and the resident's actual comfort constraints. Since the load-matrix considered by the proposed algorithm is computationally expensive, a novel region-selection approach is incorporated in the algorithm to make the strategy computationally efficient. The proposed algorithm is validated through OPAL-RT Real-Time Digital Simulation with Raspberry Pi. The test results show that the algorithm is capable of curtailing controllable loads during emergencies and outage scenarios to maintain an uninterrupted supply to the critical loads and respect the power limit request of the building.