The integration of renewable energy sources into an energy grid introduces volatility, challenging grid stability and reliability. To address these challenges, this work proposes a two-stage optimization approach for the operation of electrolyzers used in green hydrogen production. This method combines site-wide and real-time optimization to manage a fluctuating energy supply effectively. By leveraging the dual use of an existing optimization model, it is applied for both site-wide and real-time optimization, enhancing the consistency and efficiency of the control strategy. Site-wide optimization generates long-term operational plans based on long-term forecasts, while real-time optimization adjusts these plans in response to immediate fluctuations in energy availability. This approach is validated through a case study showing that real-time optimization can accommodate renewable energy forecast deviations of up to 15%, resulting in hydrogen production 6.5% higher than initially planned during periods of increased energy availability. This framework not only optimizes electrolyzer operations but can also be applied to other flexible energy resources, supporting sustainable and economically viable energy management.