The continuous increase in energy demand and the integration of a large number of renewable energy sources with intermittent production require an advanced energy management to guarantee an uninterrupted supply. Deployment of smart meters in wide areas of power networks and availability of affordable storage systems encourage development and implementation of local energy management systems. Local energy management is considered a feasible and inexpensive approach to reduce the impact of load variations and intermittent generation. Demand response and load shaping techniques provide customers with the facility to contribute to system balancing and improve power quality. This paper presents a building energy management which determines the optimal scheduling of the components of the local energy system. The developed two-stage optimization is based on a receding horizon strategy and minimizes the energy costs under consideration of the physical system constraints. The proposed building energy management has been implemented and tested with a medium-size hotel emulated in a microgrid testbed using power-hardware-in-the-loop techniques. The obtained results underline that energy management strategies can be validated under realistic conditions using flexible power scenarios.