SergiuToporjinschi / node-red-contrib-heater-controller

Heater controller for node-red dashboard
GNU General Public License v3.0
18 stars 17 forks source link

hysteresis calculation wrong #49

Open odenisenko-zz opened 4 years ago

odenisenko-zz commented 4 years ago

from recalculateAndTrigger method

function hysteresis(currentTemp, targetValue, thresholdRising, thresholdFalling) {
    var difference = (targetValue - currentTemp);
    var newHeaterStatus = (difference < 0 ? "off" : "on");
    var threshold = (newHeaterStatus === "off" ? thresholdRising : thresholdFalling);
    var changeStatus = (Math.abs(difference) >= threshold);
    if (changeStatus) {
        return newHeaterStatus;
    }
    return null;
}

test-case

var targetValue = 22;
var thresholdRising = 1;
var thresholdFalling = 3;

for (let currentTemp = 19; currentTemp < 25; currentTemp++) {
    console.log(currentTemp, hysteresis(currentTemp, targetValue, thresholdRising, thresholdFalling));
}

result

19 on test.js:17
20 null test.js:17
21 null test.js:17
22 null test.js:17
23 off test.js:17
24 off 
SergiuToporjinschi commented 3 years ago

Hi, It's possible, I will take a look. thank you for this scenario.