Function to compare two inputs

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?

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 for an example of how to join messages into one object.

1 Like

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.

[{"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"]]}]
1 Like

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.

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

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"?

1 Like

Capturar

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" ] ] }

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)

1 Like

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

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