How to convert to a function


Hope this is clearer
Want to be able to reduce or multiple this bit with the ability of adjusting the time values in one go (hope that make sense)

also just noted I already have one of the delay set different to the others another reason to try & replace with varibles

It's not exactly converting to a function, but the node-red-contrib-mytimeout node accepts overrides if you feed it JSON formatted payloads. You could store your variable delays as flow variables and build your desired payload using a template node.

20181227_232152_Trim
this is what its for just want to be able to change easerly

I am sure you are right but that looks way beyond my knowledge

You don't get better by sticking with what's comfortable. Play with it some and ask specific questions when you hit roadblocks. It's what the community's for.

Got a function working for ya. If you want to expand it id start with error checks and not allowing it to be triggered multiple times before it finishes its blink show.

You send it:
msg.lighton a numerical value in ms you want the light to be on
msg.lightoff a numerical value in ms you want the light to be off after it was on
msg.numberofblinks a numerical value of the number of time you want it to blink at that rate.

[{"id":"fd93f008.0e423","type":"change","z":"41f9ead2.0eab04","name":"","rules":[{"t":"set","p":"lighton","pt":"msg","to":"250","tot":"num"},{"t":"set","p":"lightoff","pt":"msg","to":"1000","tot":"num"},{"t":"set","p":"numberofblinks","pt":"msg","to":"3","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":260,"wires":[["1871a0b2.ea84af"]]},{"id":"3a5aa846.ad23c8","type":"inject","z":"41f9ead2.0eab04","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":260,"wires":[["fd93f008.0e423"]]},{"id":"7b2a537a.4ba75c","type":"debug","z":"41f9ead2.0eab04","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":730,"y":260,"wires":[]},{"id":"1871a0b2.ea84af","type":"function","z":"41f9ead2.0eab04","name":"The Light Show","func":"//get the values of msg.* to var's\nvar numberofblinks = msg.numberofblinks;\nvar light_on_for_ms = msg.lighton;\nvar light_off_for_ms = msg.lightoff;\n\n//how long to wait before starting the light show\nvar time_in_ms_to_wait_before_first_execution = 0;\n\n//total run time of light on and light off\nvar time_in_ms_to_wait_between_recursive_setTimeout = (light_on_for_ms + light_off_for_ms);\n\n//set the count to 0\nvar count = 0;\n\n//function to count the number of blinks and stop timmer\nfunction blinks()\n{\n    if (count === numberofblinks)\n    {\n        //stop the recursive setTimeout\n        clearTimeout(run);\n        \n        \n    }\n    else\n    {\n        count++;\n    }\n}\n\n\n\n//function to set payload to true\nfunction payload_true()\n{\n    //set payload to true\n    node.send({payload:true});\n    \n    //display green dot when light is on\n    node.status({\n                    fill: 'green',\n                    shape: 'dot',\n                    text: ' '\n                });\n}\n\n//function to set payload to false\nfunction payload_false()\n{\n    //set payload to false\n    node.send({payload:false});\n    \n    //clear status when payload false\n    node.status({\n            });\n}\n\n//run the light show!!!!\nsetTimeout(function run() \n{\n    blinks();\n    //turn the light on by setting msg.payload to true\n    payload_true();\n    \n    //turn the light off by waiting to set the payload to false by the ammount of time light is on\n    setTimeout(payload_false, light_on_for_ms);\n    //i++;\n    \n    //total ammount of time light can be on or off before it runs again\n    setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);\n}, time_in_ms_to_wait_before_first_execution);\n\n\n//clearTimeout(run);\nreturn msg;\n","outputs":1,"noerr":0,"x":560,"y":260,"wires":[["7b2a537a.4ba75c"]]},{"id":"9492094c.fcb338","type":"comment","z":"41f9ead2.0eab04","name":"every thing is in milliseconds","info":"","x":220,"y":180,"wires":[]},{"id":"9510cdff.f4ea8","type":"comment","z":"41f9ead2.0eab04","name":"make sure all msg values are set to numbers","info":"","x":270,"y":220,"wires":[]}]

the JS script in the function:

var numberofblinks = msg.numberofblinks;
var light_on_for_ms = msg.lighton;
var light_off_for_ms = msg.lightoff;

//how long to wait before starting the light show
var time_in_ms_to_wait_before_first_execution = 0;

//total run time of light on and light off
var time_in_ms_to_wait_between_recursive_setTimeout = (light_on_for_ms + light_off_for_ms);

//set the count to 0
var count = 0;

//function to count the number of blinks and stop timmer
function blinks()
{
    if (count === numberofblinks)
    {
        //stop the recursive setTimeout
        clearTimeout(run);
        
        
    }
    else
    {
        count++;
    }
}



//function to set payload to true
function payload_true()
{
    //set payload to true
    node.send({payload:true});
    
    //display green dot when light is on
    node.status({
                    fill: 'green',
                    shape: 'dot',
                    text: ' '
                });
}

//function to set payload to false
function payload_false()
{
    //set payload to false
    node.send({payload:false});
    
    //clear status when payload false
    node.status({
            });
}

//run the light show!!!!
setTimeout(function run() 
{
    blinks();
    //turn the light on by setting msg.payload to true
    payload_true();
    
    //turn the light off by waiting to set the payload to false by the ammount of time light is on
    setTimeout(payload_false, light_on_for_ms);
    //i++;
    
    //total ammount of time light can be on or off before it runs again
    setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);
}, time_in_ms_to_wait_before_first_execution);


//clearTimeout(run);
return msg;

hope this helps

1 Like

Thanks

I will take a look asap and let u know how I get on

Garrath

Had a quick go

Some off it appears to work but I am getting this error

28/12/2018, 17:57:32node: The Light Showfunction : (error)

"ReferenceError: run is not defined"

that is because the 'run' is ended with clearTimeout(run);

you can ignore this or write some error handling in if you wish.

the idea behind writing this function was to annotate it the best I could and let your expand and learn. If you need things like error checking, msg handling, things like: is it numerical; you need to add it yourself.

thats great it seems to work and is way better than how i was doing it

many thanks

could you explain this bit a little more, sorry I dont understand

That isn't how you clear timeouts. The call to setTimeout returns a timer object. You must pass that object to clearTimeout, not the function.

sorry have no idea what you are talking about

I got It kinda.........because setTimeout is being referenced outside the function there must be a reference to it.

basically im doing it wrong. :slight_smile: and the code works :stuck_out_tongue:

this is my first go at a timer. so my brain is stretched to the max

Well I'm going to need help fixing it. I don't think i have the knowledge.

Thanks anyway as it does work but I cant see how to get rid of the errors either (I understand some of it)

so is this what i need todo?

WRONG?
setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);

CORRECT?
var tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);

//total ammount of time light can be on or off before it runs again

// setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);
var tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);

I still get this error :- 28/12/2018, 23:26:09node: The Light Showfunction : (error)

"ReferenceError: run is not defined"

@gbond that is not a complete fix for the function - just one part of what would be needed. Unfortunately I'm in no position to provide a full code example from my phone.