Diff between values

Have following context values:
|Temp-Air|4.75|
|Temp-Ground|5.9375|

I need to calculate diff between them, so I put function :

flow.set('Temp-Diff', 'Temp-Air'-'Temp-Ground')

Result I got was error message, probably because data format, not sure how to fix this:

|Temp-Diff|NaN|

Hi @ajocius

Judging by your code, it looks as though you are new to JavaScript. I would recommend you go off do some simple online lessons.

Also, have a read of Writing Functions : Node-RED

var air = flow.get("Temp-Air") || 0;
var ground = flow.get("Temp-Ground") || 0;
flow.set('Temp-Diff', air - ground);

In simple terms, you need to get the value out of context and into a variable before you can do any maths.

You are absolutely right, I am new to this. Your suggested code worked great!

1 Like

I would like to set message load to "on" or "off" depending on temp diff value:

var TempD = flow.get("Temp-Diff") || 0;
if (TempD>8){msg.payload='on'}
if (TempD<=8){msg.payload='off'}
return msg;

However this seem to not work, not sure where is the problem

put node.warn() in the code to understand what is going on...

var TempD = flow.get("Temp-Diff") || 0;
node.warn("TempD = " + TempD);
if (TempD>8) {
  node.warn("TempD is > 8, setting msg.payload= 'on'");
  msg.payload='on'
} else if (TempD<=8) {
  node.warn("TempD is <= 8, setting msg.payload= 'off'");
  msg.payload='off'
}
return msg;

You really should read up...

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