Function node. How to refer to several messages?

I am looking for a method to use the function node to do some math with. There are multiple sensors in my flow providing me with data.
image

The way I would like to do this is to grab 1 message from a sensor. Grab another message from another sensor (sensor 2) and then subtract value sensor 2 from value sensor 1 with only using the function node

You can do it in 1 function node however you should probably use a join node to get the values into 1 message first.

See this article in the cookbook for an example of how to join messages into one object.

You can join the two messages into one using the Join node. But of course that fails "only using the sensor node".

Alternatively you can save the sensor1 reading in a context variable context.set('sensor1', msg.payload)
and use it when sensor2 reading arrives let answer = context.get('sensor1') - msg.payload
You can maybe use msg.topic to distinguish the two messages.

image
Indeed it did not work.

I can set a topic, but how do I refer to that in the change node?
New to this
image

Not sure how!

OK.

  1. I do not understand "how do I refer to that in the change node". You have not previously mentioned a change node, nor have we seen your flow.

  2. Your msg.payload is not a value 21.6 but an object {temperature: 26} so you access the value as msg.payload.temperature.

Here is a sample flow that uses a Join node to combine the two sensor readings into a single message, using the value of msg.topic to distinguish one from the other.
The function node just does the subtraction.
It's a bad idea to leap into coding javascript functions when you start with Node-red. It's seductive though because flow based and low-code programming is a different way of thinking.
I am certain that the subtraction can be done without the function node, via a Jsonata expression in a change node, maybe a jsonata expert will advise how.

[
    {
        "id": "5607f6657ab75a36",
        "type": "inject",
        "z": "7344b8ed03ec977f",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "sensor1",
        "payload": "21.6",
        "payloadType": "num",
        "x": 230,
        "y": 280,
        "wires": [
            [
                "4246d7e7ea568180"
            ]
        ]
    },
    {
        "id": "9b5bb5f8dc7cf909",
        "type": "inject",
        "z": "7344b8ed03ec977f",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "sensor2",
        "payload": "45.6",
        "payloadType": "num",
        "x": 230,
        "y": 320,
        "wires": [
            [
                "4246d7e7ea568180"
            ]
        ]
    },
    {
        "id": "4246d7e7ea568180",
        "type": "join",
        "z": "7344b8ed03ec977f",
        "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": 390,
        "y": 300,
        "wires": [
            [
                "2a7fe71d569a3ecc"
            ]
        ]
    },
    {
        "id": "2a7fe71d569a3ecc",
        "type": "function",
        "z": "7344b8ed03ec977f",
        "name": "",
        "func": "msg.payload = msg.payload.sensor2 - msg.payload.sensor1;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 540,
        "y": 300,
        "wires": [
            [
                "e9cfe571d7d9df77"
            ]
        ]
    },
    {
        "id": "e9cfe571d7d9df77",
        "type": "debug",
        "z": "7344b8ed03ec977f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 710,
        "y": 300,
        "wires": []
    }
]

Demo flow (use CTRL+I to import)

[{"id":"2064375997c3d07a","type":"inject","z":"c1ebfd3083f3904f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"sensor 1","payload":"18.9","payloadType":"num","x":990,"y":1540,"wires":[["440d073ef750cb4b"]]},{"id":"fda9172411bdbcec","type":"inject","z":"c1ebfd3083f3904f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"sensor 2","payload":"{ \"temperature\": 99 }","payloadType":"json","x":1040,"y":1580,"wires":[["30de3bb9e05be2a3"]]},{"id":"30de3bb9e05be2a3","type":"change","z":"c1ebfd3083f3904f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.temperature","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1300,"y":1580,"wires":[["440d073ef750cb4b"]]},{"id":"440d073ef750cb4b","type":"join","z":"c1ebfd3083f3904f","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":1490,"y":1540,"wires":[["2282069c1153400a","ada6b2435bb3f0c5"]]},{"id":"2282069c1153400a","type":"function","z":"c1ebfd3083f3904f","name":"","func":"msg.payload = msg.payload[\"sensor 2\"] - msg.payload[\"sensor 1\"]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1670,"y":1540,"wires":[["664bc70898372ce7"]]},{"id":"ada6b2435bb3f0c5","type":"debug","z":"c1ebfd3083f3904f","name":"after join","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1600,"y":1600,"wires":[]},{"id":"664bc70898372ce7","type":"debug","z":"c1ebfd3083f3904f","name":"after function","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1870,"y":1600,"wires":[]}]

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Thanks for the help and advise.

I got this in my debug, the actual sensors are connected. Why does the second debug say, not a number..?


There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

Again, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

You would pick up all the useful tips like the "copy path" one i gave you in the previous post.

The other thing which is sticking out to me is that in your screen shots BOTH messages have the same topic.

Post 8. Both Sensor 2

So there is something else going on here.
Then, one message is an object (or not a simple message) and the other is NaN....

Ah! Nan bread! :wink:
Sorry.
Bye.

Andrew, the OP definitely has individual topics - clear from the debug node named "after join node"

image

the other message with topic "Sensor 2" is from a different debug (a function after the join node)

So that is not the issue.

Sorry.... Seems I am not parsing things properly.

I'll shut up then. :wink:

Didn't mean to show I am THAT silly.

Thank you all. I found the problem... my topic was "Sensor 1" instead of "sensor 1"
Same with the other topic! I forgot they were capital letter sensitive....

It works!

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