Driven by fast advancements in wind and photovoltaic (PV) technologies, onsite renewable electricity generation is becoming attractive to manufacturers since they are able to reduce electricity purchases from the grid and may lower their electricity costs. This paper proposes a methodology to minimize the electricity cost of a grid-connected factory that also has onsite solar power generation and battery storage. Purchases from the grid are subject to time-of-use electricity rate schedules. The problem is formulated asa mixed-integer programming problem and GAMS is used to find the optimal manufacturing and onsite energy flow schedules that have the minimal electricity cost. A case study with one hybrid flow shop, onsite PV power generation, and a battery was used to test the proposed method. Testing results showed that the factory’s electricity cost can be reduced by 54.0% under summer TOU rate on a typical day while a 0.7% electricity cost reduction can be achieved for a representative day under a winter TOU rate. An annual electricity cost savings of 28.1% can be obtained with the optimal schedules. In addition, a parametric study incorporating the optimal schedules was performed to understand the economic performances associated with different PV capacity and battery bank size for the factory.