I am trying to make a script which can detect when some one takes a shower. The way i try to do this is is with the data from my Smartgate watermeter. This is my initial script.
const date = new Date();
const currentYear = date.getFullYear();
const currentMonth = date.getMonth() + 1;
const currentDay = new Date().getDate();
var minutes = date.getMinutes();
var hours = date.getHours();
var totalMinutes = (60 * hours) + minutes;
console.log('totalMinutes is:'+totalMinutes);
waterMeterShowerUpdate = global.get('waterMeterShowerUpdate');
console.log('waterMeterShowerUpdate is: ' +waterMeterShowerUpdate)
if(waterMeterShowerUpdate === false)
{
global.set('waterMeterShowerUpdate', true);
var waterMeterShower = Array(1440).fill(0);
global.set('waterMeterShower', waterMeterShower )
}
var waterMeterShower = global.get('waterMeterShower');
waterMeter = global.get('waterMeterDay'+currentYear +currentMonth);
var tmpWaterMeter = waterMeter[currentDay -1];
waterMeterShower.splice(totalMinutes,1,tmpWaterMeter);
global.set('waterMeterShower', waterMeterShower);
The global variable “waterMeterShower” has a length of 1440 . This is equal to all minutes in one day. With some advanced flows the array will only get new data when the watermeter is running. This way i don’t have to make a flow which runs every minute. I hope the array will look something like this [0,0,0,0,3855,0,3857,0,0,0,3859,3864,3868,3875,3879,3884,3890,3900,0,0,0,0,0,0,0,0]. Because every index number is the equivalent of a minute i want to detect the series of multiple numbers > 0 in a row. So the serie 3859,3864,3868,3875,3879,3884,3890,3900, has 8 entries, 8 entries is 8 minutes, so the water has been running for 8 minutes. In my householding this will be most of the time the shower. But the duration could also be 5 minutes or 17 minutes(my son) How do i find these kind of series? I want to save these series in variables or new arrays. From that point of i want to substract the last entry of a serie(3900) from the first entry of this serie(3859). This way i know 41 liters were used during 8 minutes. The array will be renewed everyday.