Have a play with this:
[{"id":"5c34e9c0.c762","type":"inject","z":"c38d0f4.885dff","name":"Open","props":[{"p":"payload.ContactSensorState","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":470,"y":460,"wires":[["efac3fa8.e076d","24da88b9.53bc68"]]},{"id":"c1c9de36.978c98","type":"inject","z":"c38d0f4.885dff","name":"Closed","props":[{"p":"payload.ContactSensorState","v":"0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":470,"y":580,"wires":[["24da88b9.53bc68","26fa87b2.464718"]]},{"id":"24da88b9.53bc68","type":"function","z":"c38d0f4.885dff","name":"","func":"const last = context.get(\"last\");\nif (last !== msg.payload.ContactSensorState) {\n context.set(\"last\", msg.payload.ContactSensorState);\n} else {\n return null;\n}\nlet timer = context.get(\"timer\") || false;\nswitch(msg.payload.ContactSensorState){\n case 1:\n if (!timer) {\n timer = setTimeout(()=>{\n context.set(\"timer\", false);\n node.send({payload:{on:true}});\n },3000);\n context.set(\"timer\",timer);\n }\n break;\n case 0:\n if (timer) {\n clearTimeout(timer);\n context.set(\"timer\", false);\n node.send({payload:{on:false}});\n } else {\n node.send({payload:{on:false}});\n }\n break;\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":800,"y":560,"wires":[["21ecd98e.1d4e16"]]},{"id":"21ecd98e.1d4e16","type":"debug","z":"c38d0f4.885dff","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":560,"wires":[]},{"id":"d224ca9c.39cb7","type":"function","z":"c38d0f4.885dff","name":"open x3","func":"for(i=0;i<3;i++){\n node.send({payload:{ContactSensorState:1}});\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":520,"wires":[["24da88b9.53bc68","efac3fa8.e076d"]]},{"id":"c545aee3.6cc78","type":"function","z":"c38d0f4.885dff","name":"close x3","func":"for(i=0;i<3;i++){\n node.send({payload:{ContactSensorState:0}});\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":640,"wires":[["24da88b9.53bc68","26fa87b2.464718"]]},{"id":"efac3fa8.e076d","type":"debug","z":"c38d0f4.885dff","name":"Open","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":460,"wires":[]},{"id":"1feff751.2512c9","type":"inject","z":"c38d0f4.885dff","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":480,"y":640,"wires":[["c545aee3.6cc78"]]},{"id":"ebfce448.1ef628","type":"inject","z":"c38d0f4.885dff","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":480,"y":520,"wires":[["d224ca9c.39cb7"]]},{"id":"26fa87b2.464718","type":"debug","z":"c38d0f4.885dff","name":"Closed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":640,"wires":[]}]
I don’t know what your sensor is sending but I can’t get the function node to behave in any way I wouldn’t expect even when throwing multiple things at it at the same time.
I added rbe functionality directly in the function node.
const last = context.get("last");
if (last !== msg.payload.ContactSensorState) {
context.set("last", msg.payload.ContactSensorState);
} else {
return null;
}
let timer = context.get("timer") || false;
switch(msg.payload.ContactSensorState){
case 1:
if (!timer) {
timer = setTimeout(()=>{
context.set("timer", false);
node.send({payload:{on:true}});
},3000);
context.set("timer",timer);
}
break;
case 0:
if (timer) {
clearTimeout(timer);
context.set("timer", false);
node.send({payload:{on:false}});
} else {
node.send({payload:{on:false}});
}
break;
}
return null;