This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
get_solcast_data_1.2.4.js
93 lines (82 loc) · 3.15 KB
/
get_solcast_data_1.2.4.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
MIT License - see LICENSE.md
Copyright (c) [2020] [Matthias Boettger <[email protected]>]
*/
var site_id = "XXXX-XXXX-XXXX-XXXX",
key_id = "yyyyyyyyyyyyyyyyyyyyyyyyyyy",
hours = 24;
const url = "https://api.solcast.com.au/rooftop_sites/" + site_id +"/forecasts?format=json&api_key="+ key_id;
//console.log(url)
function requestData() {
const options = {
url: url,
method: 'GET'
};
request(options, (error, response, body) => {
if(error) return console.log(error);
if(response.statusCode == 200) {
let array = JSON.parse(body).forecasts;
var list = [];
for(let i = 0; i < array.length; i++) {
var endtime = Date.parse(array[i].period_end);
var time = new Date(endtime-1800000);
var readpwr = array[i].pv_estimate;
var readpwr90 = array[i].pv_estimate90;
list[i] = {};
list[i].time = time/1000;
list[i].watt = Math.round(readpwr * 1000);
list[i].watt90 = Math.round(readpwr90 * 1000);
};
for(let a = 0; a < (hours*2); a++) {
let stateBaseName = "electricity.pvforecast." + a + ".";
let start = new Date((list[a].time)*1000);
let end = new Date(((list[a].time)*1000)+1800000)
var options = { hour12: false, hour: '2-digit', minute:'2-digit'};
let startTime = start.toLocaleTimeString('de-DE', options);
let endTime = end.toLocaleTimeString('de-DE', options);
setState(stateBaseName + "startTime", startTime);
setState(stateBaseName + "endTime", endTime);
setState(stateBaseName + "power", list[a].watt);
setState(stateBaseName + "power90", list[a].watt90);
}
}
});
}
function create_datapoints(){
for(let a = 0; a < (hours*2); a++) {
let stateBaseName = "electricity.pvforecast." + a + ".";
createState(stateBaseName + "startTime", "", {
read: true,
write: true,
name: "Uhrzeit",
type: "string",
def: false
});
createState(stateBaseName + "endTime", "", {
read: true,
write: true,
name: "Uhrzeit",
type: "string",
def: false
});
createState(stateBaseName + "power", 0, {
read: true,
write: true,
name: "Power",
type: "number",
def: 0
});
createState(stateBaseName + "power90", 0, {
read: true,
write: true,
name: "Power",
type: "number",
def: 0
});
}
}
create_datapoints();
// 10 freie API Request pro Tag
schedule("0 3,5,7,9,11,13,15,17,19,21 * * *", function () {
requestData();
});