In this work, a strategy for scheduling a battery energy storage system (BESS) in a renewable energy community (REC) is proposed. RECs have been defined at EU level by the 2018/2001 Directive; some Member States transposition into national legislation defined RECs as virtual microgrids since they still use the existing low voltage local feeder and share the same low-medium voltage transformer. This work analyzes a REC which assets include PV generators, BESS and non-controllable loads, operating under the Italian legislative framework. A methodology is defined to optimize REC economic revenues and minimize the operation costs during the year. The proposed BESS control strategy is composed by three different modules: (i) a machine learning-based forecast algorithm that provides a 1-day-ahead projection for microgrid loads and PV generation, using historical dataset and weather forecasts; (ii) a mixed integer linear programming (MILP) algorithm that optimizes the BESS scheduling for minimal REC operating costs, taking into account electricity price, variable feed-in tariffs for PV generators, BESS costs and maximization of the self-consumption; (iii) a decision tree algorithm that works at the intra-hour level, with 1 min timestep and with real load and PV generation measurements adjusting the BESS scheduling in real time. Validation of the proposed strategy is performed on data acquired from a real small-scale REC set up with an Italian energy provider. A 10% average revenue increase could be obtained for the prosumer alone when compared to the non-optimized BESS usage scenario; such revenue increase is obtained by reducing the BESS usage by around 30% when compared to the unmanaged baseline scenario.