Smart Automatic Control of Energy Flows in Buildings