Yeah that’s one way to do it.
The other way would be a utility meter with two tariffs, one for energy in, one for energy out. Then create a trend binary sensor that tracks if the energy is increasing or decreasing:
You switch which utiltiy meter tariff sensor accumulates based on that sensor state. See here for an example of using tariffs: How to use Utility Meter Tariffs to conditionally measure things
Your way has:
two template sensors
two utility meters
My way:
one template sensor
one utility meter (that generates two sensors)
one automation.
No real advantage to doing it either way. They both produce the two sensors you want.