I´m was using the folowing Blockly script with great success for the last year in my home automation system "iobroker".
Then I switch to a new automation system "Home Assistant" and unfortunately Blockly is not available there.
In loop
SET value of heater equal to value of switch (ON/OFF).
If status of Solar Power changed by anything DO
IF Time (7-17) and Batterycharge (>50%) are within the limits DO
If Solar Power > 2000Watt DO turn heater ON
ELSIFSolarPower <1000 DO Turn heater OFF
ELSIF(17-7) and Battercharge (<50%) DO turn heater OFF
THEN
IF value of heater unequal value of switch DO SET value of switch to value of heater.