Author Topic: Automatic Solar battery charging timings based on forecast weather  (Read 3377 times)

0 Members and 1 Guest are viewing this topic.

Online IanJTopic starter

  • Supporter
  • ****
  • Posts: 1737
  • Country: scotland
  • Full time EE & Youtuber
    • IanJohnston.com
Re: Automatic Solar battery charging timings based on forecast weather
« Reply #25 on: September 15, 2024, 11:44:59 am »
GUI mods including:
Giving the Charts a 12hr/24hr selection option.
Now displaying todays/tomorrows Irridiance.

Battery Boost added with setpoint.
If battery is depleted enough it won't cover 4pm-7pm then boost charge it in advance (3pm-4pm) from the grid. This is because during those hours the electricity cost from my provider goes up significantly, so it'll pay to boost charge in advance.

I thinks that enough mods for a while, time to just let it run 24/7 and monitor the results.

Ian.

« Last Edit: September 15, 2024, 02:39:05 pm by IanJ »
Ian Johnston - Original designer of the PDVS2mini || Author of the free WinGPIB app.
Website - www.ianjohnston.com
YT Channel (electronics repairs & projects): www.youtube.com/user/IanScottJohnston, Twitter (X): https://twitter.com/IanSJohnston
 

Offline EEVblog

  • Administrator
  • *****
  • Posts: 38690
  • Country: au
    • EEVblog
Re: Automatic Solar battery charging timings based on forecast weather
« Reply #26 on: September 16, 2024, 02:25:25 am »
I've always thought this sort of automation would be cool, but I suspect it's too much trouble, at least for my solution.
e.g. If the battery if full and the sun is still out max then I'd love to be able to automatically switch the pool heater on max and a couple of aircons to energy sink the house instead of feeding back inot the grid.

Even though we have just got out of winter, I already have more than enough solar than I know what to do with.

Yes, it is a LOT of work!.....I only implemented it because I really enjoy doing it. A true glutten for punishment!

Yeah, as much as I like this stuff, I don't really want to be constantly dicking around with it.
So at the moment I just have a permanent timer that turns the pool heater on at 10am and off at 4pm regardless of the weather or battery condition. Works fine almost all the time.
Aircon control would mean installing IR transmitters in every room, that's ugly, so once again, better off with just timers which I might look into to use some extra energy. At the moment we are exporting heaps of energy, and the battery isn't even close to 20% cutoff wince we upgraded it to 25kWh
 

Online IanJTopic starter

  • Supporter
  • ****
  • Posts: 1737
  • Country: scotland
  • Full time EE & Youtuber
    • IanJohnston.com
Re: Automatic Solar battery charging timings based on forecast weather
« Reply #27 on: September 19, 2024, 03:24:37 pm »
Ok, I couldn't resist some more mods.

I added a small log display to see the past 6 events. Scrolls upwards.

Also added an X-axis scale to fit the types of graph charts I have created, i.e. the data scrolls in from the right and the charts are 12 or 24hr long depending on the checkbox above.
I had to code the scale completely independent of the built in chart X-axis scaling because it doesn't support what I wanted. MS Chart kinda sucks!
The times on the scale will scroll to the left with the data above it. As you can see I've been running it since just after 2pm.

Still testing the Irridiance/Solis Inverter controlled battery charging time config each day.....but so far so good.

Ian.

« Last Edit: September 19, 2024, 03:49:04 pm by IanJ »
Ian Johnston - Original designer of the PDVS2mini || Author of the free WinGPIB app.
Website - www.ianjohnston.com
YT Channel (electronics repairs & projects): www.youtube.com/user/IanScottJohnston, Twitter (X): https://twitter.com/IanSJohnston
 

Online IanJTopic starter

  • Supporter
  • ****
  • Posts: 1737
  • Country: scotland
  • Full time EE & Youtuber
    • IanJohnston.com
Re: Automatic Solar battery charging timings based on forecast weather
« Reply #28 on: September 21, 2024, 09:48:59 am »
Wee update,

Irridiance on it's own looks like it is not enough from my testing over several different days, due to there are two irridiance values received from OpenWeatherMap.org, one for cloud based and another for clear based which has been difficult to pull into a useable figure that works for all types of weather.
So, I have changed to a hybrid calculation based on irridiance and cloud cover.

I'm pulling hourly irradiance and cloud cover data from the Meteosource API, adjusting the data pulled based on monthly sunrise/sunset as it varies through the year thus only considering the relevant hours for solar energy potential.
Then, the hourly values are are summed to create totals, then hybrid calculating a figure from both Irr & cloud which I can then use for the daily forecast SolarPV.

We'll see how this goes.......but it takes time to experience different weather days!

PS. I did look for another API that gives actual SolarPV predictions, but all the ones I found are very expensive. If you know of one let me know.

New sub in question for those interested.

Ian.

Code: [Select]
Private Sub MeteosourceIrradiance()

    ' Meteosource API configuration
    Dim MeteosourceAPIKey As String = "###################################"
    Dim latitude As String = "########"
    Dim longitude As String = "########"
    Dim Timezone As String = "###############"
    Dim apiUrl As String = $"https://www.meteosource.com/api/v1/flexi/point?lat={latitude}&lon={longitude}&sections=hourly&timezone={Timezone}&language=en&units=auto&key={MeteosourceAPIKey}"

    ' Ping retry settings
    Dim deviceAddress As String = "www.meteosource.com"
    Dim maxRetries As Integer = 3
    Dim retryDelaySeconds As Double = 0.2

    ' Check if the checkbox is checked
    If CheckBox8.Checked = True Then

        ' Call Function to check connectivity
        If TryPingDevice(deviceAddress, maxRetries, retryDelaySeconds) Then


            ' Ping was successful
            Try
                Dim jsonResponse As String = New WebClient().DownloadString(apiUrl)

                ' Parse the JSON response
                Dim data As JObject = JObject.Parse(jsonResponse)
                Dim hourlyData As JArray = data("hourly")("data")

                ' Initialize total variables for today's and tomorrow's irradiance and cloud cover
                Dim totalTodayIrradiance As Double = 0
                Dim totalTodayCloudCover As Double = 0
                Dim todayHours As Integer = 0

                Dim totalTomorrowIrradiance As Double = 0
                Dim totalTomorrowCloudCover As Double = 0
                Dim tomorrowHours As Integer = 0

                ' Define today and tomorrow's date
                Dim today As DateTime = DateTime.Now.Date
                Dim tomorrow As DateTime = today.AddDays(1)

                ' Define start and end hour variables
                Dim startHour As Integer
                Dim endHour As Integer

                ' Get the current month
                Dim currentMonth As Integer = DateTime.Now.Month

                ' Adjust based on the month (12 CASE statements for sunrise/sunset adjustment)
                Select Case currentMonth
                    Case 1 ' January
                        startHour = 10
                        endHour = 15
                    Case 2 ' February
                        startHour = 9
                        endHour = 16
                    Case 3 ' March
                        startHour = 8
                        endHour = 17
                    Case 4 ' April
                        startHour = 7
                        endHour = 19
                    Case 5 ' May
                        startHour = 6
                        endHour = 20
                    Case 6 ' June
                        startHour = 6
                        endHour = 21
                    Case 7 ' July
                        startHour = 6
                        endHour = 21
                    Case 8 ' August
                        startHour = 6
                        endHour = 20
                    Case 9 ' September
                        startHour = 7
                        endHour = 18
                    Case 10 ' October
                        startHour = 8
                        endHour = 17
                    Case 11 ' November
                        startHour = 9
                        endHour = 15
                    Case 12 ' December
                        startHour = 10
                        endHour = 15
                End Select

                ' Clear the ListBox before adding new data
                ListBoxHourlyData.Items.Clear()
                ListBoxHourlyData.Items.Add("Forecast = " & DateTime.Now.AddDays(1).ToString("dd-MM-yyyy") & ":" & vbCrLf)      ' Add the forecast date header

                ' Loop through the hourly data and process each entry
                For Each hourData As JObject In hourlyData
                    ' Get the date string from the "date" field and split by space
                    Dim hourDateRaw As String = CStr(hourData("date"))
                    Dim dateAndTime As String() = hourDateRaw.Split(" "c) ' Split by space

                    ' Get the date and time separately
                    Dim datePart As String = dateAndTime(0) ' Left part (date)
                    Dim timePart As String = dateAndTime(1) ' Right part (time)

                    ' Reformat the date part
                    Dim dateComponents As String() = datePart.Split("/"c) ' Split by "/"
                    datePart = $"{dateComponents(2)}-{dateComponents(1)}-{dateComponents(0)}"

                    ' Create the full date and time string
                    Dim fullDateTime As String = datePart & "T" & timePart

                    ' Convert fullDateTime to DateTime
                    Dim hourDate As DateTime
                    Try
                        ' Use DateTime.ParseExact to specify the correct format of the fullDateTime
                        hourDate = DateTime.ParseExact(fullDateTime, "yyyy-dd-MMTHH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)

                        ' Check if the hour is within the valid daytime range
                        ' Both the Irradiance and the Cloud readings per hour are totalled to present a summed value each for the day.
                        ' The hours to consider are dependant on the sunrise to sunset times.
                        Dim hour As Integer = hourDate.Hour
                        If hour >= startHour And hour <= endHour Then
                            ' Check if the hour belongs to today or tomorrow
                            If hourDate.Date = tomorrow Then
                                totalTomorrowIrradiance += CDbl(hourData("irradiance"))             ' sum the irradiance
                                totalTomorrowCloudCover += CDbl(hourData("cloud_cover")("total"))   ' sum the cloud cover
                                tomorrowHours += 1

                                ' In the 2nd listbox display the hourly Irradiance and Cloud Cover values
                                Dim timeFormatted As String = $"{hourDate:HH:mm}"   ' Format time
                                Dim irradianceFormatted As String = $"{CDbl(hourData("irradiance"))} W/m²".PadRight(15) ' Ensure consistent width
                                Dim cloudCoverFormatted As String = $"{CDbl(hourData("cloud_cover")("total"))} %".PadRight(10) ' Ensure consistent width
                                Dim outputString As String = timeFormatted & vbTab & irradianceFormatted & vbTab & cloudCoverFormatted
                                ListBoxHourlyData.Items.Add(outputString) ' Add the formatted string to the ListBox
                            End If
                        End If
                    Catch ex As FormatException
                        ' If date parsing fails, print a debug message
                        'Debug.Print($"Failed to parse date: {fullDateTime} - {ex.Message}")
                    End Try
                Next


                ' Save contents of ListBoxHourlyData
                ' Create a StringCollection to hold the ListBox items
                Dim itemsToSave As New Specialized.StringCollection()
                ' Add each item in the ListBox to the StringCollection
                For Each item As String In ListBoxHourlyData.Items
                    itemsToSave.Add(item)
                Next
                ' Save the StringCollection to My.Settings
                My.Settings.ListBoxItems = itemsToSave
                My.Settings.Save()


                ' Calculate averages for tomorrow - not used
                Dim avgTomorrowIrradiance As Double = If(tomorrowHours > 0, totalTomorrowIrradiance / tomorrowHours, 0)
                Dim avgTomorrowCloudCover As Double = If(tomorrowHours > 0, totalTomorrowCloudCover / tomorrowHours, 0)

                ' Hybrid calculation for tomorrow's irradiance/cloud value
                ' The hybrid Irr/Cloud value is the total Irradiance value modified my the total cloud cover value.
                ' No clouds all day then the total Irradiance value all all but unchanged.
                ' Full clouding all day then the value is brought way, way down.
                ' The final value goes off to be compared through the ruleset in the Solis dub.
                HybridTomorrowIrradianceCloud = totalTomorrowIrradiance * (1 - (totalTomorrowCloudCover / ((endHour - startHour + 1) * 100)))      ' Value that will be used to calculate on/off charging times for Solis Sub

                ' Log the forecast data to a text file
                Dim logData As String = $"{DateTime.Now.AddDays(1):dd-MM-yyyy HH:mm}" & vbCrLf &
                                     $"    Forecasted Total Irradiance: {totalTomorrowIrradiance} W/m²" & vbCrLf &
                                     $"    Forecasted Total Cloud Cover: {totalTomorrowCloudCover} %" & vbCrLf &
                                     $"    Hybrid Irr/Cloud Value: {Math.Floor(HybridTomorrowIrradianceCloud).ToString()}" & vbCrLf &
                                     $"    Current Battery Level: {BatteryCapacity.Text} %" & vbCrLf &
                                     "------------------------------------------------------------" & vbCrLf
                Dim logFileName As String = $"ForecastLog.txt"
                Dim logFilePath As String = System.IO.Path.Combine(Application.StartupPath, logFileName)                    ' Change to your desired log directory
                ' Append log data to the file
                System.IO.File.AppendAllText(logFilePath, logData)

                ' Update GUI
                HybridIrrCloud.Text = CInt(HybridTomorrowIrradianceCloud).ToString()
                IrridianceTomorrow.Text = CInt(totalTomorrowIrradiance).ToString() ' Update UI element with the final value
                CloudTomorrow.Text = CInt(totalTomorrowCloudCover).ToString() ' Update UI element with the final value
                IrradianceCloudForecastDate.Text = DateTime.Now.AddDays(1).ToString("dd-MM-yyyy")

                ' Hybrid value for GUI
                'HybridIrrCloud.Text = CInt(HybridTomorrowIrradianceCloud).ToString() ' Update UI element with the final value
                My.Settings.data40 = HybridIrrCloud.Text
                My.Settings.data41 = IrridianceTomorrow.Text
                My.Settings.data43 = CloudTomorrow.Text
                My.Settings.data44 = DateTime.Now.AddDays(1).ToString("dd-MM-yyyy")
                My.Settings.Save()


            Catch ex As Exception
                ' Handle any exceptions during data retrieval
                Dim currentDateAndTime As DateTime = DateTime.Now
                Dim formattedDateTime As String = currentDateAndTime.ToString("dd-MM-yyyy HH:mm", CultureInfo.InvariantCulture)
                AddToLogBox(formattedDateTime & " " & "Error fetching data from Meteosource API: " & ex.Message, Color.Red)
                IsOkIrridiance = False
                LEDirridiance.State = OnOffLed.LedState.OffSmall   ' fail RED LED
                Exit Sub
            End Try

        Else
            ' Ping failed, handle the error
            Dim currentDateAndTime As DateTime = DateTime.Now
            Dim formattedDateTime As String = currentDateAndTime.ToString("dd-MM-yyyy HH:mm", CultureInfo.InvariantCulture)
            AddToLogBox(formattedDateTime & " " & "Error: Unable to reach Meteosource API.", Color.Red)
            IsOkIrridiance = False
            LEDirridiance.State = OnOffLed.LedState.OffSmall   ' fail RED LED
            Exit Sub
        End If

    End If

End Sub
« Last Edit: September 27, 2024, 04:29:36 pm by IanJ »
Ian Johnston - Original designer of the PDVS2mini || Author of the free WinGPIB app.
Website - www.ianjohnston.com
YT Channel (electronics repairs & projects): www.youtube.com/user/IanScottJohnston, Twitter (X): https://twitter.com/IanSJohnston
 

Online IanJTopic starter

  • Supporter
  • ****
  • Posts: 1737
  • Country: scotland
  • Full time EE & Youtuber
    • IanJohnston.com
Re: Automatic Solar battery charging timings based on forecast weather
« Reply #29 on: September 27, 2024, 04:42:31 pm »
With the updated code in my previous post.....here's a couple of screenshots below.

So at just before midnight yesterday the code calculated a hybrid Irradiance/Cloud value of 1503 and thus per the ruleset it required 1hr of charge between 2am and 3am.
The battery charge for that 1hr brought it up to just under 80%.
3am onwards the battery discharged into the house until about 9am when the sun started to appear.
On and off the battery charged & discharged with the SolarPV (cloudy day) and reached a peak of 100% just and no more as you can see on the graph before the SolarPV dropped low.
The fact it reached a peak of 100% just and no more is the win for me.

So far so good!

Ian.


Ian Johnston - Original designer of the PDVS2mini || Author of the free WinGPIB app.
Website - www.ianjohnston.com
YT Channel (electronics repairs & projects): www.youtube.com/user/IanScottJohnston, Twitter (X): https://twitter.com/IanSJohnston
 
The following users thanked this post: Siwastaja

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 8796
  • Country: fi
Re: Automatic Solar battery charging timings based on forecast weather
« Reply #30 on: September 27, 2024, 05:48:17 pm »
Do you have some kind of hourly spot pricing? If not and cost of import was the same for those night hours then surely one more optimization would be to avoid cycling the battery and just buy energy from grid, both for better battery lifetime and because there are some losses (maybe 4-5% loss back-and-forth assuming 98% efficiency one way). I mean like in the attachment; when price stays constant over some fixed period, PV production is zero during that period and desired start and end SoC are the same, then just turn off the battery to zero power setting instead of discharge-charge-discharge.

But this is of course more complication to the algorithm. Interesting stuff, thanks for sharing.
 

Online IanJTopic starter

  • Supporter
  • ****
  • Posts: 1737
  • Country: scotland
  • Full time EE & Youtuber
    • IanJohnston.com
Re: Automatic Solar battery charging timings based on forecast weather
« Reply #31 on: September 27, 2024, 07:35:00 pm »
Do you have some kind of hourly spot pricing? If not and cost of import was the same for those night hours then surely one more optimization would be to avoid cycling the battery and just buy energy from grid, both for better battery lifetime and because there are some losses (maybe 4-5% loss back-and-forth assuming 98% efficiency one way). I mean like in the attachment; when price stays constant over some fixed period, PV production is zero during that period and desired start and end SoC are the same, then just turn off the battery to zero power setting instead of discharge-charge-discharge.

But this is of course more complication to the algorithm. Interesting stuff, thanks for sharing.

The battery voltage wouldn't stay horizontal on the graph like you marked up. It would continue downwards if I never charged it.
The idea is to try an build up charge to last the next day. Forecasting helps, but you still don't know for sure, and worst case scenario the battery runs out of charge before or during the 4pm to 7pm timeslot where the costs to import is almost double cost on the Octopus tariff.

With a 10kWh battery it's not a small capacity battery, but it's also not big.......so managing it is quite a lot of things to balance.

Ian.
Ian Johnston - Original designer of the PDVS2mini || Author of the free WinGPIB app.
Website - www.ianjohnston.com
YT Channel (electronics repairs & projects): www.youtube.com/user/IanScottJohnston, Twitter (X): https://twitter.com/IanSJohnston
 

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 8796
  • Country: fi
Re: Automatic Solar battery charging timings based on forecast weather
« Reply #32 on: September 28, 2024, 06:13:43 am »
The battery voltage wouldn't stay horizontal on the graph like you marked up. It would continue downwards if I never charged it.

I know, surprisingly large quiescent currents on these inverters (and maybe even batteries with their BMS and contactors) is a real issue with these batteries, it's a significant consumption averaging possibly tens of W and needs to be supplied from somewhere. You need some maintenance charging every few days.

But in this case as I understood it your battery was feeding your house loads, then charged for an hour at relatively high power, then kept feeding on house loads. This definitely does not make any sense unless there is price difference between those hours. Yes, you can't avoid supplying the quiescent current of inverter+battery so you need to charge a little, but now you are additionally having efficiency losses at around 4-5% plus extra wear from unnecessary cycling.

And I'm well aware these battery systems do such insane cycling even on their own (just yesterday evidenced a Growatt hybrid doing that, cycling around 10% SoC charging from grid and discharging to loads).
 

Online IanJTopic starter

  • Supporter
  • ****
  • Posts: 1737
  • Country: scotland
  • Full time EE & Youtuber
    • IanJohnston.com
Re: Automatic Solar battery charging timings based on forecast weather
« Reply #33 on: September 28, 2024, 11:37:27 am »
Yes there is a difference between the hours......thats what quite a bit of this is all about.

The benefit of charging the battery between cheap rate (half-price kWh) 2am - 5am in order to try and avoid losing all battery capacity the next day, even if that was at 10pm far outweigh taking a chance and not charging and ending up importing from the grid at the standard rate.
If I knew what the house load would be in advance then yes, more calcs could be done to see if the current capacity of the battery at 23:50 would be enough to last the next day could be done without charging at 2am - 5am.
In addition, I have to make sure I avoid at all costs importing from the grid between 4pm and 7pm where the rate is double.

Battery maintenance, I'll let the Solis inverter take care of that and at the moment I do not have an eye on trying to maintain battery longevity at all.......at least not till I have proved my model works, and then I can do some more work on the code.

Quite specific to my needs, and certainly not a 'fits all' method.

Thanks for the hints, tips and dialogue, it has certainly helped me understand possible issues with my own model and has added things to think about for the future.

Ian.
Ian Johnston - Original designer of the PDVS2mini || Author of the free WinGPIB app.
Website - www.ianjohnston.com
YT Channel (electronics repairs & projects): www.youtube.com/user/IanScottJohnston, Twitter (X): https://twitter.com/IanSJohnston
 

Offline max_torque

  • Super Contributor
  • ***
  • Posts: 1325
  • Country: gb
    • bitdynamics
Re: Automatic Solar battery charging timings based on forecast weather
« Reply #34 on: September 30, 2024, 06:23:19 pm »
Interesting thread!  I've been doing "automatic" solar forecasting in a basic way for about 6 years now with my home(brew) battery (ex-nissan leaf battery ). The method i use is really simple, i just let my control app crawl a number of free weather sites, parse in all the text off those sites and i simply count the number of times the folllowing words pop up "sun", "sunny", "cloud" "cloudy" "rain" "rainy" etc,  and then that uses some basic weighting to form an integer scrore for tomorrow and the week ahead!  Works suprisingly well, although with around 25 kWh of battery i can let my SoC varry a fair bit an still have space for solar charging and not worry about having to pull peak rate 'lecy off the grid !

For example, you can pull the following summary from the BBC Weather page:

"Latest forecast for UK
Last updated 17 minutes ago
Tonight

Tonight, central and eastern areas of England and Wales will see cloud and heavy rain linger for a time. By dawn, the rain will be confined to East Anglia. Drier with clear spells elsewhere. Windy.

Tuesday

Eastern England will see cloud and patchy rain linger for much of the day tomorrow. Strong winds at times, mainly in coasts. Further west and north, overall drier and sunnier, especially later on.

Outlook for Wednesday to Friday

On Wednesday, far south-eastern England will see a few showers linger through the day, these persistent in the coasts where it will remain windy. Elsewhere, a mix of cloud and sunny spells, these widespread in the afternoon. Dry and settled for all on Thursday. On Friday, Scotland will be cloudy with rain in the north-west. Dry and sunnier elsewhere."



The mentions of "cloud" and "rain" are clearly dominant, typical UK really   :-DD
 
The following users thanked this post: Siwastaja

Online IanJTopic starter

  • Supporter
  • ****
  • Posts: 1737
  • Country: scotland
  • Full time EE & Youtuber
    • IanJohnston.com
Re: Automatic Solar battery charging timings based on forecast weather
« Reply #35 on: September 30, 2024, 09:00:15 pm »
That’s how I thought I might have to do it till I realized hourly irradiance and cloud cover data could be pulled in.
Still tweaking the gain on my formula but so far it’s been surprising how accurate it’s been.

Ian
Ian Johnston - Original designer of the PDVS2mini || Author of the free WinGPIB app.
Website - www.ianjohnston.com
YT Channel (electronics repairs & projects): www.youtube.com/user/IanScottJohnston, Twitter (X): https://twitter.com/IanSJohnston
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf