节点红色setInterval返回相同的值

时间:2019-03-15 21:58:25

标签: javascript mqtt setinterval node-red

我的问题是我有一个功能节点,每2秒从PIR传感器读取输入0或1。我希望我的功能节点在从传感器读取1时将输出1保持10秒钟。

var newMsg;

if (msg.payload === 1 ){
    newMsg = {payload:0};
    setInterval(returnMsg, 10000);
}

else{
    newMsg = {payload:1};
}

return newMsg;



function returnMsg(){
    return newMsg;
}

我尝试使用setInterval,但是它不起作用。

编辑:

非常感谢您的答复!

所以要具体一点:

flow

和仪表将是这样的:

gauge

我想做的是,一旦PIR传感器检测到运动,仪表将显示0大约10秒钟。但是现在一旦传感器的读数发生变化,压力表就会不断变化。

2 个答案:

答案 0 :(得分:1)

满足您的要求的一种可能方法是将交换节点用作门。此门将首先打开,以允许消息流。当有效负载为“ 1”时,表示已触发PIR,功能节点内的代码将通过更改控制切换节点的流上下文变量来关闭门。同时,功能节点中的代码将配置一个回调函数,以在10秒内重新打开门。

函数内部的代码:

if (msg.payload === 1) {
    closeGate();
}


function closeGate() {
        flow.set("gateopen", false); //close gate
        setTimeout(openGate, 10000); // reopen gate within 10 seconds
}


function openGate() {
    flow.set("gateopen", true);
}

flow.get("gateopen") ? node.status({fill:"green",shape:"ring",text:"Opened"}) :
                       node.status({fill:"red",shape:"ring",text:"Closed"});

return msg;

流量:

[{"id":"8696aea0.42f8f","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"e3f585c9.421cb8","type":"inject","z":"8696aea0.42f8f","name":"","topic":"","payload":"0","payloadType":"num","repeat":"2","crontab":"","once":true,"onceDelay":"0.5","x":190,"y":200,"wires":[["be4bd9b8.de4d78"]]},{"id":"be4bd9b8.de4d78","type":"switch","z":"8696aea0.42f8f","name":"","property":"gateopen","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":370,"y":220,"wires":[["23cd0407.7efcec"]]},{"id":"63b4ec7c.b18904","type":"inject","z":"8696aea0.42f8f","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":240,"wires":[["be4bd9b8.de4d78"]]},{"id":"db6c9c0d.4c21b","type":"debug","z":"8696aea0.42f8f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":670,"y":220,"wires":[]},{"id":"fbf0ec86.55906","type":"inject","z":"8696aea0.42f8f","name":"Open gate","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"0.5","x":190,"y":120,"wires":[["2325a5f7.0d1d1a"]]},{"id":"23cd0407.7efcec","type":"function","z":"8696aea0.42f8f","name":"Gate","func":"if (msg.payload === 1) {\n    closeGate();\n}\n\n\nfunction closeGate() {\n        flow.set(\"gateopen\", false); //close gate\n        setTimeout(openGate, 10000); // reopen gate within 10 seconds\n}\n\n\nfunction openGate() {\n    flow.set(\"gateopen\", true);\n}\n\nflow.get(\"gateopen\") ? node.status({fill:\"green\",shape:\"ring\",text:\"Opened\"}) :\n                       node.status({fill:\"red\",shape:\"ring\",text:\"Closed\"});\n\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":220,"wires":[["db6c9c0d.4c21b"]]},{"id":"2325a5f7.0d1d1a","type":"change","z":"8696aea0.42f8f","name":"Open gate","rules":[{"t":"set","p":"gateopen","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":120,"wires":[[]]}]

enter image description here

下面是在发生新的PIR事件时用于扩展计时器的替代流程:

[{"id":"585ba08b.3667e","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"4cb35501.7c04fc","type":"inject","z":"585ba08b.3667e","name":"","topic":"","payload":"0","payloadType":"num","repeat":"2","crontab":"","once":true,"onceDelay":"0.5","x":190,"y":200,"wires":[["87ab9f12.fd8ad"]]},{"id":"e1699672.cb5788","type":"switch","z":"585ba08b.3667e","name":"","property":"gateopen","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":590,"y":200,"wires":[["c40dbf1f.79ef4"]]},{"id":"aafaf20e.95d46","type":"inject","z":"585ba08b.3667e","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":240,"wires":[["87ab9f12.fd8ad"]]},{"id":"c40dbf1f.79ef4","type":"debug","z":"585ba08b.3667e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":810,"y":200,"wires":[]},{"id":"5e76c261.a4f9ac","type":"inject","z":"585ba08b.3667e","name":"Open gate","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"0.5","x":190,"y":120,"wires":[["b255cf40.af298"]]},{"id":"4c13bbad.850434","type":"function","z":"585ba08b.3667e","name":"Gate","func":"if (flow.get(\"gateopen\")) {\n    closeGate(); // gate is opened and will be closes it for 10 seconds\n} else {\n    extendGate(); // gate is already closed. The timer will reset and  new 10 second timeout will start\n}\n\n\nfunction closeGate() {\n        flow.set(\"gateopen\", false); //close gate\n        flow.set(\"gateid\", setTimeout(openGate, 10000));\n}\n\n\nfunction openGate() {\n    flow.set(\"gateopen\", true);\n}\n\nfunction extendGate() {\n    clearTimeout(flow.get(\"gateid\"));\n    closeGate();\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":240,"wires":[["c40dbf1f.79ef4"]]},{"id":"b255cf40.af298","type":"change","z":"585ba08b.3667e","name":"Open gate","rules":[{"t":"set","p":"gateopen","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":120,"wires":[[]]},{"id":"87ab9f12.fd8ad","type":"switch","z":"585ba08b.3667e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":220,"wires":[["e1699672.cb5788"],["4c13bbad.850434"]]}]

enter image description here

答案 1 :(得分:0)

请检查自上次收到1以来是否还不到10秒:

此行应在读取传感器值的函数之外

var last = new Date(2000, 1, 1,  1, 1);

,然后在您的函数中:

if (msg.payload === 1 ){
    last = new Date();
}

return (new Date() - last)<10000?1:0;