The overall aim of this project is to improve and implement an optimal energy management system for generation systems based on photovoltaic panels and with storage units. These systems can be both on-grid (connected to the network) and off-grid (isolated systems, generally in remote locations), and connected to a small number of users, such as a home or a small business. Specifically, the management system that is implemented will learn the characteristics of the generation and demand curves of the connected user, information that will be used to determine how to manage energy. This system will define when to store, use or inject the energy generated into the grid, as well as what to do with the stored energy, given future generation and demand scenarios. All this, with the objective of maximizing the economic / social benefits of the generation system, making distributed generation / storage systems more profitable and increasing their penetration.