Posts: 120
Threads: 24
Joined: Sep 2021
Reputation:
2
08.02.2022, 21:21
(This post was last modified: 08.02.2022, 21:25 by pioneersteffen .)
Hi @all,
I need your help please. I want to use the solcast API to get a forecast for my photovoltaic system. In detail I want to get the hourly forecasts for the next 24 hours and write them to group addresses.
Here is the link for the API documentation:
https://docs.solcast.com.au/?_ga=2.24121...i-pv-power
Here is my actual code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
require (
'json' )
https =
require 'ssl.https'
appid =
'XX'
lat =
'52.436225'
lon =
'10.42526'
azi =
'170'
hours =
'24'
format =
'json'
tilt =
'33'
capa =
'8'
url =
'https://api.solcast.com.au/world_pv_power/forecasts?latitude=%s&longitude=%s&hours=%s&format=%s&tilt=%s&capacity=%s&azimuth=%s&api_key=%s'
url =
string.format (
url ,
lat ,
lon ,
hours ,
format ,
tilt ,
capa ,
azi ,
appid )
res ,
code ,
headers ,
status =
ssl.https.request (
url )
log (
code ,
status ,
headers )
data =
json.pdecode (
res )
if not data then
alert (
'Error converting data' )
return
end
if not res then
alert (
'Error collecting data' )
return
end
data =
data.forecasts
grp.write (
'45/1/1' ,
data.pv_estimate )
I getting the following failure message:
Code:
1 2 3 4
Solcast 08.02.2022 22 :
01 :
35
User script :
36 :
attempt to index field 'pv_estimate' (
a nil value )
stack traceback :
User script :
36 :
in main chunk
Where is my failure? Can you help me please?
Many thanks for help!
Best Regards
Steffen
Posts: 8071
Threads: 43
Joined: Jun 2015
Reputation:
471
Try data[1].pv_estimate instead of data.pv_estimate .
Posts: 120
Threads: 24
Joined: Sep 2021
Reputation:
2
Hi admin,
many thanks for your help. The first line (21:30) is working now. Stupid question from my side: how to get access to the 22:00 forecast? Is this
data[2].pv_estimate?
Code:
1
string : {
"forecasts" :[{
"period_end" :
"2022-02-09T21:30:00.0000000Z" ,
"period" :
"PT30M" ,
"pv_estimate" :
0 },{
"period_end" :
"2022-02-09T22:00:00.0000000Z" ,
"period" :
"PT30M" ,
"pv_estimate" :
0 },{
"period_end" :
"2022-02-09T22:30:00.0000000Z" ,
"period" :
"PT30M" ,
"pv_estimate" :
0 },{
"period_end" :
"2022-02-09T23:00:00.0000000Z" ,
"period" :
"PT30M" ,
"pv_estimate" :
0 }, ……
Many thanks.
Best Regards
Steffen
Posts: 8071
Threads: 43
Joined: Jun 2015
Reputation:
471
Yes, data[2] , data[3] and so on.
Posts: 120
Threads: 24
Joined: Sep 2021
Reputation:
2
11.02.2022, 08:18
(This post was last modified: 11.02.2022, 08:20 by pioneersteffen .)
Hi Admin,
many thanks for your help!
I want to include a repeat loop to write the next 12h into group addresses. Unfortunately my actual code doesn’t work. Can you please give me a hint for the loop?
Many thanks for your help!
Best Regards
Steffen
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
require (
'json' )
https =
require 'ssl.https'
appid =
'XXX'
lat =
'52.436225'
lon =
'10.42526'
azi =
'170'
hours =
'24'
format =
'json'
tilt =
'33'
capa =
'8'
url =
'https://api.solcast.com.au/world_pv_power/forecasts?latitude=%s&longitude=%s&hours=%s&format=%s&tilt=%s&capacity=%s&azimuth=%s&api_key=%s'
url =
string.format (
url ,
lat ,
lon ,
hours ,
format ,
tilt ,
capa ,
azi ,
appid )
res ,
code ,
headers ,
status =
ssl.https.request (
url )
log (
code ,
status ,
headers ,
res )
data =
json.pdecode (
res )
if not data then
alert (
'Error converting data' )
return
end
if not res then
alert (
'Error collecting data' )
return
end
data =
data.forecasts
i =
1
repeat
grp.write (
'45/1/' ..
i ,
data [
i ].
pv_estimate )
i =
i +
1
until i >
24
Posts: 8071
Threads: 43
Joined: Jun 2015
Reputation:
471
The loop looks correct though a for loop over the whole data set makes more sense.
Code:
1 2 3 4
for i ,
row in ipairs (
data )
do
log (
i ,
row )
grp.write (
'45/1/' ..
i ,
row.pv_estimate )
end
Keep in mind that there's a limit on API calls so your script might not produce any results if you hit this limit.
Posts: 120
Threads: 24
Joined: Sep 2021
Reputation:
2
Sorry for the late reply.
@admin: Many thanks for your idea.
Here is my actual script which calculates the forecast for the next 24h, today and tomorrow. I hope somebody can use it to progress with it.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
require (
'json' )
https =
require 'ssl.https'
appid =
'xxx'
lat =
'52.436225'
lon =
'10.42526'
azi =
'170'
hours =
'48'
format =
'json'
tilt =
'33'
capa =
'8'
url =
'https://api.solcast.com.au/world_pv_power/forecasts?latitude=%s&longitude=%s&hours=%s&format=%s&tilt=%s&capacity=%s&azimuth=%s&api_key=%s'
url =
string.format (
url ,
lat ,
lon ,
hours ,
format ,
tilt ,
capa ,
azi ,
appid )
res ,
code ,
headers ,
status =
ssl.https.request (
url )
log (
code ,
status ,
headers )
data =
json.pdecode (
res )
if not data then
alert (
'Error converting data' )
return
end
if not res then
alert (
'Error collecting data' )
return
end
data =
data.forecasts
now =
os.date (
'*t' )
hour =
now.hour
i =
1
j =
2
k =
1
forecastsum =
0
rest_hour =
24 -
hour
repeat
forecast =
data [
i ].
pv_estimate +
data [
j ].
pv_estimate
forecastsum =
forecastsum +
forecast
if k ==
24 then
forecast_24h =
forecastsum
grp.write (
'43/4/25' ,
forecast_24h )
end
if rest_hour ==
k then
forecast_today =
forecastsum
grp.write (
'43/4/26' ,
forecast_today )
end
if (
rest_hour +
24 ) ==
k then
forecast_tomorrow =
forecastsum -
forecast_today
grp.write (
'43/4/27' ,
forecast_tomorrow )
end
if k <
25 then
grp.write (
'43/4/' ..
k ,
forecast )
end
i =
i +
2
j =
j +
2
k =
k +
1
until k > (
rest_hour +
24 )