Set Output for specified time high

Hello

If my function node returns "1" then I need to turn on a GPIO Pin for 10 seconds on. After this then seconds it should be turned off.
First I tried it with a while loop. But the delay function is somethin JS didn't now :slight_smile:

var start = Date.now()//.toLocaleString("de-CH");
var end = start + 5000
msg.start = start
msg.end = end


if (measurement >= setpoint2){
    
while (start < end) {
  start += 1000
  msg.payload = 1
  node.send(msg)
  delay(1000)
 }
 msg.payload = 0
}

else {msg.payload = 0}
return msg;

I also tried something with the trigger node but I things somethin in my head didn't get it...

Are there any suggestions on how to solve this problem?

Here is a subset from my current status of the flow

[{"id":"6c55019.adf4a8","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"2e5bef35.a4d0a","type":"inject","z":"6c55019.adf4a8","name":"","props":[{"p":"payload"}],"repeat":"900","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1.5","payloadType":"num","x":1030,"y":480,"wires":[["c76ac4d4.203918"]]},{"id":"c76ac4d4.203918","type":"function","z":"6c55019.adf4a8","name":"","func":"measurement = Number(msg.payload)\nlet setpoint2 = 1.2 //flow.get(\"setpoint2\")\n\nif (measurement >= setpoint2){\n    msg.payload = 1\n}\nelse {msg.payload = 0}\n\n\nreturn msg;\n\n\n/*\nvar start = Date.now()//.toLocaleString(\"de-CH\");\nvar end = start + 5000\nmsg.start = start\nmsg.end = end\n\n\nif (measurement >= setpoint2){\n    \nwhile (start < end) {\n  start += 1000\n  msg.payload = 1\n  node.send(msg)\n  delay(1000)\n }\n msg.payload = 0\n}\n\nelse {msg.payload = 0}\nreturn msg;\n\n*/","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1220,"y":480,"wires":[["b6ec0b5c.0e6b","66d639c0.ce1828","51d71eec.2a82f","8288b2db.03046"]]},{"id":"51d71eec.2a82f","type":"rpi-gpio out","z":"6c55019.adf4a8","name":"LED GPIO 40","pin":"40","set":false,"level":"0","freq":"","out":"out","x":1640,"y":480,"wires":[]},{"id":"8288b2db.03046","type":"ui_chart","z":"6c55019.adf4a8","name":"","group":"26725c43.69ad54","order":5,"width":0,"height":0,"label":"GPIO","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"604800","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"x":1610,"y":540,"wires":[[]]},{"id":"b6ec0b5c.0e6b","type":"debug","z":"6c55019.adf4a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1390,"y":400,"wires":[]},{"id":"66d639c0.ce1828","type":"ui_led","z":"6c55019.adf4a8","order":2,"group":"26725c43.69ad54","width":3,"height":2,"label":"","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"#ff0000","value":"0","valueType":"num"},{"color":"#008000","value":"1","valueType":"num"}],"allowColorForValueInMessage":false,"shape":"circle","showGlow":true,"name":"","x":1610,"y":420,"wires":[]},{"id":"26725c43.69ad54","type":"ui_group","name":"Default","tab":"","order":1,"disp":true,"width":"6","collapse":false}]

Dont loop in a function (it blocks the single node thread)

Have you tried the setting the trigger node like this?

image

Hello

Perfect. Thanks a lot. This was a easy one. In my head I alwasy try to cover the else case. I will use the flow to water my plants in this case you are right. I don't need to now when its wet only if its dry or not :slight_smile:

Thanks a lot for the fast answere

Regards
malaxy

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