Energy cost from HVAC is a significant fraction of the over- All operational cost of a commercial building. Moreover, in developing countries such as India with inadequate grid connectivity and frequent outages, diesel generators are a common source of backup power. While this may solve the problem of outages, power produced by diesel generators is much more expensive than grid power. This work proposes an optimization framework targeted to minimize energy costs of buildings in the presence of outages and a mix of energy sources such as grid, diesel generators, solar, and battery. Our framework is novel since it exploits the inherent ability of a building to store thermal energy and availability of opportunistic occupancy proxies such as CO2 concentration. The framework when investigated for real world scenarios including office buildings and cell towers, resulted in savings of more than 10% relative to normal operational practices.