`function` node. Not seeing the results I expect

I think it is more to do with what time it is here now.

Simple thing:

values (numbers) go into a function node.
If it is 70, one thing happens.
If it is 75, another thing happens.

For testing, I also check if the value is "test".

It isn't working.

I am seeing the first node.warn( ) and the second one.

But not the third.

And not getting an output.

Injecting 70 (number)

I just added 3 lines to set up msg1, msg2 and msg3 which has kind of helped. But not solved the problem.

[{"id":"3478f7f3.682718","type":"inject","z":"675e227d.d158b4","name":"70","topic":"","payload":"70","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1770,"y":80,"wires":[["f08765be.6acb68"]]},{"id":"8ef67943.0946e8","type":"inject","z":"675e227d.d158b4","name":"test","topic":"","payload":"TEST","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1770,"y":120,"wires":[["f08765be.6acb68"]]},{"id":"f08765be.6acb68","type":"function","z":"675e227d.d158b4","name":"Make message","func":"node.warn(\"Payload \" + msg.payload);\n\nnode.warn(\"a\");\n\n//  Set up messages\nmsg1.payload = {\n    event: \"Over temperature\",\n    priotiry: 1\n}\n\nnode.warn(\"1\");\n\nmsg2.payload = {\n    event:\"BedPi shutting down.  Excess heat\",\n    priority: 4\n}\nmsg3.payload = {\n    DEVICE:\"BedPi\",COMMAND:\"Shutdown\",state:1\n}\n\nnode.warn(\"Got here ok\");\nvar z = parseInt(msg.payload);\nnode.warn(\"Payload \" + msg.payload);\n\nif (msg.payload == 70)\n{\n    return [msg1,null,null];\n} else\nif (msg.payload == 75)\n{\n    return [msg1, msg2, msg3];\n} else\nif (msg.payload == \"TEST\")\n{\n    return [msg1,msg2,null];\n}","outputs":3,"noerr":0,"x":1940,"y":160,"wires":[["ac098d23.584e2","2d3ef271.888286"],["e74c05a9.5c3958"],["82f08226.354798"]],"outputLabels":["A1","Shutdown command","A4"]}]

This is your function node as you posted

node.warn("Payload " + msg.payload);

node.warn("a");

//  Set up messages
msg1.payload = {
    event: "Over temperature",
    priotiry: 1
}

node.warn("1");

msg2.payload = {
    event:"BedPi shutting down.  Excess heat",
    priority: 4
}
msg3.payload = {
    DEVICE:"BedPi",COMMAND:"Shutdown",state:1
}

node.warn("Got here ok");
var z = parseInt(msg.payload);
node.warn("Payload " + msg.payload);

if (msg.payload == 70)
{
    return [msg1,null,null];
} else
if (msg.payload == 75)
{
    return [msg1, msg2, msg3];
} else
if (msg.payload == "TEST")
{
    return [msg1,msg2,null];
}

Where do you declare msg1 msg2 and msg3?

If I run it I get

09/09/2019, 14:00:59[node: Make message](http://192.168.1.10:1880/#)function : (error)

"ReferenceError: msg1 is not defined (line 6, col 1)"

When you say you've added 3 lines is this included in what you posted or not?
You need to declare
msg1={} etc

If I add msg1={} I now get the third node.warn output

You also have a typo...

What if msg.payload is 71 ?
What is the purpose of var z =... ?

This looks like something that fits the switch node, attach some template nodes with the format you want.

Is it possible that the message payload is a string, rather than numeric value? i.e. "70"?

You should, as already mentioned, have a catch-all "else" as well, even if it's just to help in debugging.

Ok, recap.

(My fault)

This is the node - after I added the "three lines";

node.warn("Payload " + msg.payload);

node.warn("a");

//  Set up messages
var msg1;
var msg2;
var msg3;
msg1.payload = {
    'event': "Over temperature",
    'priotiry': 1
};

node.warn("1");

msg2.payload = {
    event:"BedPi shutting down.  Excess heat",
    priority: 4
}
msg3.payload = {
    DEVICE:"BedPi",COMMAND:"Shutdown",state:1
}

node.warn("Got here ok");
var z = parseInt(msg.payload);
node.warn("Payload " + msg.payload);

if (msg.payload == 70)
{
    return [msg1,null,null];
} else
if (msg.payload == 75)
{
    return [msg1, msg2, msg3];
} else
if (msg.payload == "TEST")
{
    return [msg1,msg2,null];
}

Ok, I had posted the flow also, not just the node. Phew.

All those "what if. . . . . ." questions.
Where is/are the 71 (etc) inputs?

There aren't any.

Over night while asleep this thread/post has opened another set of questions about "how to construct a message" which I know I have been down recently.

@ukmoose
The new bit of code covers your question.
I probably should have posted it last night, but I was just too tired.

@molesworth
That is an interesting question on another level to which I will get back to shortly.

Message construction.
And of course now I am looking for it, I can't find the other posts of which I am thinking.

Somewhere recently I was talking about message construction.

Basically it boiled down to showing me how to create "complex" messages.

var msg1;
msg1 = {
    payload:"Something",
    another_name:"blah" ,
}

But it wasn't that simple.

It was actually - from memory - more like:

var msg1;
msg1 = {
     'payload':"something", 
     'another_name':"blah"
}

And then there was also mentioned the , at the end.
One way it was a , and another it was ;

(I'm sure @knolleary was involved, but again: searches yielded nothing helpful)

Because I can't find them here I am looking a bit silly. (What else is new?)

But that has not helped with the over all problem I have with the node.

Anyway, the function node - as I have it sets up 3 messages to be sent back with given conditions.

I construct the messages before anything just as it seemed the better way to do things.

Then depending on what is received in the msg.payload depends on what 3 messages (or more so: HOW those messages) are returned.

Now @molesworth:
Looking at the FLOW I posted, it is weird that when I am looking at it
I see this:
"inject","z":"675e227d.d158b4","name":"70","topic":"","payload":"70"
Yeah, I know: there is more.

That threw me a bit but the next part clears it up.
"payloadType":"num",

Does that answer your question?

But it erks me a bit because I have just raised a question about the colours of different things:
strings, numbers, boolean values.
In one screen they are one (set) of colours. In another they are different. Confusing.

But I'm guessing that the values are stored as strings and their type is set by just that part of the code:
"payloadType":"num",

Problem solved - I guess.

(update)

Ok, I goofed - again.

Rather than the three lines:

var msg1;
var msg2;
var mgs3;

I should have had:

var msg1 = {};
var msg2  = {};
var msg3 = {};

FIXED for now.

1 Like