Coding functions

#1

Hi
i am trying to get at timer function to work, i have tried something like this:

var a = msg.payload;
var myVar;

if ( a === true){
myFunction();
}

function myFunction() {
myVar = setTimeout(alertFunc, 3000);
}

function alertFunc() {
return msg;
}

this will not return the payload. any ideas ?
best regards

0 Likes

#2

Why not use Node-RED nodes to make things simple ?

Flow:

[{"id":"3d109f83.9e4bd","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"23fb1e3b.2faaa2","type":"inject","z":"3d109f83.9e4bd","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":300,"wires":[["b7c54366.523e8"]]},{"id":"4c0fee22.1eff1","type":"debug","z":"3d109f83.9e4bd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":300,"wires":[]},{"id":"368eb1aa.e7518e","type":"inject","z":"3d109f83.9e4bd","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":360,"wires":[["b7c54366.523e8"]]},{"id":"b7c54366.523e8","type":"switch","z":"3d109f83.9e4bd","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":320,"wires":[["b37afd32.e166c"],["13be90fe.f9c38f"]]},{"id":"b37afd32.e166c","type":"delay","z":"3d109f83.9e4bd","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":300,"wires":[["4c0fee22.1eff1"]]},{"id":"13be90fe.f9c38f","type":"debug","z":"3d109f83.9e4bd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":380,"wires":[]}]
0 Likes

#3

What im trying to do in the long run is to reset a bool to false, this is for opening a door, and i only want the door to be open 15 seconds before releasing.
Im going to use the alexa local, and need to reset the input from alexa to false after the time has passed.

0 Likes

#4

I see. In such case it is even simpler if you use the trigger node.

Flow:

[{"id":"9ef6cff7.fb755","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"a2fb45e5.2aab88","type":"inject","z":"9ef6cff7.fb755","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":180,"wires":[["635b95c3.89e57c"]]},{"id":"635b95c3.89e57c","type":"trigger","z":"9ef6cff7.fb755","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":400,"y":180,"wires":[["c198a401.a82c78"]]},{"id":"c198a401.a82c78","type":"debug","z":"9ef6cff7.fb755","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":180,"wires":[]}]
0 Likes

#5

Just so you know what the problem is that should be

function alertFunc() {
node.send( msg );
}
0 Likes