update_pool from MQTT rather than from timer

This commit is contained in:
Gilles Grandou 2022-04-21 10:51:55 +02:00
parent bcc945137c
commit 87c43ff253

58
pool.js
View File

@ -7,9 +7,14 @@ let status = {
temp_yesterday: null, temp_yesterday: null,
update_time: null, update_time: null,
update_time_last: 0,
update_temp_max_last: null,
disable_temp: null, disable_temp: null,
tick_temp: 0,
tick_pool: 0,
tick_pump: 0,
tick_timer: 0, tick_timer: 0,
tick_day: 0, tick_day: 0,
}; };
@ -72,9 +77,12 @@ function time_to_timespec(t) {
} }
// update temperature from Sensor // update temperature from Sensor
// - trigger pump update if needed
function update_temp(temp) { function update_temp(temp) {
print("[POOL] update_temp", temp); status.tick_temp++;
print("[POOL] update_temp", status.tick_temp, temp);
if (status.disable_temp !== null) { if (status.disable_temp !== null) {
print("[POOL] updated disabled"); print("[POOL] updated disabled");
return; return;
@ -85,6 +93,13 @@ function update_temp(temp) {
status.temp_max = Math.max(status.temp_today, status.temp_yesterday); status.temp_max = Math.max(status.temp_today, status.temp_yesterday);
print("[POOL] update_temp - max:", status.temp_max, "today:", status.temp_today, "yesterday:", status.temp_yesterday); print("[POOL] update_temp - max:", status.temp_max, "today:", status.temp_today, "yesterday:", status.temp_yesterday);
if (status.temp_max !== status.update_temp_max_last) {
update_pool();
}
else {
print("[POOL] no temp change, skip update_pool");
}
} }
// new day, update status // new day, update status
@ -126,7 +141,8 @@ function do_call(calls) {
// TODO freeze mode if cur < 1 // TODO freeze mode if cur < 1
function update_pump(temp, max, time) { function update_pump(temp, max, time) {
print("[POOL] update_pump", temp, max, time); status.tick_pump++;
print("[POOL] update_pump", status.tick_pump, temp, max, time);
let duration = compute_duration_filt(max); let duration = compute_duration_filt(max);
let schedule = compute_schedule_filt(duration); let schedule = compute_schedule_filt(duration);
@ -168,15 +184,21 @@ function update_pump(temp, max, time) {
do_call(calls); do_call(calls);
} }
// // update pool:
// - retrieve current time
// - switch to next day
// - do a pump update if the last one didn't not happen too close
function update_pool() { function update_pool() {
status.tick_pool++;
print("update_pool", status.tick_pool);
Shelly.call ( Shelly.call (
"Sys.GetStatus", "Sys.GetStatus",
{}, {},
function (result) { function (result) {
let time = result.time; // "HH:MM" let time = result.time; // "HH:MM"
print("[POOL] timer", time); print("[POOL] time", time);
// compute current time in float format (12h45 -> 12.75) // compute current time in float format (12h45 -> 12.75)
let t = JSON.parse(time.slice(0,2)) + JSON.parse(time.slice(3,5)) / 60; let t = JSON.parse(time.slice(0,2)) + JSON.parse(time.slice(3,5)) / 60;
@ -186,13 +208,22 @@ function update_pool() {
status.update_time = t; status.update_time = t;
if (status.temp_max !== null) if (status.temp_max !== null) {
if ((t - status.update_time_last) > 0.15) { // 9 minutes
update_pump(status.temp, status.temp_max, t); update_pump(status.temp, status.temp_max, t);
status.update_time_last = t;
status.update_temp_max_last = status.temp_max;
}
else {
print("[POOL] to much update_pool, skipped");
}
}
} }
); );
} }
// receives updated from Pool Sensor // receives update from Pool Sensor
// - trigger all temperature and pump updates
MQTT.subscribe( MQTT.subscribe(
"zigbee2mqtt/Piscine", "zigbee2mqtt/Piscine",
@ -229,18 +260,3 @@ Shelly.addEventHandler(
); );
// Run once per hour
// - update transition today -> yesterday
// - update pump schedule
Timer.set (
3600 * 1000,
true,
function () {
status.tick_timer++;
print("[POOL] timer", status.tick_timer);
update_pool();
}
);