Blinking LED in function

Hi there,

I want to have a blinking LED.....how can i Do this in a function ???
I have already the following:

abs = msg.payload.abstand;
alarmon = msg.payload.alarm;
if (abs < 20){
 do 
{return {payload:1};

}
while(alarmon === true)

}
if (alarmon === false){return {payload:0};}
return msg;

I need after the first "return payload"another payload return with 0 an a delay......I hope you know what I mean...
Thanks for your help !!

Normally I'd write out the solution but here is a flow demonstrating it turning on and off a ui-led using a switch, trigger, delayand change nodes. You should be able to adapt it to your needs.

[{"id":"9f6951d1.47eee","type":"tab","label":"Test","disabled":false,"info":""},{"id":"66bfc89c.55c1f","type":"switch","z":"9f6951d1.47eee","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":290,"y":220,"wires":[["99a0ef7d.62fcf"],["b002a39.ef7776"]]},{"id":"99a0ef7d.62fcf","type":"trigger","z":"9f6951d1.47eee","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"1","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":460,"y":180,"wires":[["7aa06b8a.acd8b4","3163e7ae.c42568"]]},{"id":"3163e7ae.c42568","type":"delay","z":"9f6951d1.47eee","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":460,"y":80,"wires":[["99a0ef7d.62fcf"]]},{"id":"7aa06b8a.acd8b4","type":"ui_led","z":"9f6951d1.47eee","group":"b853aa95.01f38","order":1,"width":0,"height":0,"label":"","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"green","value":"1","valueType":"num"},{"color":"red","value":"0","valueType":"num"}],"allowColorForValueInMessage":false,"name":"","x":910,"y":180,"wires":[]},{"id":"e98ec34b.4f4a4","type":"inject","z":"9f6951d1.47eee","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":180,"wires":[["66bfc89c.55c1f"]]},{"id":"4dd5c1b.e1fbdc","type":"inject","z":"9f6951d1.47eee","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":260,"wires":[["66bfc89c.55c1f"]]},{"id":"b002a39.ef7776","type":"change","z":"9f6951d1.47eee","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":260,"wires":[["8f1dbf6.b70354","a0219e49.8cb4c8"]]},{"id":"88aa448a.423f98","type":"inject","z":"9f6951d1.47eee","name":"Manual and startup reset","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":210,"y":360,"wires":[["b002a39.ef7776"]]},{"id":"a0219e49.8cb4c8","type":"change","z":"9f6951d1.47eee","name":"needed to turn of ui-led","rules":[{"t":"set","p":"payload","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":220,"wires":[["7aa06b8a.acd8b4"]]},{"id":"8f1dbf6.b70354","type":"link out","z":"9f6951d1.47eee","name":"to RESET","links":["b3ed9183.7e5668"],"x":660,"y":300,"wires":[],"l":true},{"id":"b3ed9183.7e5668","type":"link in","z":"9f6951d1.47eee","name":"RESET","links":["8f1dbf6.b70354"],"x":250,"y":120,"wires":[["3163e7ae.c42568","99a0ef7d.62fcf"]],"l":true},{"id":"b853aa95.01f38","type":"ui_group","z":"","name":"Default","tab":"c0e9b3.7bfe565","disp":true,"width":"14","collapse":false},{"id":"c0e9b3.7bfe565","type":"ui_tab","z":"","name":"Test","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Is it possible to do this in JSON ??

If you need to send two (or more) messages from within a function then you need to use node.send(...) - see Sending Messages Asynchronously here - https://nodered.org/docs/user-guide/writing-functions
Note you also have setTimeout and setInterval available to you in a function so you may use those also.

What do you mean by do it in JSON?

SO i want to have it in the function, like this:

abs = msg.payload.abstand;
alarmon = msg.payload.alarm;
if (abs < 20){
 do 
{return {payload:1};
delay(1000);
return {payload:0};
delay(1000);

}
while(alarmon === true)

}
if (alarmon === false){return {payload:0};}
return msg;

So while alarmon ist true the function will send "1" then wait 1 second, then send "0", wait 1 second, then send again "1", wait.....and so on....do you understand what i mean ?!:slight_smile:

1 Like

delay is not a valid built in function in javascript, read again what @dceejay wrote and google a bit about that for javascript, that is the best way learning and not just expecting ready recipes

FYI
JSON (JavaScript Object Notation) is an open standard file format
`functions' are written using JavaScript (a programming language)

I'll be intereated to see the solution you come up with in your function node.

Okay...sorry, but I'm obviously too stupid to do this !!! SO my LED will not blink....so it is !!

Don't give up that easy, you are here to learn, we have all walked that long journey. But you will not help yourself if we just serve you a ready solution

So let's break this down a bit... and maybe re-arrange

if the alarmon is false we need to cancel any running flash sequence and send out a 0 (as you do already) - and return.
if abstand is greater (or =) to 20 then cancel and return. (though you haven't said if this is what should happen if it goes back above 20 rather than being turned off)
otherwise
if there is an existing flash sequence running do nothing (return null)
or start a flash sequence

In this case a flash sequence is probably best set by a setInterval running every (say 1) second to turn it on - and containing another setTimeout call after 0.5 sec to turn it off... - as this is non-obvious I will give it to you :slight_smile:

tick = setInterval(function() { 
    node.send({payload:1}); 
    setTimeout(function() {
        node.send({payload:0})
        }, 500)
}, 1000);

to ensure that we preserve the flash operation between multiple message arriving (so we can tell if it's already flashing or not and don't start another at the same time - we need to save it is context -
so once started you will need to save it like context.set("timer",tick)
and then that can be used (further up the function) where you need to test if it's running or not - to clear a setInterval you can then use clearInterval(context.get("tick")

Hopefully enough clues there to get you going - and googling - and trying some stuff and asking more questions :slight_smile:

Hi,

i tested it like this:

abs = msg.payload.abstand;
alarmon = msg.payload.alarm;
tick = setInterval(function() { 
    node.send({payload:1}); 
    setTimeout(function() {
        node.send({payload:0})
        }, 500)
}, 1000);

if (abs < 20){
 do 
{tick();
context.set("timer",tick);
}
while(alarmon === true)

}
if (alarmon === false){
    clearInterval(context.get("tick"));
return {payload:0};}
return msg;

My LED is blinking, but always !!!:frowning:

Sorry, that I'm so stupid.....

Not stupid - just need more practise... here you go...

abs = msg.payload.abstand;
alarmon = msg.payload.alarm;
tick = context.get("timer") || null; // retrieve saved timer (if there is one)

if (alarmon === false) { // alarm is off
    if (tick) { // if running - stop and clear it
        clearInterval(tick);
        context.set("timer",null);
    }
    return;
}

if (abs >= 20) { // if above the limit stop
    if (tick) {  // if running - stop and clear it
        clearInterval(tick);
        context.set("timer",null);
    }
    return;
}

if (tick) { return; } // if already running - leave it alone

// otherwise start it running
tick = setInterval(function() { 
    node.send({payload:1}); 
    setTimeout(function() {
        node.send({payload:0})
        }, 500)
}, 1000);
context.set("timer",tick); // save it 

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