# Function to compare two inputs

**URL:** <https://discourse.nodered.org/t/function-to-compare-two-inputs/70037>\
**Category:** General\
**Tags:** database, function-node\
**Created:** [3 November 2022 15:33 UTC](https://discourse.nodered.org/t/function-to-compare-two-inputs/70037 "2022-11-03T15:33:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![everklein](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@everklein](https://discourse.nodered.org/u/everklein)\
**Post date:** [3 November 2022 15:33 UTC](https://discourse.nodered.org/t/function-to-compare-two-inputs/70037/1 "2022-11-03T15:33:35Z")

</div>

Hello,  
I'm trying to create a function that takes two separate inputs.  
Input 1 is boolean (true or false) and Input 2 is an integer.  
The function should compare the two inputs and every time Input 1 is true it should return the value of Input 2.  
The Input 2 value will be written to a database.

The goal is to write the value of entry 2 to the database every time entry 1 is true.

Can someone help me?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 November 2022 15:51 UTC](https://discourse.nodered.org/t/function-to-compare-two-inputs/70037/2 "2022-11-03T15:51:39Z")

</div>

When you say it takes two inputs do you mean two separate messages that come in at different times? If so then you can use a Join node to combine them into one message so you can compare them. See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 November 2022 15:56 UTC](https://discourse.nodered.org/t/function-to-compare-two-inputs/70037/3 "2022-11-03T15:56:32Z")

</div>

Not a function as requested, but here is a Join and a Switch node to achieve it.  
In my opinion it is a mistake to leap in to writing functions when there are other nodes to do most things.

Note that an input value does not just hang around waiting for you to use it. The Join node retains the last message received for each msg.topic, so you have to ensure that your inputs have appropriate topics.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/087cf9e3ab3e3448c59130168099c0c1e3c9a864.jpeg)

```auto
[{"id":"799c56ac99bde8c9","type":"inject","z":"da87fe02d823a937","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"boolean","payload":"true","payloadType":"bool","x":170,"y":100,"wires":[["3e30386179f001f4"]]},{"id":"c89cd7587740c733","type":"inject","z":"da87fe02d823a937","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"boolean","payload":"false","payloadType":"bool","x":170,"y":140,"wires":[["3e30386179f001f4"]]},{"id":"3e30386179f001f4","type":"join","z":"da87fe02d823a937","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":370,"y":180,"wires":[["b48d1b2a54419f70"]]},{"id":"8898b5f472e0ad9e","type":"debug","z":"da87fe02d823a937","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.number","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":180,"wires":[]},{"id":"b48d1b2a54419f70","type":"switch","z":"da87fe02d823a937","name":"","property":"payload.boolean","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":510,"y":180,"wires":[["8898b5f472e0ad9e"]]},{"id":"c73573682a3daa1e","type":"inject","z":"da87fe02d823a937","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"number","payload":"42","payloadType":"num","x":180,"y":200,"wires":[["3e30386179f001f4"]]},{"id":"9d5a6d4411145a85","type":"inject","z":"da87fe02d823a937","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"number","payload":"126","payloadType":"num","x":170,"y":280,"wires":[["3e30386179f001f4"]]},{"id":"176d45aa9c293463","type":"inject","z":"da87fe02d823a937","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"number","payload":"84","payloadType":"num","x":180,"y":240,"wires":[["3e30386179f001f4"]]}]

```

---

<div class="post-metadata">

**Author:** ![everklein](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@everklein](https://discourse.nodered.org/u/everklein)\
**Post date:** [3 November 2022 22:29 UTC](https://discourse.nodered.org/t/function-to-compare-two-inputs/70037/4 "2022-11-03T22:29:55Z")

</div>

Hello,  
I don't want to put two values ​​together in a message.  
What I want is the value of Input 2 when Input 1 is true.

---

<div class="post-metadata">

**Author:** ![everklein](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@everklein](https://discourse.nodered.org/u/everklein)\
**Post date:** [3 November 2022 22:33 UTC](https://discourse.nodered.org/t/function-to-compare-two-inputs/70037/5 "2022-11-03T22:33:57Z")

</div>

Hello,

I tested your solution, but every time I get the value TRUE in INPUT 1, the numeric message from INPUT 2 is not sent in debug.

The node I'm using in the input is "node-red-contrib-s7"  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/77582e99bbf735631975bb645accca6cfacd783e.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 November 2022 22:41 UTC](https://discourse.nodered.org/t/function-to-compare-two-inputs/70037/6 "2022-11-03T22:41:44Z")

</div>

Export your flow and share it here.  
Put a debug node after the join and show us what comes from that.

Are you sure that the boolean value is boolean true/false and not "TRUE"/"FALSE"?

---

<div class="post-metadata">

**Author:** ![everklein](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@everklein](https://discourse.nodered.org/u/everklein)\
**Post date:** [3 November 2022 23:04 UTC](https://discourse.nodered.org/t/function-to-compare-two-inputs/70037/7 "2022-11-03T23:04:33Z")

</div>

![Capturar](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/002aafed48742ae431c0f14179fcb7aab7eea007.png)

DEBUG 3  
{"ANALOG INPUT TEMP.":13645,"boolean":true}

`[{ "id": "799c56ac99bde8c9", "type": "inject", "z": "d9cbdfa3708592b2", "name": "", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" }], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "boolean", "payload": "true", "payloadType": "bool", "x": 410, "y": 160, "wires": [[ "3e30386179f001f4"] ] }, { "id": "c89cd7587740c733", "type": "inject", "z": "d9cbdfa3708592b2", "name": "", "props": [{ "p": "payload" }, { "p": "topic", "vt": "str" }], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "boolean", "payload": "false", "payloadType": "bool", "x": 410, "y": 200, "wires": [[ "3e30386179f001f4"] ] }, { "id": "3e30386179f001f4", "type": "join", "z": "d9cbdfa3708592b2", "name": "", "mode": "custom", "build": "object", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": "\\n", "joinerType": "str", "accumulate": true, "timeout": "", "count": "2", "reduceRight": false, "reduceExp": "", "reduceInit": "", "reduceInitType": "", "reduceFixup": "", "x": 610, "y": 240, "wires": [[ "b48d1b2a54419f70", "dc43067f0905f0ea"] ] }, { "id": "8898b5f472e0ad9e", "type": "debug", "z": "d9cbdfa3708592b2", "name": "debug 1", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload.number", "targetType": "msg", "statusVal": "", "statusType": "auto", "x": 900, "y": 240, "wires": [] }, { "id": "b48d1b2a54419f70", "type": "switch", "z": "d9cbdfa3708592b2", "name": "", "property": "payload.boolean", "propertyType": "msg", "rules": [{ "t": "true" }], "checkall": "true", "repair": false, "outputs": 1, "x": 750, "y": 240, "wires": [[ "8898b5f472e0ad9e"] ] }, { "id": "dc43067f0905f0ea", "type": "debug", "z": "d9cbdfa3708592b2", "name": "debug 3", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 780, "y": 140, "wires": [] }, { "id": "8533922611fd9a69", "type": "s7 in", "z": "d9cbdfa3708592b2", "endpoint": "6dba5846d06c8294", "mode": "single", "variable": "ANALOG INPUT TEMP.", "diff": false, "name": "ANALOG INPUT TEMP.", "x": 380, "y": 300, "wires": [[ "3e30386179f001f4"] ] }`

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 November 2022 23:28 UTC](https://discourse.nodered.org/t/function-to-compare-two-inputs/70037/8 "2022-11-03T23:28:56Z")

</div>

In my example, after the Join, msg.payload contains {"boolean":true,"number":84}.  
Debug 1 is showing the value of msg.payload.number.

Your msg.payload contains {"ANALOG INPUT TEMP.":13645,"boolean":true} so your debug1 should be set to show msg.payload."ANALOG INPUT TEMP.", not msg.payload.number.  
Or maybe msg.payload["ANALOG INPUT TEMP."] (not sure how to handle the case where an element name contains spaces and punctuation)

---

<div class="post-metadata">

**Author:** ![everklein](https://avatars.discourse-cdn.com/v4/letter/e/a87d85/32.png) [@everklein](https://discourse.nodered.org/u/everklein)\
**Post date:** [4 November 2022 01:08 UTC](https://discourse.nodered.org/t/function-to-compare-two-inputs/70037/9 "2022-11-04T01:08:17Z")

</div>

Hello,

It worked by setting msg.payload["ANALOG INPUT TEMP."] .  
I created the following function

`var temp = msg.payload["ANALOG INPUT TEMP."]; var bool = msg.payload.boolean; if (bool == "true"){ msg.payload = temp; return msg; } return msg;`

Thank you very much

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 November 2022 01:08 UTC](https://discourse.nodered.org/t/function-to-compare-two-inputs/70037/10 "2022-11-18T01:08:25Z")

</div>

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