Renewables such as solar and wind energy are good for the environment, save energy costs, and are expected to be an important part of future energy systems. For many applications, however, renewables cannot be the only energy source because their output fluctuates, depending on weather and seasonal conditions. As a result, renewables are usually used in a hybrid energy system (HES) that has access to conventional energy supplies, such as the power grid and generators, as well as to re-chargeable batteries that store any excess renewable energy. In an HES, variable loads are ones that can be scheduled at flexible times and it is desirable to schedule them in a way that maximizes usage of renewable energy and minimizes costs. In this work, we developed an optimal approach based on a state-space model of the HES and dynamic programming. The efficacy of this approach has been demonstrated in simulated experimental results, where it performs significantly better than several other control strategies, such as the constant, random, and bang-bang controls.