-
Notifications
You must be signed in to change notification settings - Fork 0
/
heating_element.js
321 lines (283 loc) · 8.9 KB
/
heating_element.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
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
let CONFIG = {
em_ip: "192.168.100.100",
key_total: "EM_TOTAL",
key_total_ret: "EM_TOTAL_RET",
key_total_store_datetime: "EM_STORETIME",
em_measurement_period: 15, // tuntinetotus 15min osissa
key_boiler_temperature: "BOILER_TEMPERATURE",
key_boiler_store_datetime: "BOILER_STORETIME",
power_limit: 100, // powerlimit 100Wh,
anturi_id_ylakierto: "100",
anturi_offset: 8.0,
temp_min: 50,
temp_min_activetime: 55,
temp_max_solar: 75,
temp_heating_increase: 2,
boiler_stop_temperature: 40,
temp_boiler_increase: 10,
debug: false,
dryrun: false,
};
function debugPrint(line) {
if (CONFIG.debug) {
print(line);
}
}
function setTemperatureComponent() {
Shelly.call(
"Temperature.SetConfig",
{
id: CONFIG.anturi_id_ylakierto,
config: {
id: CONFIG.anturi_id_ylakierto,
name: "yläkierto lämpötila",
report_thr_C: 1.0,
offset_C: CONFIG.anturi_offset
}
},
function (response, error_code, error_message) { }
);
};
//
// A remote Shelly abstraction Call an RPC method on the remote Shelly
let RemoteShelly = {
_cb: function (result, error_code, error_message, callbackObject) {
// connection to remote failed, callback with null values
if (result === undefined) {
callbackObject.responseCallback(null, error_code, error_message);
} else {
let rpcResult = JSON.parse(result.body);
let rpcCode = result.code;
let rpcMessage = result.message;
callbackObject.responseCallback(rpcResult, rpcCode, rpcMessage);
}
},
composeEndpoint: function (method) {
return "http://" + this.address + "/rpc/" + method;
},
call: function (rpc, data, callbackObject) {
this.responseCallback = callbackObject;
let postData = {
url: this.composeEndpoint(rpc),
body: data,
};
Shelly.call("HTTP.POST", postData, RemoteShelly._cb, this);
},
getInstance: function (address) {
let rs = Object.create(this);
// remove static method
rs.getInstance = null;
rs.address = address;
return rs;
},
responseCallback: function (rpcResult, rpcCode, rpcMessage) {
debugPrint("This is default responseCallback that should override!");
}
};
function switchVastus(activate) {
debugPrint("activate vastus " + activate);
if (CONFIG.dryrun) {
return;
}
Shelly.call(
"Switch.Set",
{ id: 0, on: activate },
function (response, error_code, error_message) { }
);
};
let Heater = (function () {
let upCirculationTemperature;
let emTotalAct;
let emTotalActRet;
let prevEmTotalAct;
let prevEmTotalActRet;
let prevEmDatetime;
let solarPowerStatus;
let boilerTemperature;
let boilerDatetime;
// calculate energy meter power balance from recent measurement period
// return values
// null: cannot calculate value
// positive value: used more than produced
// negative value: produced more than used
function emPowerUsageFromMeasurementPeriod(timeNow) {
let powerSummary = null;
if (emTotalAct == null || prevEmTotalAct == null) {
debugPrint("emPower Error: no previous or current data!");
return powerSummary;
}
let diffsec = (timeNow.valueOf() - prevEmDatetime.valueOf()) / 1000;
// timestamp is older than 30 min (2* em measurement period), em data is outdated
if (diffsec > (60 * CONFIG.em_measurement_period * 2)) {
debugPrint("emPower Error: previous data outdated!");
return powerSummary;
}
// calculate is power used or produced
let powerUsed = emTotalAct - prevEmTotalAct;
let powerRet = emTotalActRet - prevEmTotalActRet;
powerSummary = powerUsed - powerRet;
return powerSummary;
};
// return values
// -2 : energy meter previous values are not exixts on outdated, cannot use to switch heater
// -1 : not enough power produced on measurement period
// 0 : turn off, used more power than produced
// 1,2 : turn on, produced more power than used
function refreshSolarPowerStatus(timeNow) {
// get power balance
let powerSummary = emPowerUsageFromMeasurementPeriod(timeNow);
if (powerSummary == null) {
debugPrint("refreshSolarPowerStatus Error: no power usage from recent measurement period!");
// if previous check has been turn on, should set to stop first
solarPowerStatus = (solarPowerStatus > 0) ? 0 : -2;
}
// no solar power produced enough to calculate
else if ((emTotalActRet - prevEmTotalActRet) < CONFIG.power_limit) {
// if previous status has been turn on, should set to stop first
// use slow mode, case measurement period has just changed
solarPowerStatus = (solarPowerStatus > 0) ? solarPowerStatus - 1 : -1;
}
// used more power than produced from previous netto leveling
else if (powerSummary > CONFIG.power_limit) {
// use slow mode, case measurement period has just changed
solarPowerStatus = (solarPowerStatus > 0) ? solarPowerStatus - 1 : 0;
}
// produced more power than used from previous netto leveling
else if (powerSummary < (CONFIG.power_limit * -1)) {
solarPowerStatus = 2;
}
debugPrint("action solarPowerStatus : " + solarPowerStatus);
// return last state
return solarPowerStatus;
};
function getTempMin(timeNow, hour){
// use higher temperature active time
let min_temp = (hour > 16 && hour < 21) ? CONFIG.temp_min_activetime : CONFIG.temp_min;
// check is boiler stopped, set limit higher if boiler is stopped
let diffsec = (timeNow.valueOf() - boilerDatetime.valueOf()) / 1000;
// timestamp is older than 60 min , boiler temperature data is outdated
if (diffsec > (60 * 60)) {
debugPrint("boiler Temperature Error: temperature data outdated!");
min_temp = min_temp + CONFIG.temp_boiler_increase;
}
else if (boilerTemperature < CONFIG.boiler_stop_temperature) {
debugPrint("boiler is stopped!");
min_temp = min_temp + CONFIG.temp_boiler_increase;
}
return min_temp;
};
function action() {
let now = Date(Date.now());
let hour = now.getHours();
let solarStatus = refreshSolarPowerStatus(now);
// use higher temperature active time
let min_temp = getTempMin(now, hour);
// own produced solar power set max limit to higher
let max_temp = (solarStatus > 0) ? CONFIG.temp_max_solar : min_temp + CONFIG.temp_heating_increase;
debugPrint("action upCirculationTemperature : " + upCirculationTemperature);
debugPrint("action min_temp : " + min_temp);
debugPrint("action max_temp : " + max_temp);
// under minimum limit, set heater on
if (upCirculationTemperature < min_temp) {
switchVastus(true);
}
// over maximum limit, set heater off
else if (upCirculationTemperature > max_temp) {
switchVastus(false);
}
// use solar power, set heater on
else if (solarStatus > 0) {
switchVastus(true);
}
// no produced solar power, set heater off
else if (solarStatus == 0) {
switchVastus(false);
}
};
function previousEmTotal() {
Shelly.call(
"KVS.GetMany",
{ id: 0 },
function (result, error_code, error_message, user_data) {
prevEmTotalAct = result.items.EM_TOTAL.value;
prevEmTotalActRet = result.items.EM_TOTAL_RET.value;
prevEmDatetime = Date(result.items.EM_STORETIME.value);
boilerTemperature = result.items.BOILER_TEMPERATURE.value;
boilerDatetime = Date(result.items.BOILER_STORETIME.value);
action();
},
null
);
};
function getEmTotal() {
let emShelly = RemoteShelly.getInstance(CONFIG.em_ip);
emShelly.call(
"EMData.GetStatus",
{ id: 0 },
Heater.getEmTotalCallback
);
};
function callGetTemperature(id) {
Shelly.call(
"Temperature.GetStatus",
{ "id": id },
function (result, error_code, error_message, user_data) {
upCirculationTemperature = result.tC;
// debugPrint(result);
// debugPrint(temp);
getEmTotal()
},
null
);
};
return { // public interface
init: function () {
solarPowerStatus = -2;
},
refresh: function () {
callGetTemperature(CONFIG.anturi_id_ylakierto);
},
getEmTotalCallback: function (result, error_code, error_message) {
if (result === null) {
emTotalAct = null;
emTotalActRet = null;
} else {
emTotalAct = result.total_act;
emTotalActRet = result.total_act_ret;
}
// debugPrint(result);
// debugPrint(LATEST_DATA);
// debugPrint(user_data);
previousEmTotal();
},
};
})();
Heater.init();
Shelly.addEventHandler(
function (event, ud) {
if (!event || !event.info) {
return;
}
let event_name = event.info.event;
// debugPrint(event_name);
if (event_name === "switchHeater") {
Heater.refresh();
}
// temperature has changed
else if (event_name === "temperature_change") {
Heater.refresh();
}
},
null
);
setTemperatureComponent();
// check heater status every minute
Timer.set(
1000 * 60, // msec, 1min check
true,
function (user_data) {
Shelly.emitEvent("switchHeater", {});
},
null
)
Shelly.emitEvent("switchHeater", {});