Energy storage off-grid and grid-connected automatic switching