How to enter multiple input

Have a nice day,
I can't figure out how to calculate the given value from solar chargers. I have the following code:

var output1 = msg.payload[0]; // Performance from the first tracker
var output2 = msg.payload[1]; // Performance from the second tracker
var nTotal = output1 + output2;
msg.nSum = nTotal;

if ( nTotal > 700 ) {
     // If both conditions are true

     //msg.url ="/relay/0?turn=on";
    
} else {
     // If both conditions are not met
     // msg.url ="/relay/0?turn=off";
}
return msg;

My goal is to separate both values, but I don't know how to do it. When I perform one loader on the function object and value the value as follows:

var output1 = msg.payload;

So I get the value.

When I have several inputs, I don't know how to index the input field/object.

doesn't work for me:

var output1 = msg.payload[0];
var output1 = msg.payload[1];

I also tried:

var output1 = msg.payload[0].number;
var output1 = msg.payload[1].number;

so without success...

Can you advise me...
Where can I find information, how the object is put together, what it contains... I'm no stranger to C++ languages and the like, but I'm not getting the hang of it here...

Have a nice day.

Hi @hacesoft

If you mean you have 2 messages coming in - use a join node, that will deliver the payloads after nth number of payloads have been received.

see : Linking two or more conditions in a function node - syntax? - General - Node-RED Forum (nodered.org)

Please show us a screen capture of your flow.

Use debug nodes to show the data being passed to your function node.
Is it an array [21, 42]?
An array of objects [{number:21}, {number:42}]?
A single number 21?
A single object {number:21}

Are you aware that a Node-red function cannot simultaneously know the data from two discrete messages, only two data values contained within a single message (Use a Join node if necessary to achieve this)

1 Like

I've already used JOIN, it looks better, but the same thing...

I want to achieve this: I have two victron solar chargers, each of them sends me instant power when the power will be 1000w and the cooling connected to the shelly plugin turns on... Then I want to add some hysteresis so that it doesn't turn on and off when the solar power is out the charger will oscillate around 1kw...

code:

[{"id":"6cf0017697520c44","type":"tab","label":"chlazeni MPTT trakeru","disabled":false,"info":"\r\n","env":[]},{"id":"6d949c46f8659f66","type":"http request","z":"6cf0017697520c44","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"http://192.168.11.6/relay/0?turn=on","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":830,"y":220,"wires":[["b7c77c51b1d5825d"]]},{"id":"9daa212495cd1b4d","type":"http request","z":"6cf0017697520c44","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"http://192.168.11.6/relay/0?turn=off","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":830,"y":340,"wires":[["b7c77c51b1d5825d"]]},{"id":"b7c77c51b1d5825d","type":"debug","z":"6cf0017697520c44","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":970,"y":280,"wires":[]},{"id":"d436d62da80b86e4","type":"victron-input-solarcharger","z":"6cf0017697520c44","service":"com.victronenergy.solarcharger/278","path":"/Yield/Power","serviceObj":{"service":"com.victronenergy.solarcharger/278","name":"MPPT WEST"},"pathObj":{"path":"/Yield/Power","type":"float","name":"PV Power (W)"},"name":"WEST","onlyChanges":true,"roundValues":"0","x":70,"y":280,"wires":[["6638525d4c6381ee"]],"outputLabels":["west"]},{"id":"acc00f93399a6275","type":"victron-input-solarcharger","z":"6cf0017697520c44","service":"com.victronenergy.solarcharger/279","path":"/Yield/Power","serviceObj":{"service":"com.victronenergy.solarcharger/279","name":"MPPT VJ"},"pathObj":{"path":"/Yield/Power","type":"float","name":"PV Power (W)"},"name":"JV","onlyChanges":true,"roundValues":"0","x":70,"y":340,"wires":[["6638525d4c6381ee"]],"outputLabels":["vj"]},{"id":"963735aa2c2813dd","type":"function","z":"6cf0017697520c44","name":"SUB_TrackerS","func":"var vystup1 = msg.Trackers[0]; // Výkon z druhého trackeru\nvar vystup2 = msg.Trackers[1]; // Výkon z druhého trackeru\nvar nCelkem = vystup1 + vystup2;\n\nmsg.nSuma = nCelkem;\n\nif ( nCelkem > 200) {\n    // Pokud jsou splněny obě podmínky\n\n   // msg.url =\"/relay/0?turn=on\";\n    \n} else {\n    // Pokud nejsou splněny obě podmínky\n   //  msg.url =\"/relay/0?turn=off\";\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":320,"wires":[["92eeb595a9a9160e"]]},{"id":"0cae4cec9dd05aea","type":"debug","z":"6cf0017697520c44","name":"debug 1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":180,"wires":[]},{"id":"92eeb595a9a9160e","type":"debug","z":"6cf0017697520c44","name":"debug 2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"nSuma","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":440,"wires":[]},{"id":"810a6386fa6f0485","type":"debug","z":"6cf0017697520c44","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":480,"y":440,"wires":[]},{"id":"6638525d4c6381ee","type":"join","z":"6cf0017697520c44","name":"","mode":"custom","build":"merged","property":"","propertyType":"full","key":"payload","joiner":"[92,110]","joinerType":"bin","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":350,"y":320,"wires":[["810a6386fa6f0485","963735aa2c2813dd"]]},{"id":"4730b73ad4fac2ed","type":"change","z":"6cf0017697520c44","name":"","rules":[{"t":"change","p":"Tracker","pt":"global","from":"","fromt":"str","to":"","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":260,"wires":[[]]}]

Try this Join node setup:

Ensure that messages from WEST and JV have different msg.topic - "WEST" and "JV" would work. If they don't already, you can use a change node to set msg.topic for each.

The output from your join node will then contain msg.payload.WEST and msg.payload.JV and you can use these values in the function.

EDIT: Should be using the value of msg.topic as the key. Sorry for the misinformation.

Did you mean Using the value of msg.topic as the key?

Yes I think I do! Thank you for correcting me.

I was somewhat confused by @hacesoft's unusual join node config.

If you insert a new join node and change the mode to Manual, the right options are automatically filled in (except for the number of message parts to join)

When I used the settings see above, I get what you see on the attached print screen

See @Colin and my correction above. It should be using msg.topic not msg.payload as the key.

I deleted the object JOIN and inserted it again, and as recommended, I didn't change anything, I just changed the number of connections and it looks usable... But now I'm confused why it's called JV on the right and WEST on the other, when I've already connected them... ..?

Thanks for the advice, it works now :).

image

I agree, it's confusing. The join node takes two messages
{topic: "WEST", payload: 64} &
{topic: "JV", payload: 60}
and combines them to give
{topic: "JV", payload: {JV: 60, WEST: 64}}

The topic in the combined message comes from whichever input message was the latest.
One might argue that the join node ought to delete msg.topic.

Definitely not, it can be essential to know which input generated the output. Often one may only want to take action after a particular input is received.

Sorry, getting off-topic here.

Fair enough, if there are only two inputs and the input frequency is moderate.

There is a timeout option on the join node to ensure messages are not too stale when they are passed on.

What if there are 5 input topics and you only want to take action if at least three of them are fresh? In this case the combined msg.topic is of no value.

But I am not saying the behaviour should be changed,just that it can be confusing. :grinning:

Here I present the whole mini-project for future inspiration.

The first implementation in NODE-RED here is the URL:

Cooling_Trackers_Rack/README_EN.md at main · hacesoft/Cooling_Trackers_Rack (github.com)

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