A systematic method is proposed for enhancing the energy efficiency of vertical farms when integrated into urban infrastructure. The method relies on the optimization of a multiperiod model that determines the system's best operation plan considering variations in its parameters throughout the day. These parameters include factors such as resources availability from the urban infrastructure and fluctuations in electricity prices. The model is formulated and solved via the tools of the P-graph framework, which permits the automatic formulation of the rigorous superstructure and the efficient optimization of the integrated system's operation. The method is illustrated via a case study comprising three scenarios. The most efficient case demonstrates up to 40% electricity cost saving potential for a particular operation day, and up to 31% for the entire year. The method presented constitutes an optimization tool that can use real data on electricity prices and forecasts of weather conditions to reduce operating costs and enhance the energy efficiency and sustainability of vertical farming systems.