Tracking energy useage based on binary sensor being on or off

Hi,

Definitely a bit over my head on this one.

I have an energy tariff that is dynamic and changes from peak to low rate and vice versa depending on many factors.

I have automated home battery storage to charge and discharge based on when a binary sensor turns on and off that tracks the price change from peak to low rate.

I track my energy usage from the grid using a Shelly EM. What I’d like to see/track is my grid useage when the battery is in use / the binary sensor is off. This is because my inverter will only output at a maximum of 3000w. Anything over that and I’m drawing from the grid. I’d like to know what that figure is.

I already have a helper set up to tell the Energy tab what price per kWh is being charged during these different peak and cheap rates.

Thanks for any help.