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.
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 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.
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.
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.
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.
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.
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.
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....