This is a fairly complicated topic. You have a couple of different issues here:
You seem to be trying to do calculations with solar import and export values. Those numbers are not readily available from IoTaWatt over longer timeframes because they require integrating the higher resolution data, which is an I/O and compute intensive activity that the ESP8266 just doesn’t have the resources to do. There are ways to accumulate import and export, but they require sacrificing additional inputs to obtain datalog space to save the data.
I have explained parts of this issue in various posts on the forum, but really need to add a thorough explanation of the problem and various solutions to the documentation. So I’ll try to do that over the next couple of days.
I will say, without actually seeing your data, that I don’t think there is a problem with the output calculation. I believe it is a lack of understanding how the script system works and what data it works on and when.
There are ways to track solar import and export over longer periods, but it requires dedicating additional resources to it or using an external database with the space to save the accumulated values and/or the ability to integrate the detail data quickly.