Function with multiple inputs and outputs

Hi,
I want to create a function with multiple inputs and multiple outputs.
I use 2 illumination sensors and I want to specify my multiple outputs.
Here ist my code

context.voga = context.voga;
context.grtn = context.grtn;
//-----Kontext definieren-----
if (msg.topic === 'sensor.voga_hmipsnsr_helligkeit_current_illumination')
{
    context.voga = msg.payload;
}
if (msg.topic === 'sensor.grtn_hmipsnsr_helligkeit_current_illumination')
{
    context.grtn = msg.payload;
}

//-----Vorgarten prüfen-----
if (context.voga > 750.0 && context.voga < 40000.0)
{
    msg1.payload = "Hell";
}
if (context.voga > 40000.0)
{
    msg1.payload = "Sonnig";
}
//-----Garten prüfen-----
if (context.grtn > 750.0 && context.grtn < 40000.0)
{
    msg2.payload = "Hell";
}
if (context.voga > 40000.0)
{
    msg1.payload = "Sonnig";
}


return [msg1,msg2];

So, If the illumination sensor voga is between a 700 and 40000 output one shoud send the string "hell".
If the illumination is above 40000 output one should send the string "sonnig".

The same should happen on output two, so I have different output for different areas. But I always receive an error: "ReferenceError: msg1 is not defined (line 33, col 9)"

Can someone help me please?

Thanks in advance

The error happens because you never initialise msg1 and msg2.

You can do that by adding the following to the start:

let msg1 = {};
let msg2 = {};

Thank you for your help,
now I receive the error

24.4.2021, 23:32:08node: Vogamsg.payload : undefined

undefined

That looks like the output from a Debug node rather than an error.

It is certainly possible, with your current code, that neither msg1 or msg2 will have their payload set, so it will be undefined.

It all depends on what behaviour you want. For example, if a message's payload has not been set by any of your if statements, should it return anything at all?

Yes, the illumination sensors will always send values. When this value is between a specific range then I will set "Hell" or "Sonnig" so it is easier to control my covers. I know I can do that with switches but I want to learn how to input multiple values and send them to multiple outputs.
Maybe you have a simple example how to solve this. The docs always explain how to use multiple inputs OR multiple outputs but not both together.
I hope you understand what I mean, english is a foreign language for me :slight_smile:

What do you want the payloads set to if voga or grtn < 750? Or perhaps you don't want to send a message at all in that situation.

Should the last if test in your flow be

if (context.grtn > 40000.0)
{
    msg2.payload = "Sonnig";
}

So grtn and msg2 not msg1?

Hi Colin,
you are absolutely right. That is the mistake. While I was opening my threat, the value was below 750 so undefined. Very embarrasing. Maybe you can help me to do nothing when the value is below 750.

In the case where you don't want to send anything set the msg to null. You can achieve this by using else with the if tests

if (context.voga > 750.0 && context.voga < 40000.0)
{
    msg1.payload = "Hell";
}
else if (context.voga >= 40000.0)
{
    msg1.payload = "Sonnig";
} 
else
{
  msg1 = null
}

Note that I have added else to the second test and also changed it to >=. As you had it if the value was exactly 40000 neither test would pass.

Thank you very much for your help!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.