this github has some good tips and a kit list for an example modbus setup with an ESP32. I know you can also make more advanced ones with a Raspberry Pi and Home Assistant.
Discharging when the grid is down I’m now so sure about. The inverter has an “off grid” mode but you’ll need to check the regulations about having a live connection since it might be dangerous to have your property and cabling still be live when there is an outage - you might injure someone trying to repair the damage at the other end.