-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcirculation_pump.js
214 lines (188 loc) · 5.35 KB
/
circulation_pump.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
// 100 anturi varaaja alakierto
// 101 anturi pannu
let CONFIG = {
anturi_boiler_id: "101",
anturi_boiler_name: "Pannu lämpötila",
anturi_downCirculation_id: "100",
anturi_downCirculation_name: "Varaaja alakierto lämpötila",
anturi_downCirculation_offset: 8.0,
boiler_max_temperature: 75, // pannun max lämpö, ylitys -> kierto päälle
boiler_min_temperature: 40, // talviajan alaraja
downCirculation_max_temperature: 65, // kierron max lämpö -> ylitys kierto pois päältä
upCirculation_max_temperature: 72, // kierron ylä max lämpö -> ylitys kierto päälle päältä. case vastus lämmittää
debug: false,
dryrun: false,
}
function debugPrint(line) {
if (CONFIG.debug) {
print(line);
}
}
function switchPump(activate) {
debugPrint("activate pump " + activate);
if (CONFIG.dryrun) {
return;
}
Shelly.call(
"Switch.Set",
{ id: 0, on: activate },
function (response, error_code, error_message) { }
);
};
function setTemperatureComponent() {
Shelly.call(
"Temperature.SetConfig",
{
id: CONFIG.anturi_downCirculation_id,
config: {
id: CONFIG.anturi_downCirculation_id,
name: CONFIG.anturi_downCirculation_name,
report_thr_C: 1.0,
offset_C: CONFIG.anturi_downCirculation_offset
}
},
function (response, error_code, error_message) { }
);
Shelly.call(
"Temperature.SetConfig",
{
id: CONFIG.anturi_boiler_id,
config: {
id: CONFIG.anturi_boiler_id,
name: CONFIG.anturi_boiler_name,
report_thr_C: 1.0
}
},
function (response, error_code, error_message) { }
);
};
let TemperatureHandler = (function () {
let boilerTemperature;
let downCirculationTemperature;
let upCirculationTemperature; // -1 if outdated
let upCirculationDatetime;
function winterTime() {
let now = Date(Date.now());
let month = now.getMonth(); // months start from 0
debugPrint("month is " + month);
if (month < 3 || month > 8) {
debugPrint("WINTERTIME !");
return true;
}
return false;
};
function setPump() {
debugPrint("Pannu lämpötila: " + boilerTemperature);
debugPrint("Kierto lämpötila: " + downCirculationTemperature);
// pannu max lämpö ylitetty
if (boilerTemperature > CONFIG.boiler_max_temperature) {
debugPrint("Rule 1");
switchPump(true);
}
// kierron ylälämpö ylitetty, vastus lämmittää
else if (upCirculationTemperature > CONFIG.upCirculation_max_temperature && upCirculationTemperature > boilerTemperature) {
debugPrint("Rule 2");
switchPump(true);
}
// pannun minilämpö alitettu (talvikuukaudet)
else if (winterTime() && boilerTemperature < CONFIG.boiler_min_temperature) {
debugPrint("Rule 3");
switchPump(true);
}
// pannun lämpötila alle kierron
else if (boilerTemperature < downCirculationTemperature) {
debugPrint("Rule 4");
switchPump(false);
}
// kierron lämpö yli max arvon
else if (downCirculationTemperature > CONFIG.downCirculation_max_temperature) {
debugPrint("Rule 5");
switchPump(false);
}
// pannun lämpötila yli kierron lämpötilan
else if (boilerTemperature > downCirculationTemperature) {
debugPrint("Rule 6");
switchPump(true);
};
};
function readKvs() {
Shelly.call(
"KVS.GetMany",
{ id: 0 },
function (result, error_code, error_message, user_data) {
upCirculationTemperature = result.items.UP_CIRCULATION_TEMPERATURE.value;
upCirculationDatetime = Date(result.items.UP_CIRCULATION_STORETIME.value);
let now = Date(Date.now());
let diffsec = (now.valueOf() - upCirculationDatetime.valueOf()) / 1000;
// timestamp is older than 30 min (2* em measurement period), em data is outdated
if (diffsec > (60 * 5)) {
debugPrint("upCirculation Error: data outdated!");
upCirculationTemperature = -1;
}
setPump();
},
null
);
};
function readTemperatureAlakierto() {
Shelly.call(
"Temperature.GetStatus",
{ "id": CONFIG.anturi_downCirculation_id },
function (result, error_code, error_message, user_data) {
debugPrint(result.tC);
downCirculationTemperature = result.tC;
readKvs();
},
null
);
};
function readTemperatureBoiler() {
Shelly.call(
"Temperature.GetStatus",
{ "id": CONFIG.anturi_boiler_id },
function (result, error_code, error_message, user_data) {
// debugPrint(result);
debugPrint(result.tC);
boilerTemperature = result.tC;
readTemperatureAlakierto();
},
null
);
};
return { // public interface
refresh: function () {
readTemperatureBoiler();
},
};
})();
setTemperatureComponent();
Shelly.addEventHandler(
function (event, ud) {
if (!event || !event.info) {
return;
}
let event_name = event.info.event;
// debugPrint(event_name);
// temperature has changed
if (event_name === "manual") {
TemperatureHandler.refresh();
}
if (event_name === "temperature_change") {
TemperatureHandler.refresh();
}
if (event_name === "up_circulation_temperature") {
TemperatureHandler.refresh();
}
},
null
);
// check heating up circulation status every 5 minute
Timer.set(
1000 * 60 * 5, // msec, 5min check
true,
function (user_data) {
Shelly.emitEvent("up_circulation_temperature", {});
},
null
)
Shelly.emitEvent("manual", {});