-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoymiles-nulleinspeisung.js
117 lines (97 loc) · 2.91 KB
/
hoymiles-nulleinspeisung.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const inverters = [
{
name: "Inverter 1",
maxPower: 1600,
newLimit: 100,
oldLimit: 100,
grid: 500,
},
{
name: "Inverter 2",
maxPower: 1600,
newLimit: 100,
oldLimit: 100,
grid: 500,
},
{
name: "Inverter 3",
maxPower: 1600,
newLimit: 100,
oldLimit: 100,
grid: 833,
},
]
const powerPhaseA = -290;
const powerPhaseB = 0;
const powerPhaseC = 0;
const limitMin = 5;
const limitMax = 100;
async function check() {
const powerSum = powerPhaseA + powerPhaseB + powerPhaseC;
const gridSum = await gridSumme();
const neededPower = await gridSum + powerSum;
const maxInverterSumme = await maxInverterSum();
const limitSum = await percent(neededPower, maxInverterSumme, 100);
console.log(`limitSum: ${limitSum} %, neededPower: ${neededPower} W, gridSum: ${gridSum} W, maxInverterPower: ${maxInverterSumme}`);
if (powerSum >= -300 && powerSum <= 0 || ((neededPower - gridSum) < 100 && powerSum > 0)) {
console.log(`Nichts zu tun. Erzeugte Leistung liegt im Bereich der gemessen Leistung`);
await reload();
return;
}
for (let i = 0; i < inverters.length; i++) {
const inverter = inverters[i];
const factor = await percent(inverter.grid, gridSum, 100);
const newlimitPower = await round(neededPower * factor / 100);
const newLimitPercent = await percentAndLimit(newlimitPower, inverter.maxPower, 100);
//console.log(`${inverter.name}: Factor = ${factor}, newLimitPercent = ${newLimitPercent} %, new maximumPower = ${newlimitPower} W`);
await setLimit(inverter, newLimitPercent);
}
await reload();
}
async function setLimit(inverter, limit) {
if (inverter.oldLimit === limit) {
console.log(`Limit von ${inverter.name} ist gleich altem Limit. Keine Begrenzung`);
return;
}
inverter.oldLimit = limit;
console.log(`Neues Limit ${limit} % für ${inverter.name} gesetzt.`);
}
async function gridSumme() {
let result = 0;
for (let i = 0; i < inverters.length; i++) {
result += inverters[i].grid;
}
return result;
}
async function maxInverterSum() {
let result = 0;
for (let i = 0; i < inverters.length; i++) {
result += inverters[i].maxPower;
}
return result;
}
async function limit(limit) {
return Math.min(limitMax, Math.max(limitMin, limit));
}
async function round(value) {
return Math.round(value * 100) / 100;
}
async function percent(power1, power2, factor) {
let result = power1 / power2 * factor;
result = await round(result);
return result;
}
async function percentAndLimit(power1, power2, factor) {
let result = power1 / power2 * factor;
result = await round(result);
result = await limit(result);
return result;
}
async function reload() {
for (let i = 5; i > 0; i--) {
//console.log(`Countdown: ${i} Sekunden verbleiben...`);
await new Promise(resolve => setTimeout(resolve, 1000)); // 1 Sekunde Verzögerung
}
//await check();
}
check();