A system with automatic control of local generation, consumption, storage, buying, and selling of electrical energy is provided. This automation can be governed by optimization criteria and policies established by the administrative entity responsible for the domain benefiting from this invention. The control method, using a data processing computer, implements the optimization criteria and provides near real time directives for the system. The control program estimates energy generation and consumption, monitors voltage and power levels, configures the power circuit and adjusts device specific controls over a network. Depending on a specific situation, the control program can continue to store extra energy, sell energy for a financial gain, maximize sustainable generation to meet social obligations, or increase consumption for extra comfort. This control program optimizes on multiple time granularities under a variable pricing scheme and environmental conditions, with related information including weather forecasts accessed over the Internet.