Ask for help with debug in node red

hi evryone can anyone help me with this :
i wana simulate code can control a system {pump-low start and stop by switch after pump-low start the pump-décolorant and pump-scalaut starting after 2 sec also pump-high start after 6 sec when switch is false all pump stoped}

but i get northing in debug any advise
here is the code :
<
// Declare variables for the pumps
let pumpLow;
let pumpDecolorant;
let pumpScalaut;
let pumpHigh;

// Declare a variable to track the status of the pumps
let pumpStatus = "off";

// This function is called when the switch is flipped
function startPumps(msg) {
// Check the value of the switch
if (msg.payload === true) {
// If the switch is turned on, start the pump-low
pumpLow = setInterval(function () {
console.log("pump-low started");
}, 1000); // Run the function every second

    // Set a timeout to start the pump-decolorant and pump-scalaut after 2 seconds
    setTimeout(function () {
        pumpDecolorant = setInterval(function () {
            console.log("pump-decolorant started");
        }, 1000);  // Run the function every second

        pumpScalaut = setInterval(function () {
            console.log("pump-scalaut started");
        }, 1000);  // Run the function every second
    }, 2000);

    // Set a timeout to start the pump-high after 6 seconds
    setTimeout(function () {
        pumpHigh = setInterval(function () {
            console.log("pump-high started");
        }, 1000);  // Run the function every second
    }, 6000);

    // Update the pump status
    pumpStatus = "on";
} else {
    // If the switch is turned off, stop all pumps
    stopPumps();
}

}

// This function stops all pumps
function stopPumps() {
clearInterval(pumpLow);
clearInterval(pumpDecolorant);
clearInterval(pumpScalaut);
clearInterval(pumpHigh);
console.log("all pumps stopped");
pumpStatus = "off";
}

// Send the pump status to the output
return {
payload: pumpStatus
};

//


setTimeout functions may eventually create all kinds of problems, can I suggest a more node-red "way" of implementing this ?

Try this flow:

[{"id":"1622bb3abca3940f","type":"inject","z":"31c1e2adb95d7cd8","name":"pump start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":160,"y":60,"wires":[["b779a5bda71c5c6b"]]},{"id":"b779a5bda71c5c6b","type":"function","z":"31c1e2adb95d7cd8","name":"function 1","func":"\nif(msg.payload){\n    node.status({ fill: \"green\", shape: \"dot\", text: \"Pumps started\" });\n}\nelse{\n    node.status({ fill: \"red\", shape: \"dot\", text: \"Pumps stopped\" });\n}\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":275,"y":80,"wires":[["eb6dfcad89e271f5"]],"l":false},{"id":"ab2f978a43b0451d","type":"inject","z":"31c1e2adb95d7cd8","name":"pumps stop","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":150,"y":100,"wires":[["b779a5bda71c5c6b"]]},{"id":"9be737534ef39b61","type":"function","z":"31c1e2adb95d7cd8","name":"function 2","func":"if (msg.payload) {\n    node.status({ fill: \"green\", shape: \"dot\", text: \"pump-decolorant started\" });\n    return msg;\n}\nelse {\n    node.status({ fill: \"red\", shape: \"dot\", text: \"pump-decolorant stopped\" });\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":555,"y":160,"wires":[[]],"l":false},{"id":"558eb0a27b419ae5","type":"delay","z":"31c1e2adb95d7cd8","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":420,"y":160,"wires":[["979a5e0e162278dc","9be737534ef39b61"]]},{"id":"eb6dfcad89e271f5","type":"switch","z":"31c1e2adb95d7cd8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":345,"y":80,"wires":[["979a5e0e162278dc","9be737534ef39b61","eeb91795a917d96a"],["558eb0a27b419ae5"]],"l":false},{"id":"1c4161b1161d26e6","type":"delay","z":"31c1e2adb95d7cd8","name":"","pauseType":"delay","timeout":"6","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":680,"y":80,"wires":[["eeb91795a917d96a"]]},{"id":"eeb91795a917d96a","type":"function","z":"31c1e2adb95d7cd8","name":"function 3","func":"if (msg.payload) {\n    node.status({ fill: \"green\", shape: \"dot\", text: \"pump-high started\" });\n    msg.payload = {pumpstatus:\"On\"}\n    return msg;\n}\nelse {\n    msg.payload = { pumpstatus: \"Off\" }\n    node.status({ fill: \"red\", shape: \"dot\", text: \"pump-high stopped\" });\n    return msg;\n\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":785,"y":40,"wires":[["76261901fede0e36"]],"l":false},{"id":"979a5e0e162278dc","type":"function","z":"31c1e2adb95d7cd8","name":"function 4","func":"if (msg.payload) {\n    node.status({ fill: \"green\", shape: \"dot\", text: \"pump-scalaut started\" });\n    return msg;\n}\nelse {\n    node.status({ fill: \"red\", shape: \"dot\", text: \"pump-scalaut stopped\" });\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":555,"y":100,"wires":[["1c4161b1161d26e6"]],"l":false},{"id":"76261901fede0e36","type":"debug","z":"31c1e2adb95d7cd8","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":845,"y":40,"wires":[],"l":false}]

thanks
but do you know how to stop the delay in the same time
because it remember the last msg of start

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.