Managing power return to grid with Shelly EM

Hi everyone!

Here in Spain you are paid for the energy that your solar panels returns to the grid if you are not using it, but is usually a low price, so its better to use it inside the house.

I have a Shelly EM with two clamps so I can monitor the power that the house is using and the production of the solar panels. A device called “FreeDS” can detect when consumption of the house is less than a value you set (let’s say 50W) and then send to the water boiler (or another device with a resistance) the amount of production of the solar panels, whichever is.

A resistance can heat water even if the power is less than his maximum, so every watt is used. If the producction is higher than the maximum power of the resistance or the water is hot enough, then energy will be sent to the grid.

I would like to get rid of the FreeDS and make this throug HA.

Using a Shelly 1 with probe addon is easy to turn on or off the relay accordingly to the house consumption and solar panel production, but, is there a way to control that the amount of power used by the water boiler is only the production of the solar panels in that moment, not the maximun power of the boiler resistance?

For example, if the solar panels have a excess of production of 500W, use this 500W (but just this 500W, not the 1.800W of the resistance) to heat the water boiler.
If the excess of production is 500W and the resistance demands 1.800W, when the relay is open, the excess will be 0, so the relay will close again, so it doesn’t makes sense.

Thanks in advance.