Promoting Energy and Economic Empowerment with Python | by Diogo Sá | Towards Data Science

I created a tool to do solar power simulations over a 20 year time and tested it on my city, Porto, with several values of electricity average monthly bills. The goal is to give, for each average monthly bill, the best solar solution (which and how many solar panels, if a battery, and which battery, would be a good idea) to maximize the return of the investment.This report can be useful by consumers or companies.
The most important parts of the code can be viewed at Github.Check the report of this project below!
The idea for this project came out when I was trying to think about what I would do if I ran a renewable energy company. Of course, other software exists to calculate the renewable energy potential but neither is free, accurate and with the possibility to compare multiple solar panels at the same time. Being passionate about renewable energies I thought this would be the perfect first project on python data science libraries.
When it comes to scientific computing and data science the two key python libraries are NumPy and pandas.
Numpy has incredible advantages on its use: NumPy is written in Python and C, therefore NumPy arrays are faster compared to Python lists. However, unlike Python lists, NumPy arrays are not flexible and all elements should be of the same type.
Pandas is quite a game changer when it comes to analyzing data with Python. Pandas receives data (CSV file, SQL databases, etc.) and creates a Python object with rows and columns called data frame that looks very similar to table in a statistical software (allow mathematical operations on the data structures, ignoring the metadata of the data structures, uses relational operations and it is easy to handle missing data). This enables easy and fast data analysis and manipulation tools by providing numerical tables and time series data structures.
Scikit-learn is a powerful and efficient tool for data mining and data analysis. The library is focused on modeling data and is one of the main libraries to use when doing machine learning projects.
PVlib python is a community supported tool that provides a set of functions and classes for simulating the performance of photovoltaic energy systems. PVlib was developed at Sandia National Laboratories and it implements many of the models and methods developed at the Labs.
Sandia National Laboratories is facilitating a collaborative group of photovoltaic (PV) professionals (PV Performance Modeling Collaborative or PVPMC). This group is interested in improving the accuracy and technical rigor of PV performance models and analyses. Such models are used to evaluate current performance (performance index) and determine the future value of PV generation projects (expressed as the predicted energy yield) and, by extension, influence how PV projects and technologies are perceived by the financial community in terms of investment risk.
The source code for PVlib python is hosted on GitHub.
The important code used on this section can be viewed on Github: Function_Find_Panels, Find_Panels_DB, Prices_aux
Due to the lack of information regarding the solar panel prices on the Sandia Database, new ways of finding how the companies valued their products were found. Two databases were created based on web scrapping two different websites with solar panel price information. The two websites had different relevant information:
DB1: Brand; Solar Panel Model; Maximum Power; Everyday Power; Efficiency; Minimum Power; Cost per Panel; Country of production; Warranty;
DB2: Brand; Solar Panel Model; Power; Cost per Panel; Size; Weight; Country of production
As there was no clear or available data on the prices of the solar modules to be tested. I had to create an interpolation method to predict the price per watt that each solar company was charging based on the prices charged for the panels available for purchase, its power and the year.
A new database was created associating the solar panel brands with its predicted prices with two methods from scikit-learn library: (1) Linear Regression Method — A single independent variable is used to predict the value of a dependent variable; (2) Ransac Method — Iterative algorithm for the robust estimation of parameters from a subset of inliers from the complete data set.
Looking at the plots above, it is possible to see that for SunPower prices, the two regression methods do not differ one from another. On the other hand, LG shows a very interesting case as to why the RANSAC regressor should be chosen to interpolate the price of the modules. If a linear regression was chosen, it was assumed that the prices of LG panels decreased as the power increased and that does not make much sense. For that reason, the RANSAC Regressor was chosen to interpolate the solar module prices.
With the most information I could gather regarding the solar module prices, the two databases were compared (Sandia modules and solar panel prices) and 35 panels would be tested from that comparison (see table on the left). Panel Name and Sandia_Name are with “(...)” in the image on the lefy to avoid creating a table too large.
The important code used on this section can be viewed on Github: CONSUMPTION_FUNC, sazonal_consumption, AC_helper
The consumption function was adapted from the California hourly electric load curve. It was assumed that the same curve pattern applied to Portugal’s case. The curve from the image was scanned through a plot digitizer package which converted the curve to a x,y coordinates text file.
With numpy, I created a function that would fit the points that I had previously marked on the plot digitizer package. Several polifit functions with different regression degrees were run and the degree with less error was found at n=15 (see image below)
It was also important to know the seasonal consumption pattern. For that, the same procedure was made for a Portuguese energy monthly variation.
Now comes the tricky part:The daily variation had to fit within the yearly variation of energy consumption. In other words, the daily energy consumption within each month had to check with the variation that each month had within a year. For that, I divided every value with the sum of all the Watt values. That way, the sum of all values would be one.
The yearly variation on electrical consumption was made differently. Instead of having the sum equal to one, as in a cluster of consumptions, it made sense to divide every 12 values for the mean of the values. That way it would be easy to multiply the values with the average monthly bill.
With numpy functionalities, a matrix 24x12 was created. Inside the matrix, there are the values of electricity consumed through each hour every month (it was assumed that there was no variation of consumption within the days from the same month).
Finally, with the pandas library date-range (the same used in PVlib), for every hour of the calculation was allocated a specific electrical consumption.
The batteries database was built by web scrapping three websites with battery information and prices.
A database with 22 battery models was created. The models were from AXITEC Energy, LG, SolaX Power, BYD, Tesla, PYLONTECH, VARTA and Enphase were stored in a csv file and contained the information as the example on the left.
The important code used on this section can be viewed on Github: Solar_modules, Batt_Interpreter, Batt_Script
To ensure the most accurate results as possible, the calculations are done by the hour. The solar panels energy is calculated with irradiance changes over the year and over the years in hourly periods. Having a 20 year time-line, each round of calculations for a single panel combined with the consumption data takes about 40 seconds. Calculating all the panel production with all the batteries for all the electric bills would take an absurd amount of time. To tackle this issue I thought about discovering the best solution for each monthly electric bill in three steps:
Step 1 — Every solar panel would be tested to sort them in potential savings. The calculated savings would be an approximation of the real savings that would be calculated in step 2.Step 2 — The five best potential savings for each average monthly bill would make it step 2 calculations. On this stage, precision savings will be simulated.Step 3 — The two best cases on step 2 for every average monthly bill would be tested with every battery on the battery database.
The first calculations involving solar panels performance were made with the use of the PVlib library. For every average monthly bill, it is calculated how many solar panels would be needed to ensure that the yearly production of each panel installation would be at least close enough to the yearly consumption.
Three models were created for each solar module installation to give the consumers more choice and improve transparency. The first, using the closest integer number from what the yearly consumption divided by the annual energy of one panel would be (n=0). The second case would be one more panel than the first one (n=1) and the third case being one less panel than the first case (n=-1).
The savings per panel were calculated being the energy produced over the 20 years times the price per kWh of energy in Portugal subtracting by the price of the panel. The total savings for the recommended number of solar panels, the savings were multiplicated by the number of recommended solar panels. For each installation, a new database was created. The table above represents the best case when the monthly average bill is 50€ and n=0.
The results from step 1 are the basis for this stage. Only the five best results from the previous stage are calculated now.
A new method to calculate the total savings is introduced. On this stage, the savings are calculated hourly for improved precision. The consumption function calculated above takes part in this. The method consisted on calculating the savings being a balance between the money that one would spend without the installation during the 20 year time, the initial investment, the money that one would still have to buy to the grid (night times essentially) and the money earned from selling power to the grid (although in Portugal this value is still 4 times lower compared to buying energy to the grid).
To calculate hourly if the solar panels would be producing energy to feed the consumer needs, not producing at all or even producing more than the needs the following for loop was used with ac_df having 175 316 elements (number of hours in 20 years):
for k in range(len(ac_df)):        Grid_Power[k] = consumo[k] - ac_df.iloc[k].loc['Watt-hour']if Grid_Power[k] < 0:        Grid_Buying_Power[k] = 0        Grid_Selling_Power[k] = Grid_Power[k]if Grid_Power[k] > 0:        Grid_Buying_Power[k] = Grid_Power[k]        Grid_Selling_Power[k] = 0
Once again, three solutions were calculated for each average monthly bill.
The final results on this step present five panels and reorder them by the new savings calculation. On the image at the left there is the final information for the best panel for a monthly average of 75€ (when n=0). After looking at these results, a careful eye would say that the results presented on this panel are simply impossible. Unfortunately, a bought electricity of only 1129.6€ in a 20 year period is impossible. In Portugal, the daylight period is about 40%. Thus, without batteries, the total electricity bought from the grid would have to be somewhere near half of the price paid for electricity without solar panels. This error was found on several other panels and those panels were erased from the calculations.
Step 3 calculations contemplate the battery storage. The savings calculations are done similarly to step 2 but this time having the battery to store energy to be used overnight. It is expected that the remaining buying electricity to the grid to be near zero in all cases.
On these calculations, the 2 best panels (on step 2) on each average monthly bill was used.
To calculate hourly if the balance of energy production and battery storage the following for loop was used:
for k in range(len(ac_df)):if k == 0:        batt_stock[k] = batt_aux + ac_df.iloc[k].loc['Watt-hour']        -consumo[k]else:        batt_stock[k] = batt_stock[k - 1] +         ac_df.iloc[k].loc['Watt-hour'] - consumo[k]        Grid_Selling_Power[k],Grid_Selling_Power[k] = 0, 0if batt_stock[k] < 0:            Grid_Buying_Power[k] = - copy.deepcopy(batt_stock[k])            batt_stock[k] = 0if batt_stock[k] > Battery_Capacity_Wh[num]:            Grid_Selling_Power[k] = batt_stock[k] -                 battery_Capacity_Wh[num]            batt_stock[k] = copy.deepcopy(Battery_Capacity_Wh[num])            Grid_Buying_Power[k] = 0
The best solution was chosen based on the highest savings value.
Final results are presented for each average monthly electric bill:
Although the results are very accurate, approximations were made: (1) Every solar panel was considered to have the same 15% loss of efficiency over the 20 year period (see the calculations here); (2) Only one inversor was used on the calculations. This may result in different results since an inversor can work better on a certain type of solar panel; (3) The energy loss between solar modules was not accounted for.
Although the small setback with some panel’s bought from the grid values eliminated some solar modules from the calculation, reducing the number of solar panels and brands from the study, the goal of this project was reached. It was proven that solar panels can be a great long-term investment and batteries can make that investment even more profitable.We can draw a line between the number of watts hour consumed during a time period and the number of solar panels that your house should need. The number of panels installed should be able to generate that power at the same time period. That way, the solar installation will generate 200% emotional profit and 400% of real, monetary, profit.
Renewable energies can be a great way to empower new generations of people. Saving, at average, 620€ per year can be a big leap forward in people’s lives and with smart investments, the money saved on the electrical bill can become a game changer to entrepreneurs and companies.
This post marks the first perkier.tech project. perkier.tech creates innovative engineering projects aimed to help the world to be a better place and at the same time letting me research and learn something new.
The next step on this energy project will be creating a small grid-independent community using micro-grid and blockchain technologies with also implementing new ways of generating energy (p.e. small wind turbines).
I will be learning more about smart grids and blockchain code for the next few months.
I welcome feedback and suggestions! I’d love to hear from you about new ideas on how to implement innovative engineering.
You can reach me at diogoncsa@gmail.com or find me at LinkedIn