I think this is covered in another WTH, including accounting for solar/ battery/ grid consumption prices: