Im not really anything more than you only try and error!
for MOST people and for most installed scripts it would be the simplest method to only take your surplus from your mysolar and put this into your elwa
power: >-
{{ (((states('sensor.stromverkauf_nach_wp_zaehler') | float -4 )* 36
+ states('sensor.elwa_e_2_mypv_stromverbrauch_aktuell') | float)) *
0.95 }}
this is only for calculating the amount of Power on which MY Elwa should run. (On our house there are special engery calculating and measuring devices installed due to our Heatpump) please look here: https://community.home-assistant.io/t/mypv-integation-started/395584/24?u=sleepymaxx
IN short i will only run my elwa on the amount of power which is going to the Grid AFTER all other house consumers, and after my Heapump is also satisfied.
Also i have installed a house Battery which should no be unloaded by my Elwa
So here i go for explaining my sensors and the idea of this template:
sensor.stromverkauf_nach_wp_zaehler
i take the actual power which is going from my house outwards to the grid
-4
i subtrahate 4 amounts of this measured amount (so i dont activate the ELWA too early) and this “4” is the same amount witch has to be the minium to start as condition of my numeric state of my activating script
* 36
i calculate the output how much watts should my elwa consume
+ states
sensor.elwa_e_2_mypv_stromverbrauch_aktuell
the calculated amount of energy for the next period has to be added to the actual amount of energy which is going through the elwa in the actual period (otherwise there will be to less energy going through the elwa and too much to the grid in the next period)
* 0.95
the now new calculated amount of power should be redueced a little bit, so if there is a little cloud or a consumer in my house starts it will not take engery from my battery or the grid!
- service: rest_command.update_elwa_power
this will push the explained amount of power to my elwa
* 0.95 / * 36 / - 4
so feel free to experiment with the calculating parts of your template… i think its depending on your house/installation/battery/waterboiler/using… what best calculating points you have. (i figured it out by controlling my history graph)
PS: please look out! my first script should go into your configuration.yaml
my second script is a automation!