Java script basics


Trying to optimise my flows a bit but am having problems i cant quite fathom.

i have 4 x function nodes i think i can replace with 1. Each is passed the same message containing sensor readings and has to reference a flow level variable to determine the record source.
(the preceding node overwrites the topic).

so in each function i am retrieving the flow level variable that indicates the sensor data source and using that to recreate a meaningful topic hierarchy, including the sensor type that function is devoted to, and resetting the message payload to the payload.sensor value of interest before passing on the new message.

the individual function devoted to temperatures :

var plant = flow.get('plant')||'PlantU';
var part1 = plant.slice(0,5);
var part2 = plant.slice(-1);
plant = part1 +'/' + part2;
msg.payload = msg.payload.temperature;
msg.topic = 'Notify/'+ plant +'/T';
return msg;

Works without errors and the other functions mirror what its doing too

and this is my combined funtion,

var plant = flow.get('plant')||'PlantY';
var part1 = plant.slice(0,5);
var part2 = plant.slice(-1);
plant = part1 +'/' + part2;
var msg1 = { payload:0 };
var msg2 = { payload:0 };
var msg3 = { payload:0 };
var msg4 = { payload:0 };
//moisture msg1
msg1.payload = msg.payload.moisture;
msg1.topic = 'Notify/'+ plant +'/M';
// battery msg2
msg2.payload = msg.payload.battery;
msg2.topic = 'Notify/'+ plant +'/B';
// Temp msg3
msg3.payload = msg.payload.temperature; //.payload.temperature;
msg3.topic = 'Notify/'+ plant +'/T';
//EC msg4
msg4.payload = msg.payload.conductivity;
msg4.topic = 'Notify/'+ plant +'/EC';
return [msg1,msg2,msg3,msg4]

it kicks out this error

11/09/2018, 19:23:51[node: Notification machine]( : (error)

"TypeError: Cannot read property 'temperature' of undefined"

it then continues to work as i would expect, and kick out the correct sensor values with the correct topics

So should i just ignore the error??


ok i just moved the payload assignments up to the declarations and the error has gone away !!

i have to leave the room for a little screaming..


I could not reproduce the error. I just crafted an input the flow with the required properties:

    "moisture": 30,
    "battery": 40,
    "temperature": 50,
    "conductivity": 60

Also, if you want to send all four messages out of the function node than you need to change the last line to:

return [[msg1,msg2,msg3,msg4]]

If your leave

return [msg1,msg2,msg3,msg4]

then your function node will need 4 outputs.


Thanks Andrei,

After my initial post i thought i may as well move the final payload assignments to where i am declaring the new messages, after doing so the error stopped appearing in the debug window?
i had copied the declaration statement directly from a node red guide page.

But now no error??? alls well i guess..

I guess i must have left a stray ',' or invisible character in the text i have pasted a lot as i dont know any java.. ..

re the output, i am currently using 4 discrete output nodes and directing all 4 paths back to the same mqtt out node, with your suggestion i could reduce the flows to 1 and send out all 4 messages in an array? sequentially?, Ok i will give that a go.. thanks again for taking the time to look at this?


yes the array of messages out of a single node works great.

so far i have managed to reduce the number of nodes employed by a factor of 10

from 300+ to less than 40 and 24 of those are devoted to the ui and retrieving user set settings..


wow, well done. Excellent simplification, For sure the flow is more readable and easier to maintain now.


Have you tried to use "Change" and "Switch" instead of "Function" for such a manipulation? I'm pretty sure you can do most of those string managements by only using the "Change" node and increase the readability even more