The same condition does different things

I made a simple flow,

it contains a node function that sets 2 variables to a certain numerical value, then the node function compares the numerical values of the two variables and sets one variable to a certain string of characters based on the result :

And finally a debug node to see the value of the "msg.result" variable, so IF msg.test1>msg.test2 THEN msg.result=More sites than entities, IF msg.test1<msg.test2 THEN msg.result=Less sites than entities

So far, no worries about the flow working as it should, now I want to reproduce this flow but without setting "msg.test1" and "msg.test2" to a value I choose the value of these 2 variables will be decided by a http request

I have tried several methods but without success and I don't understand why it doesn't work, here is the 1st method:

In this flow we have 2 http request nodes that allow me to retrieve information from a web page accessible from my local network. After getting the information from the web page I sort the information with my 2 nodes function


After having sorted this information, I have only one numerical value left in the information received thanks to the https request nodes, so I set my variables to the same value as the numerical values I have left (the msg.test1 variable takes the numerical value from the first http request and the msg.test2 takes that from the second http request)


Once msg.test1 and msg.test2 have a numerical value I compare them using a swicth node


but no matter what the values of msg.test1 and msg.test2 are the result is wrong, for example IF msg.test1=663 and msg.test2=664 I should see that the debug node which is present in the output of the second condition (msg.test1<msg. test2) but yet I see the debug node that is in the output of the second and third condition as if the flow is telling me that msg.test1>msg.test2 and at the same time msg.test1<msg.test2, so I don't understand why, so this was my first method that I can't get to work.

here is the 2nd method:

this method works almost the same as the first one, we make 2 http request to get information from a web page accessible from my local network, I sort this information with the 2 function nodes at the output of the http request nodes, but in addition to sorting the information, the function nodes set directly the variables msg.test1 and msg.test2 without passing by a change node


then to compare the values of msg.test1 and msg.test2 I use another function node, which uses the same function that was functional in my first simple flow

but it doesn't work either my msg.result is "undefined" while msg.test1<msg.test2 and therefore msg.result should be equal to 'Plus dentité que de site !!!'

I think I have put all the necessary information, do not hesitate to ask me for additional information if you need it, thank you in advance

Let's start step by step:

What does the http-request node return to you?
Is it a number or a String?

If it's a String you can use Number(value) to convert it.
You can use variable instead of the message property:

const num1 = msg.payload.numberOne;
const num2 = msg.payload.numberTwo;

PS: Use " instead of ' for your msg.result in order to have d'entité.

Show us what you see in a debug node going into function 51 and another showing what is coming out and paste the results here.

In future when posting code please copy/paste the text rather than use screenshots, in case we need to pick up bits of it. When pasting use the </> button so that the forum does not mess with the formatting.

From what i can tell your msg.test1 and msg.test2 are in different messages
You will need to join them, this article in the cookbook shows how to join them. Create a single message from separate streams of messages : Node-RED
Once in same message then you can compare them

the http node returns a JSON object but after sorting the info in this JSON object I only have a numerical value left in the JSON object and I set my msg.test variable to the numerical value in the JSON object

Here is the object returned by the http request:

here is the object returned after sorting the information

and here is the value of msg.test1 (defined by the command "msg.test1=msg.payload.totalcount")

I show you that debug node 72 and 76 allow you to see the values of msg.test1 and msg.test2


and here are the incoming values at node function 51:


I'll give it a try and let you know

TThank you very much to all 3 of you for your help, your leads and for your reactivity !!!!

Ok I see, the first step is to join your two messages, as there is no topic, you have to create one to identify the message, use a change node (add this node after each http-request node):

Then you use a join node as shown in the link of E1cid's message.

Then the final step is to compare your values:

const num1 = msg.payload.num1.count;
const num2 = msg.payload.num2.count;
if (num1 == num2) {...}

The topic used becomes the key of your object.

You could also run the http request sequentially
test1 http > function/change set msg.testt1 > test2 http > function/change set msg.test2 > function compare test1 and test2

I'm really sorry but I don't understand much, so I'm trying to realize what you showed me in your answer, but I have some problems

  • I don't understand why the change node should be after the http request nodes and not after the node function that sorts the information

  • I can't understand what "num1" is in my case

  • and I don't understand what the change block is for, the msg.payload doesn't interest me, the only values I'm interested in are msg.test1 and msg.tets2

I tried to reproduce what you showed me but since I didn't understand well I don't see where the problem is

my flow now looks like this:

In my function nodes 65 and 66 I have this:
I sort the info and set msg.test2 and msg.test1 to the value I want

After that I added a change node as you advised, as I don't know what num1 and num2 correspond to in your example I assumed that num1=msg.test1 and that num2=msg.test2


And finally a node join as you advised, unfortunately I'm really new to NodeRed so I didn't quite understand the need for the 2 values to be in the same message to be compared so I just stupidly copied what you showed me


and to finish I compare my 2 values to know which one is the biggest, I even added a condition so that a message is still displayed on the debug console even if it did not manage to compare the values but no message appears on my debug console except the values of msg.test1 and msg.test2 which are displayed thanks to the node debug 83 and 84


I still don't see a message saying that one value is bigger than the other. Obviously I suspect that the problem is with my configuration and not with what you have shown me, but I can't adapt it to my case,

sorry to waste your time with things like this

I use the change node to add a topic that identifies whether the message comes from your first http-request node or the second one.
The sorting can be done in the function node, it's a simplification. In your case you delete what you are not interested in but we can also ignore it and take what interests you, namely totalcount.

I use num1 like you use msg.test1.

Basically what happens is:
I add a topic to your query response then I join the two queries (so we have both messages in one) then in the function node I compare your numbers (no filter needed because I go directly to totalcount ).
In your function 67 you must replace msg.test1 by totalcount1 because as said before it is the same value but without filtering your message unnecessarily.
Small change:

const totalcount1 = msg.payload.num1.totalcount;
const totalcount2 = msg.payload.num2.totalcount;
if (totalcount1 == totalcount2) {...}

Because you want totalcount instead of count.

Do not forget to mention 2 (because you expect 2 messages to be joined) as message number and uncheck the "subsequent" option in your join node.

Try with this :

[{"id":"3ef9e8b2772f3ec4","type":"inject","z":"0611fdba4878ab79","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":1240,"wires":[["2411c3a1ce662dcf","e4aef24679bbee34"]]},{"id":"2411c3a1ce662dcf","type":"http request","z":"0611fdba4878ab79","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{"user":"","password":""},"x":290,"y":1200,"wires":[["686fa1a75c3d782e"]]},{"id":"e4aef24679bbee34","type":"http request","z":"0611fdba4878ab79","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{"user":"","password":""},"x":290,"y":1280,"wires":[["c1e3f0219d3800cc"]]},{"id":"686fa1a75c3d782e","type":"change","z":"0611fdba4878ab79","name":"requestOne","rules":[{"t":"set","p":"topic","pt":"msg","to":"requestOne","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":1200,"wires":[["0282e907583b4163"]]},{"id":"c1e3f0219d3800cc","type":"change","z":"0611fdba4878ab79","name":"requestTwo","rules":[{"t":"set","p":"topic","pt":"msg","to":"requestTwo","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":1280,"wires":[["0282e907583b4163"]]},{"id":"0282e907583b4163","type":"join","z":"0611fdba4878ab79","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":650,"y":1240,"wires":[["2afd66fedfd602db"]]},{"id":"2afd66fedfd602db","type":"function","z":"0611fdba4878ab79","name":"function 2","func":"const totalCount1 = msg.payload.requestOne.totalcount;\nconst totalCount2 = msg.payload.requestTwo.totalCount;\nlet result = \"merde\";\n\nif (totalCount1 == totalCount2) {\n    result = \"Autant\";\n} else if (totalCount1 > totalCount2) {\n    result = \"Plus de site\";\n} else if (totalCount1 < totalCount2) {\n    result = \"Plus d'entité\";\n}\n\nmsg = {\n    payload: result\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":1240,"wires":[["0ae522f242d76c2c"]]},{"id":"0ae522f242d76c2c","type":"debug","z":"0611fdba4878ab79","name":"debug 15","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":960,"y":1240,"wires":[]}]

(ctrl + i) then paste the flow above.

so I imported your flow into mine, so I now have something that looks like this:

Indeed I completely agree with you, that I delete what does not interest me, or that I take only what interests me, that returns to the same thing except that in your case one has already 2 nodes in less what simplifies the flow

Unfortunately when I run the cycle, I only get the message "Shit" (Thanks for the translation :slight_smile: ) on my debug console as if no comparison has been validated


so I tried to modify your flow a bit but without success, I added some debug nodes so you can see what is in the output of the http request node, this is what the 1st request sends back to me :


This is what the 2nd request sends back to me :


I'm sorry to be such a jerk, but I don't understand when you get the totalcount value of the 2 http requests, because in the change nodes you set the value of the msg.topic to the value of the variable RequestTwo, but the variable RequestTwo/RequestOne is never defined. So I said to myself that I had to replace RequestTwo by payload.totalcount, so I modified the 2 change nodes as follows

I also added debug nodes to see the value of the msg.topic output from the 2 nodes change, this is what I get on my debug console :


I left the node join as such :


And I have modified the function node as follows :


And so I get the right result,


but I know that my method is catastrophic because it is enough that the totalcount which is equal to 663 changes to 665, and I have to modify manually the function node like this


Even better if the 2 totalcounts are equal to 664 then I have to modify the function node like this


And then NodeRed will compare twice the same values, and it will necessarily say that the two values are equal because they are the same, it's like if I do :
set test1=1
If (test1 == test1)

it's a calimity :rofl:

Sorry, I made a typo in the function node:

- const totalCount2 = msg.payload.requestTwo.totalCount;
+ const totalCount2 = msg.payload.requestTwo.totalcount;

Thanks for your help, it works perfectly!

Great support !!!

Have a nice days

1 Like

Pareillement :wink:

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