-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolcast.groovy
152 lines (138 loc) · 5.22 KB
/
solcast.groovy
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
metadata {
definition(
name: "Solcast",
namespace: "ke7lvb",
author: "Ryan Lundell",
importUrl: "https://raw.githubusercontent.com/ke7lvb/Solcast/main/solcast.groovy",
) {
capability "Refresh"
capability "EnergyMeter"
capability "PowerMeter"
attribute "energy", "number"
attribute "power", "number"
attribute "1 Hour Estimate", "number"
attribute "one_hour_estimate", "number"
attribute "24 Hour Peak Production", "number"
attribute "48 Hour Peak Production", "number"
attribute "72 Hour Peak Production", "number"
attribute "48 Hour Estimate", "number"
attribute "72 Hour Estimate", "number"
attribute "lastUpdate", "string"
}
preferences {
input name: "logEnable", type: "bool", title: "Enable Info logging", defaultValue: true, description: ""
input name: "debugLog", type: "bool", title: "Enable Debug logging", defaultValue: true, description: ""
input name: "api_key", type: "string", title: "API Key", required: true
input name: "resource_id", type: "string", title: "Site Resource ID", required: true
input("refresh_interval", "enum", title: "How often to refresh the battery data", options: [
0: "Do NOT update",
30: "30 minutes",
1: "1 Hour",
3: "3 Hours",
8: "8 Hours",
12: "12 Hours",
24: "Daily",
], required: true, defaultValue: "3")
}
}
def version() {
return "1.0.6"
}
def installed() {
if (logEnable) log.info "Driver installed"
state.version = version()
}
def uninstalled() {
unschedule(refresh)
if (logEnable) log.info "Driver uninstalled"
}
def updated() {
if (logEnable) log.info "Settings updated"
if (settings.refresh_interval != "0") {
//refresh()
if (settings.refresh_interval == "24") {
schedule("51 7 2 ? * * *", refresh, [overwrite: true])
} else if(settings.refresh_interval == "30"){
schedule("51 */30 * ? * *", refresh, [overwrite: true])
} else {
schedule("51 7 */${settings.refresh_interval} ? * * *", refresh, [overwrite: true])
}
}else{
unschedule(refresh)
}
state.version = version()
}
import groovy.json.JsonOutput;
def refresh() {
outputTZ = TimeZone.getTimeZone('UTC')
host = "https://api.solcast.com.au/rooftop_sites/${resource_id}/forecasts?format=json&api_key=${api_key}&hours=72"
if(debugLog) log.debug host
forecasts = httpGet([uri: host]) {resp -> def respData = resp.data.forecasts}
if(debugLog) log.debug JsonOutput.toJson(forecasts)
def next1 =0;
def next24 = 0;
def next24High = 0;
def next24Low = 0;
def next48 = 0;
def next48High = 0;
def next48Low = 0;
def next72 = 0;
def next72High = 0;
def next72Low = 0;
def size = forecasts.size();
for(int x=0; x<size; x++){
if(debugLog) log.debug forecasts[x]
pv_estimate = forecasts[x].pv_estimate/2
pv_estimate_high = forecasts[x].pv_estimate90/2
pv_estimate_low = forecasts[x].pv_estimate10/2
if(x < 2){
next1 = next1 + pv_estimate
}
if(x < 48){
next24 = next24 + pv_estimate
next24High = next24High + pv_estimate_high
next24Low = next24Low + pv_estimate_low
}
if(x < 96){
next48 = next48 + pv_estimate
next48High = next48High + pv_estimate_high
next48Low = next48Low + pv_estimate_low
}
next72 = next72 + pv_estimate
next72High = next72High + pv_estimate_high
next72Low = next72Low + pv_estimate_low
}
tomorrow = new Date().next().format("yyyy-MM-dd'T'HH:mm:ss'Z'",outputTZ)
forecast24 = forecasts.findAll { it.period_end < tomorrow}
if(debugLog) log.info forecast24
peak24 = forecast24.max() { it.pv_estimate }
twoDays = new Date().plus(2).format("yyyy-MM-dd'T'HH:mm:ss'Z'",outputTZ)
forecast48 = forecasts.findAll { it.period_end < twoDays}
peak48 = forecast48.max() { it.pv_estimate }
peak72 = forecasts.max() { it.pv_estimate }
state.peak24 = peak24.pv_estimate
sendEvent(name: "24 Hour Peak Production", value: state.peak24)
state.peak48 = peak48.pv_estimate
sendEvent(name: "48 Hour Peak Production", value: state.peak48)
state.peak72 = peak72.pv_estimate
sendEvent(name: "72 Hour Peak Production", value: state.peak72)
state.next1 = next1*1000
sendEvent(name: "1 Hour Estimate", value: next1*1000)
sendEvent(name: "one_hour_estimate", value: next1*1000)
state.next24 = next24
sendEvent(name: "energy", value: next24)
sendEvent(name: "power", value: next24*1000)
state.next24High = next24High
state.next24Low = next24Low
state.next48 = next48
sendEvent(name: "48 Hour Estimate", value: next48)
state.next48High = next48High
state.next48Low =next48Low
state.next72 = next72
sendEvent(name: "72 Hour Estimate", value: next72)
state.next72High = next72High
state.next72Low = next72Low
now = new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'")
state.lastUpdate = timeToday(now)
sendEvent(name: "lastUpdate", value: state.lastUpdate)
}