The fundamental principle on which HOMER's simulation logic rests is the minimization of cost. Whenever it has the choice between two or more dispatchable power sources (batteries, generator, grid) it chooses the power source or combination of power sources that can supply the load and the required operating reserve at the least cost. That concept is explained in the paper "Micropower System Modeling with HOMER". Here I'll focus on the issue of grid-connected batteries.
In the case of a grid-connected system that contains a battery, HOMER will discharge the battery to serve the load whenever 1) there is some energy available in the battery and 2) battery power is cheaper than grid power. Otherwise it will draw power from the grid. If grid sales are allowed, HOMER will discharge the battery to sell to the grid whenever it can do so profitably.
HOMER calculates the fixed and marginal cost of each power source, then uses those values to decide which power sources to operate each time step. Unlike a generator, the battery and the grid have zero fixed cost. But their marginal costs can vary in time. The grid's marginal cost is equal to the grid power price, which can change in time. The battery's marginal cost is equal to the battery wear cost (a fixed value defined in the help file) plus the battery energy cost. The idea is that there are two separate costs to discharging the battery: the cost of wearing out the battery so it requires replacement a little faster, and the cost of the energy that's in the battery.
The battery energy cost is equal to the average cost of energy that has gone into the battery bank. If you always charge the battery bank with free renewable energy, the battery energy cost will be zero. But if you are buying off-peak grid power to charge the battery, it will not be zero. You can plot the battery energy cost in the hourly simulation results.
Important note: by default, in a grid-battery system HOMER tries to keep the battery fully charged, and unless you prevent it from doing so, it will charge the battery even with very expensive grid power. To use the battery for arbitrage (buying low, selling high) you must prevent this by setting constraints/control parameters through the grid power prices and demand prices.