You can use a Smart Energy Meter for Source Detection.
Install a bidirectional energy meter (e.g., Victron Energy Meter, Eastron SDM, etc.) to measure power flow between grid, the inverter, and the load. The inverter uses this to decide: when you're generating solar power, when to charge batteries
and when to stop charging (e.g., at sunset).