Grid-connected or off-grid energy storage system