-
Notifications
You must be signed in to change notification settings - Fork 0
/
Amount.SolCast.ips.php
424 lines (408 loc) · 14.1 KB
/
Amount.SolCast.ips.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
declare(strict_types=1);
################################################################################
# Script: Amount.SolCast.ips.php
# Version: 1.3.20230515
# Author: Heiko Wilknitz (@Pitti)
# Idee von STELE99 (2022)
#
# Forecast von PV Analage(n) berechnen via solcast.com
#
# ------------------------------ Installation ----------------------------------
#
# Dieses Skript richtet automatisch alle nötigen Objekte bei manueller
# Ausführung ein. Eine weitere manuelle Ausführung setzt alle benötigten Objekte
# wieder auf den Ausgangszustand.
#
# - Neues Skript erstellen
# - Diesen PHP-Code hineinkopieren
# - Abschnitt 'Konfiguration' mit eigenen Werten anpassen (__WWX austauschen)
# - Skript Abspeichern
# - Skript Ausführen
# - Visualisierung per Link auf entstandene Variablen erstellen
#
# ------------------------------ Changelog -------------------------------------
#
# 22.02.2023 - Initalversion (v1.0)
# 12.03.2023 - BuildTable,CalcTotal & ArchiveValue hinzugefügt (v1.1)
# 17.03.2023 - Fix für Archive Control (v1.2)
# 15.05.2023 - Keine Zwischenwerte mehr im Archiv
#
# ------------------------------ Konfiguration ---------------------------------
#
# Global Debug Output Flag
$DEBUG = false;
#
# PV Anlagen definiert durch
# token: API Key
# rid: Ressource ID der Anlage in Solcast
# graph: Wenn true wird via quickchart.io ein Graph erstellt
#
# Erste Anlage (meine ist auf der Garage, bitte den Namen auch anpassen)
$PVA['Garage'] = [
'token' => __WWX['SCC_TOKEN'], // (string) erstezen durch => '<API-KEY>'
'rid' => __WWX['SCC_RID'], // (string) erstezen durch => '<GUID>'
'graph' => true,
];
#
# Weitere Anlage
# $PVA['Haus'] = [
# 'token' => '<API-KEY>',
# 'rid' => <GUID>,
# 'graph' => true,
# ];
#
################################################################################
#
# Requires include of the global function script via autoload (__autoload.php)
# or direct in the script (uncomment next line)!
# require_once(IPS_GetKernelDir()."scripts".DIRECTORY_SEPARATOR.'System.Functions.ips.php');
# You can download it from here https://github.com/wilkware/ips-scripts
#
defined('WWX_FUNCTIONS') || die('Global function library not available!');
require_once IPS_GetKernelDir() . 'scripts' . DIRECTORY_SEPARATOR . 'System.QuickChart.ips.php';
// INSTALLATION
if ($_IPS['SENDER'] == 'Execute') {
// Event 1 mal am Tag
$midnight = mktime(0, 4, 16);
CreateEventByName($_IPS['SELF'], 'UpdateMidnight', $midnight);
// Event aller 6 Stunde von 03:00 bis 22:00 Uhr
$from = mktime(2, 54, 16);
$to = mktime(22, 0, 0);
CreateEventByNameFromTo($_IPS['SELF'], 'UpdateDaily', 3, 1, $from, $to);
foreach ($PVA as $name => $plant) {
// Kategorie erzeugen
$cid = CreateCategoryByName($_IPS['SELF'], $name);
$vid = CreateVariableByName($cid, 'Daten', 3, 0);
SetValueString($vid, '[]');
$vid = CreateVariableByName($cid, 'Tabellarischer Verlauf', 3, 1, 'Database', '~HTMLBox');
$vid = CreateVariableByName($cid, 'Graphischer Verlauf', 3, 2, 'Graph', '~HTMLBox');
$vid = CreateVariableByName($cid, 'PV heute (normal)', 2, 10, '', '~Electricity');
// Archivierung aktivieren (Typ: Zähler)
RegisterArchive($vid, false);
$vid = CreateVariableByName($cid, 'PV heute (besser)', 2, 11, '', '~Electricity');
// Archivierung aktivieren (Typ: Zähler)
RegisterArchive($vid, false);
$vid = CreateVariableByName($cid, 'PV heute (schlechter)', 2, 12, '', '~Electricity');
// Archivierung aktivieren (Typ: Zähler)
RegisterArchive($vid, false);
$vid = CreateVariableByName($cid, 'PV morgen (normal)', 2, 20, '', '~Electricity');
$vid = CreateVariableByName($cid, 'PV morgen (besser)', 2, 21, '', '~Electricity');
$vid = CreateVariableByName($cid, 'PV morgen (schlechter)', 2, 22, '', '~Electricity');
}
}
// WEBFRONT
elseif ($_IPS['SENDER'] == 'WebFront') {
// Benutzer hat etwas geändert!
}
// VARIABLENAENDERUNG
elseif ($_IPS['SENDER'] == 'Variable') {
// ToDO?
}
// TIMER EVENT
elseif ($_IPS['SENDER'] == 'TimerEvent') {
$event = IPS_GetName($_IPS['EVENT']);
// pro Anlage werden die Daten auswerten
foreach ($PVA as $name => $plant) {
UpdateData($name, $plant, ($event == 'UpdateMidnight'));
}
}
#----------------------------------- Functions ---------------------------------
// Für die Anlagen den Forecast ermitteln und tabellarisch und graphisch aufbereiten
function UpdateData($name, $plant, $reset = false)
{
// Kategorie erzeugen oder lesen
$cid = CreateCategoryByName($_IPS['SELF'], $name);
// Daten holen
$data = RequestData($plant);
// Daten normalisieren
$fore = ForecastData($data);
// Gespeicherte Daten einlesen
$did = CreateVariableByName($cid, 'Daten', 3);
$data = json_decode(GetValue($did), true);
// Variablen
$hnid = CreateVariableByName($cid, 'PV heute (normal)', 2);
$hbid = CreateVariableByName($cid, 'PV heute (besser)', 2);
$hsid = CreateVariableByName($cid, 'PV heute (schlechter)', 2);
$mnid = CreateVariableByName($cid, 'PV morgen (normal)', 2);
$mbid = CreateVariableByName($cid, 'PV morgen (besser)', 2);
$msid = CreateVariableByName($cid, 'PV morgen (schlechter)', 2);
// Daten zurücksetzen oder zusammenführen
if ($reset) {
$data = $fore;
// Heute Variablen
SetValueFloat($hnid, 0);
SetValueFloat($hbid, 0);
SetValueFloat($hsid, 0);
IPS_Sleep(1000);
// Morgen Variablen
SetValueFloat($mnid, 0);
SetValueFloat($mbid, 0);
SetValueFloat($msid, 0);
IPS_Sleep(1000);
} else {
foreach ($fore as $day => $hours) {
foreach ($hours as $hour => $values) {
$data[$day][$hour] = $values;
}
}
}
// Data wieder speichern
$str = json_encode($data);
SetValueString($did, $str);
// Summen ermitteln für Heute (Archive) und Morgen
$values = CalcTotal($data);
// Heute
ArchiveValue($hnid, $values[0]['norm']);
ArchiveValue($hbid, $values[0]['more']);
ArchiveValue($hsid, $values[0]['poor']);
// Morgen
SetValueFloat($mnid, $values[1]['norm']);
SetValueFloat($mbid, $values[1]['more']);
SetValueFloat($msid, $values[1]['poor']);
// HTML Table bauen
$html = BuildTable($data);
$vid = CreateVariableByName($cid, 'Tabellarischer Verlauf', 3);
SetValueString($vid, $html);
// SVG Chart plotten
$svg = DrawChart($data);
$svg = str_replace(['1024pt', '325pt'], '100%', $svg);
$vid = CreateVariableByName($cid, 'Graphischer Verlauf', 3);
SetValueString($vid, $svg);
}
// Von SolCast gelieferten Daten auf Tages- und stundenbasis Normalisieren
function ForecastData($data)
{
$fd = [];
foreach ($data['forecasts'] as $o) {
$hour = date('H', strtotime($o['period_end']));
$day = date('z', strtotime($o['period_end'])) - date('z');
if (isset($fd[$day][$hour])) {
// echo "Add " . $day . ' - ' . $hour . PHP_EOL;
$fd[$day][$hour]['norm'] = ($fd[$day][$hour]['norm'] + $o['pv_estimate']) / 2;
$fd[$day][$hour]['poor'] = ($fd[$day][$hour]['poor'] + $o['pv_estimate10']) / 2;
$fd[$day][$hour]['more'] = ($fd[$day][$hour]['more'] + $o['pv_estimate90']) / 2;
} else {
// echo "New " . $day . ' - ' . $hour . PHP_EOL;
$fd[$day][$hour] = [
'norm' => $o['pv_estimate'],
'poor' => $o['pv_estimate10'],
'more' => $o['pv_estimate90'],
];
}
}
return $fd;
}
// Daten gezielt ins Archive schreiben (Zähler -> keine negativen Werte)
function ArchiveValue($vid, $value)
{
$lv = GetValueFloat($vid);
if (($lv > 0) && ($lv != $value)) {
//Den letzten Wert, der in der Datenbank gespeichert wurde, holen
$aid = IPS_GetInstanceListByModuleID(ExtractGuid('Archive Control'))[0];
$last = AC_GetLoggedValues($aid, $vid, 0, 0, 1)[0];
AC_DeleteVariableData($aid, $vid, $last['TimeStamp'], 0);
IPS_Sleep(1000);
SetValueFloat($vid, $value);
AC_ReAggregateVariable($aid, $vid);
IPS_Sleep(1000);
} else {
SetValueFloat($vid, $value);
}
}
// Daten bei SolCast.com anfragen
function RequestData($plant, $forecast = true)
{
// Which data
$url = 'https://api.solcast.com.au/rooftop_sites/' . $plant['rid'];
if ($forecast) {
$url .= '/forecasts?format=json';
} else {
$url .= '/estimated_actuals?format=json';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $plant['token'],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new ErrorException(curl_error($ch));
}
curl_close($ch);
// $response = $SO_JSON;
$data = json_decode($response, true);
// print_r($data);
return $data;
}
// Tages-Summen bilden und zurückliefern
function CalcTotal($data)
{
$total = [];
foreach ($data as $day => $hours) {
$poor = 0;
$norm = 0;
$more = 0;
foreach ($hours as $hour => $values) {
$poor += $values['poor'];
$norm += $values['norm'];
$more += $values['more'];
}
$total[$day]['poor'] = $poor;
$total[$day]['norm'] = $norm;
$total[$day]['more'] = $more;
}
return $total;
}
// HTML-Tabelle pro Tag bauen
function BuildTable($data)
{
// cols
$cols = 0;
foreach ($data as $day => $hours) {
$count = 0;
foreach ($hours as $hour => $values) {
if (($values['poor'] != 0) || ($values['norm'] != 0) || ($values['more'] != 0)) {
$count++;
}
}
$cols = max($cols, $count);
}
// html
$html = '';
$html .= '<table class="wwx">';
foreach ($data as $day => $hours) {
$count = 0;
$th = '<thead class="orange"><th><center>' . date('d.m.Y', strtotime('today+' . $day . 'day')) . '</center></th>';
$tr1 = '<tr><td>kW/h (bewölkt)</td>';
$tr2 = '<tr><td>kW/h (normal)</td>';
$tr3 = '<tr><td>kW/h (sonnig)</td>';
foreach ($hours as $hour => $values) {
if (($values['poor'] != 0) || ($values['norm'] != 0) || ($values['more'] != 0)) {
$th .= '<th>' . $hour . ':00</th>';
$tr1 .= '<td>' . round(floatval($values['poor']), 4) . '</td>';
$tr2 .= '<td>' . round(floatval($values['norm']), 4) . '</td>';
$tr3 .= '<td>' . round(floatval($values['more']), 4) . '</td>';
$count++;
}
}
for ($i = $count; $i < $cols; $i++) {
$th .= '<th>-</th>';
$tr1 .= '<td>-</td>';
$tr2 .= '<td>-</td>';
$tr3 .= '<td>-</td>';
}
$html .= $th . '</thead>';
$html .= $tr1 . '</tr>';
$html .= $tr2 . '</tr>';
$html .= $tr3 . '</tr>';
}
$html .= '</table>';
return $html;
}
// Daten als Chart plotten
function DrawChart($data)
{
$axis_x = [];
$axis_yn = [];
$axis_yp = [];
$axis_ym = [];
foreach ($data as $day => $hours) {
foreach ($hours as $hour => $value) {
if (($value['norm'] != 0) && ($value['poor'] != 0) && ($value['more'] != 0)) {
$axis_x[] = '\'' . $hour . ':00\'';
$axis_yn[] = intval($value['norm'] * 1000);
$axis_yp[] = intval($value['poor'] * 1000);
$axis_ym[] = intval($value['more'] * 1000);
}
}
}
// new chart object
$chart = new QuickChart(['width' => 1024, 'height' => 325, 'format' => 'svg']);
// chart config
$chart->setConfig("{
type: 'line',
data: {
labels: [" . implode(',', $axis_x) . "],
datasets: [
{
label: 'Sonniger',
backgroundColor: 'rgba(255, 125, 0, 0.1)',
borderColor: 'rgb(255, 125, 0)',
borderWidth: 1,
lineTension: 0.4,
pointRadius: 1,
fill: true,
data: [" . implode(',', $axis_ym) . "]
},
{
label: 'Normal',
backgroundColor: 'rgba(255, 255, 0, 0.1)',
borderColor: 'rgb(255, 255, 0)',
borderWidth: 1,
lineTension: 0.4,
pointRadius: 1,
fill: true,
data: [" . implode(',', $axis_yn) . "]
},
{
label: 'Bewölkter',
backgroundColor: 'rgba(0, 255, 255, 0.1)',
borderColor: 'rgb(0, 255, 255)',
borderWidth: 1,
lineTension: 0.4,
pointRadius: 1,
fill: true,
data: [" . implode(',', $axis_yp) . "]
}
]
},
options: {
legend: {
labels: {
fontColor: 'rgb(255, 255, 255)'
}
},
scales: {
xAxes: [{
ticks: {
fontColor: '#fff',
},
gridLines: {
color: 'rgba(211, 211, 211, 0.2)',
borderDash: [5,5]
},
scaleLabel: {
fontColor: '#fff',
display: false,
labelString: 'Uhrzeit'
}
}],
yAxes: [{
ticks: {
fontColor: '#fff'
},
gridLines: {
color: 'rgba(211, 211, 211, 0.2)',
borderDash: [5,5]
},
scaleLabel: {
fontColor: '#fff',
display: true,
labelString: 'Leistung (Watt)'
}
}]
}
}
}");
return $chart->toBinary();
}
################################################################################