Multiple outputs from Function node

I'm getting an error saying:
ReferenceError: msg1 is not defined (line 56, col 9)

I've used multiple message outputs before and this is the second attempt at getting it to work.

so I am not understanding what is gonig on.

What I am wanting to do (really)

This is a hack as I nearly posted a thread a while back asking, and thought I had worked it out myself.
(You know what thought did.)

I have two series of data coming in.
I want to see what difference there is, and if it is above a "threshold" I get an alert.

As the difference can swing from - to + I thought I would be smart and say:
if (r < 0) {reverse the subtraction}
Which is fine in theory, but I can't seem to get it working.

I've set a low threshold only for testing.
(Forgive the bad spelling in the code.)

Can someone explain why I am getting the error? And maybe a nicer way to do this?

var threashold = 60;

if (msg.topic == 'slip')
{
    context.set('threashold'), msg.payload;
}
//---------------------
if (msg.topic == "RTC")
{
    context.set('RTC',msg.payload);
    node.warn("RTC   " + context.get('RTC'));
}
if (msg.topic == "local")
{
    context.set('local', msg.payload);
    node.warn("Local " + context.get('local'));
}
if (context.get('RTC') !== 0)
{
    if (context.get('local') !== 0)
    {
        var a = context.get('RTC');
        var b = context.get('local');
        var r = a - b;
    }
    
    node.warn("Error is " + r);
    if (r > threashold)
    {
        //  ** We have a problem! **
        node.warn("Part 1");
        node.status({fill:"red",shape:"dot",text:"Time error"});
        msg1 = {payload: false};
        msg2 = {payload: r};
    } 
    if (r < 0)
    {
        r = b - a;
        if (r > threashold)
        {
            //  ** We have a problem! **
            node.warn("Part 2");
            node.status({fill:"red",shape:"dot",text:"Time error"});
            msg1 = {payload: false};
            msg2 = {payload: r};
        } else
        {
            //   All Good!
            node.warn("Part 3");
            node.status({fill:"green",shape:"dot",text:"Time good"});
            msg1 = {payload: true};
            msg2 = {payload: r};
        }
    }
}
return [msg1, msg2];

This

return [msg1, msg2];

is outside your if statement.

If if (context.get('RTC') !== 0) is not matched, both msg1 and msg2 are undefined.

Thanks bakman2.

I've since found another problem:

The 'threshold' is not being set.

I modified the code in the function node to this:
(and again: excuse the way I did it. I have only just got it working to this level)

var threshold = context.get('threshold')|| 60;
context.set('threshold', threshold);

if (msg.topic == 'slip')
{
    node.warn("**************************");
    node.warn(msg.payload);
    var t = msg.payload;
    node.warn(t);
    context.set('threshold'), t;
    node.warn("Threshold set to " + msg.payload);
}
//---------------------
if (msg.topic == "RTC")
{
    context.set('RTC',msg.payload);
    //node.warn("RTC   " + context.get('RTC'));
}
if (msg.topic == "local")
{
    context.set('local', msg.payload);
    //node.warn("Local " + context.get('local'));
}
node.warn("Stored RTC value " + context.get('RTC'));
node.warn("Stored RPI value " + context.get('local'));
node.warn("Stored threshold value " + context.get('threshold'));

if (context.get('RTC') !== 0)
{
    if (context.get('local') !== 0)
    {
        var a = context.get('RTC');
        var b = context.get('local');
        var r = a - b;
    }
    
    node.warn("Error is " + r);
    if (r > threshold)
    {
        //  ** We have a problem! **
        node.warn("Part 1");
        node.status({fill:"red",shape:"dot",text:"Time error"});
        msg1 = {payload: false};
        msg2 = {payload: r};
        return [msg1, msg2];
    } 
    if (r < 0)
    {
        r = b - a;
        if (r > threshold)
        {
            //  ** We have a problem! **
            node.warn("Part 2");
            node.status({fill:"red",shape:"dot",text:"Time error"});
            msg1 = {payload: false};
            msg2 = {payload: r};
            return [msg1, msg2];
        }
    }
    else
    {
        //   All Good!
        node.warn("Part 3");
        node.status({fill:"green",shape:"dot",text:"Time good"});
        msg1 = {payload: true};
        msg2 = {payload: r};
        return [msg1, msg2];
    }
}
//return [msg1, msg2];

I left the last line there only .... out of lazliness.

It is working better.

But now at the top:

var threshold = context.get('threshold')|| 60;
context.set('threshold', threshold);

if (msg.topic == 'slip')
{
    node.warn("**************************");
    node.warn(msg.payload);
    var t = msg.payload;
    node.warn(t);
    context.set('threshold'), t;
    node.warn("Threshold set to " + msg.payload);
}

This is messy but I didn't do this out of choice.

Originally it was (something like:

var threshold = context.get('threshold')|| 60;
context.set('threshold', threshold);

if (msg.topic == 'slip')
{
    node.warn("**************************");
    node.warn("Threshold set to " + msg.payload);
    context.set('threshold'), msg.payload;
}

But that didn't seem to work.
The threshold - if changed - was wiped.

Putting it like this:

var threshold = context.get('threshold')|| 60;
context.set('threshold', threshold);

if (msg.topic == 'slip')
{
    node.warn("**************************");
    context.set('threshold'), msg.payload;
    node.warn("Threshold set to " + msg.payload);
}

Also failed.

I then tried something like:

var threshold = context.get('threshold')|| 60;
context.set('threshold', threshold);

if (msg.topic == 'slip')
{
    node.warn("**************************");
    node.warn(msg.payload);
    context.set('threshold'), msg.payload;
    node.warn("Threshold set to " + msg.payload);
}

Also didn't work.

I was getting stuff back like this when I injected a 10 as the new value:

slip : msg : Object
{ _msgid: "6d7ad8f6.754ea8", topic: "slip", payload: 10 }
4/16/2019, 3:50:04 PMnode: 80e3a376.889948function : (warn)
"**************************"
4/16/2019, 3:50:04 PMnode: 80e3a376.889948function : (warn)
"Threshold set to 10"
4/16/2019, 3:50:04 PMnode: 80e3a376.889948function : (warn)
"Stored RTC value 56983"
4/16/2019, 3:50:04 PMnode: 80e3a376.889948function : (warn)
"Stored RPI value 56997"
4/16/2019, 3:50:04 PMnode: 80e3a376.889948function : (warn)
"Stored threshold value undefined"

You can see that a debug node says a message is there with the topic of slip and a value of 10.
You can see it received by the node.
Threshold set to 10.
It then gets the two time stamps and says:

"Stored threshold value undefined"

I'm lost.

It is impossible, or at least time consuming to try and read through all your posts and work out exactly what question you are asking.
Rather than posting huge posts that include lots of tried this, it didn't work, then did this ... etc you would be better to just post a small section of code exhibiting one problem at at time. Say what is happening and what you expected to happen.

1 Like

We are lost because we don't see what you input.

Yeah, ok. Fair enough.

Here is the code.

Deploy the flow. (No GUI)
Down the bottm, press the DB on button. DeBug On.
Then press the two 10 buttons. (set up values.)

You will see a message in the output:
"Stored threshold value 60"

Now things are set up.

At the top press either the 3 or 2 at the top.

You will see:
"Stored threshold value undefined"

Why?

what do you input as data.

The two sets of buttons to the left are the two inputs.

Numbers.

They describe the varying inputs.

All being good, if the difference is close, you get a true out.
If it is beyond the range it should give a false out.

The default value can't work, but it gets things working.

Dropping it to a lower value, the stored value for the threshold seems to be wiped.

what 'buttons' ?
you didnt post your flow.

It was there, but for clarity:
(I'm sure)

[{"id":"63d42689.ed65c","type":"function","z":"15af81c5.64999e","name":"","func":"var threshold = context.get('threshold')|| 60;\ncontext.set('threshold', threshold);\n\nif (msg.topic == 'Debug')\n{\n    if (msg.payload === true)\n    {\n        context.set('db', 1);\n    } else\n    if (msg.payload === false)\n    {\n        context.set('db', 0);\n    }\n}\nvar db = context.get('db');\n\n//---------------------\nif (msg.topic == 'slip')\n{\n    if (db == 1)\n    {\n        node.warn(\"**************************\");\n        node.warn(\"Threshold set to \" + msg.payload);\n    }\n    context.set('threshold'), msg.payload;\n}\n//---------------------\nif (msg.topic == \"RTC\")\n{\n    context.set('RTC',msg.payload);\n    //node.warn(\"RTC   \" + context.get('RTC'));\n}\nif (msg.topic == \"local\")\n{\n    context.set('local', msg.payload);\n    //node.warn(\"Local \" + context.get('local'));\n}\nif (db == 1)\n{\n    node.warn(\"Stored RTC value \" + context.get('RTC'));\n    node.warn(\"Stored RPI value \" + context.get('local'));\n    node.warn(\"Stored threshold value \" + context.get('threshold'));\n}\n\nif (context.get('RTC') !== 0)\n{\n    if (context.get('local') !== 0)\n    {\n        var a = context.get('RTC');\n        var b = context.get('local');\n        var r = a - b;\n    }\n    \n    node.warn(\"Error is \" + r);\n    if (r > threshold)\n    {\n        //  ** We have a problem! **\n        node.warn(\"Part 1\");\n        node.status({fill:\"red\",shape:\"dot\",text:\"Time error\"});\n        msg1 = {payload: false};\n        msg2 = {payload: r};\n        return [msg1, msg2];\n    } \n    if (r < 0)\n    {\n        r = b - a;\n        if (r > threshold)\n        {\n            //  ** We have a problem! **\n            node.warn(\"Part 2\");\n            node.status({fill:\"red\",shape:\"dot\",text:\"Time error\"});\n            msg1 = {payload: false};\n            msg2 = {payload: r};\n            return [msg1, msg2];\n        }\n    }\n    else\n    {\n        //   All Good!\n        node.warn(\"Part 3\");\n        node.status({fill:\"green\",shape:\"dot\",text:\"Time good\"});\n        msg1 = {payload: true};\n        msg2 = {payload: r};\n        return [msg1, msg2];\n    }\n}\n//return [msg1, msg2];","outputs":2,"noerr":0,"x":530,"y":2910,"wires":[["277b3f16.b6f128"],[]]},{"id":"6e2b3517.9ed78c","type":"change","z":"15af81c5.64999e","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"RTC","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2860,"wires":[["63d42689.ed65c"]]},{"id":"d22775c6.2e404","type":"change","z":"15af81c5.64999e","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"local","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2970,"wires":[["63d42689.ed65c"]]},{"id":"ef4dd3b6.db953","type":"change","z":"15af81c5.64999e","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"slip","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":2790,"wires":[["63d42689.ed65c"]]},{"id":"4d97e22.82a141c","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":2790,"wires":[["6e2b3517.9ed78c"]]},{"id":"950fd1fa.e5eab","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"9","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":2830,"wires":[["6e2b3517.9ed78c"]]},{"id":"a668703.aab149","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"8","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":2870,"wires":[["6e2b3517.9ed78c"]]},{"id":"74ee18cd.0340f8","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"7","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":2910,"wires":[["6e2b3517.9ed78c"]]},{"id":"e6c2ccd5.c25628","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"6","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":2950,"wires":[["6e2b3517.9ed78c"]]},{"id":"19e0f847.3e46c8","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":2990,"wires":[["6e2b3517.9ed78c"]]},{"id":"70c5418e.063f","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3000,"wires":[["d22775c6.2e404"]]},{"id":"4a330c75.955ecc","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"9","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3040,"wires":[["d22775c6.2e404"]]},{"id":"a0db14f6.3b0528","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"8","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3080,"wires":[["d22775c6.2e404"]]},{"id":"1a4bf7be.fb7b88","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"7","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3120,"wires":[["d22775c6.2e404"]]},{"id":"6558df4.153d2a","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"6","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3160,"wires":[["d22775c6.2e404"]]},{"id":"d6ffe07.33bbc2","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3200,"wires":[["d22775c6.2e404"]]},{"id":"3bb87c5.0beb004","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"3","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":430,"y":2720,"wires":[["ef4dd3b6.db953"]]},{"id":"f02d13b4.d34b28","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":430,"y":2760,"wires":[["ef4dd3b6.db953"]]},{"id":"277b3f16.b6f128","type":"debug","z":"15af81c5.64999e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":2900,"wires":[]},{"id":"41181fde.e9669","type":"inject","z":"15af81c5.64999e","name":"DB off","topic":"Debug","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":430,"y":3020,"wires":[["63d42689.ed65c"]]},{"id":"6e33eaf7.1bc79c","type":"inject","z":"15af81c5.64999e","name":"DB on","topic":"Debug","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":430,"y":3054,"wires":[["63d42689.ed65c"]]}]
if (msg.topic == 'slip')
{
    if (db == 1)
    {
        node.warn("**************************");
        node.warn("Threshold set to " + msg.payload);
    }
    context.set('threshold'), msg.payload;
}

try context.set('threshold', msg.payload); instead.

1 Like