Calling a function within a function

aside from bringing a msg into a function node and sending a msg out at the end of a function, how can i call a function from within a function?

in the middle of my function i need to perform some numerical computes that i can either copy and paste the same formulas throughout my functions or i can call a function.

I did write a function and saved it to a function library, but i don't see how to call it. (node.send?). I may need to call the same function 10 times within the function.

In my searches i see talk about 'sub flows'., but how is the 'return' implemented? is a sub flow wired or called?

A subflow is wired the same as any node. The beauty of using a subflow is your function code exists only once (i.e. not an unmanageable 10 copy/paste affair).

Pass your data to input of subflow, get processed data out of its output(s)

1 Like

If you want to stay totally inside the one function you can just declare an internal function just like in any other piece of javascript. var myfunc = function(foo) {... style then call it from within your function.

Things to note are that the whole function is instantiated whenever a msg arrives so you if you need to maintain some context you must remember to do that.

1 Like

Thank you dceejay,

i'm not sure where i read it, but is it possible to declare a function as you stated as a 'global variable' where i change a memory config in settings.js?

You can declare functions in global. But it isn't wise to do that within a flow, only from settings.js

That is because not all var handlers may support serialisation of functions.

But setting in the globals section of settings.js should always work. You can then use `global.get('myfnName') in a function node to get a reference to the function.

do i simply add the function declaration at the end of the settings file?

function myFunction(n1,n2) {
  return n1 + n2;
}
var input = msg;
var off = input.payload.foff;   
var on = input.payload.fon;     
var count = 1;                  
var input_off_float = flow.get(["input_off_float"]);    
var ttltimeofffloatoff, ttltimeofffloaton, difference, calcdifference, testf ; 
var d = new Date();             
var timestamp1, timestamp2;     
var newfunction = global.get('myFunction');

timestamp1 = d.getTime();



if(input_off_float == 2){ 
    on=on+count;            // increase count variable to write back to lifetime counter            
    timestamp2 = global.get(["offfloatlaststoptime"]);  // get the last time off float went off to compute how long float has been off
    difference = timestamp1 - timestamp2;               // computes the difference using epoch time
    calcdifference = parseInt(difference/1000);        
    flow.set(["difference"],difference);                // sets difference to actual difference in milliseconds
    flow.set(["ttltimeofffloaton"],calcdifference);    // gives us seconds for total time off float to on
    testf = newfunction(difference, 5);
    flow.set(["testf"],testf);
}
else if (input_off_float == 3){ 
    off=off+count;               // increase count variable to write back to lifetime counter 
    timestamp2 = global.get(["onfloatlaststoptime"]);   // get the global variable storing the last time the on float went to off status
    difference = timestamp1 - timestamp2;               // computes the difference using epoch time
    calcdifference = parseInt(difference/1000);         
    flow.set(["difference"],difference);              
    flow.set(["ttltimetopumpfromhighfloat"],calcdifference);    // set the total time var for the pump to empty  
    testf = newfunction(difference, 10);
    flow.set(["testf"],testf);
}

flow.set(["on"],on);
flow.set(["off"],off);

msg.payload.foff = off;
msg.payload.fon = on;

return msg;

this is my console output

22 Apr 17:40:10 - [error] [function:reads count file updates count writes back] TypeError: newfunction is not a function


I feel like i am close, but what am i missing?

thanks for your time...

This did it,

I can work with this

1 Like