Function node no negative values

Hello together,

I have the problem that my values in this case the variable "spd_diff" cant become negative. HEre is my code:

context.temp = context.temp || 0.0;
context.compspd = context.compspd || 0;
context.compspd_soll = context.compspd_soll || 0;
context.defrost = context.defrost || false;
var setting = 0;
var spd_diff = 0;

if (msg.topic === 'auslasstemp') {
    context.temp = msg.payload;
} else if (msg.topic === 'compressorspeed') {
    context.compspd = msg.payload;
} else if (msg.topic === 'abtauen') {
    context.defrost = msg.payload;
} else if (msg.topic === 'heatpump/solldrehzahl') {
    context.compspd_soll = msg.payload;
}
spd_diff = context.compspd_soll - context.compspd

if (spd_diff < 100 && spd_diff < (-100)) {
    setting = 0;
} else if (spd_diff >= 100 && spd_diff < 300) {
    setting = 1;
} else if (spd_diff >= 300 && spd_diff < 500) {
    setting = 2;
} else if (spd_diff >= 500 && spd_diff < 8000) {
    setting = 3;
} else if (spd_diff <= (-100) && spd_diff > (-300)) {
    setting = (-1);
} else if (spd_diff <= (-300) && spd_diff > (-500)) {
    setting = (-2);
} else if (spd_diff <= (-500) && spd_diff > (-8000)) {
    setting = (-3);
}

var steuertemp = ((setting + context.temp) * 10);


return { topic: 'steuertemp', payload: (steuertemp) }

Hi!
Welcome to this forum!
Before we're going to care about the issue you reported: What do you intend to achieve with this?
The syntax to get a value from the context is

let xy = context.get("temp")

I'm not sure what happens with your code; yet you wont get values from the context at all...

Hey,

thx for the fast reply. Let me introduce, Im a real beginner, so there are maybe stupid mistakes. I want to do a regulation depending on the real world values (in this case compressor speed of my heatpump) and the outgoing value should be the temp the pump should maintain. So I want to controll the compressorspeed.
so for doing calculations I have to define variables first that get the values from the context variable?

Understood.

Can't answer this yes or no: The context interface is defined as context.get(name) and context.set(name, value). If you intend to use this functionality, you have to operate via this API.
You might yet do calculations as well completely independent from any access to context.

And: You're touching an interesting point here. Could you do me a favour, do the following change to your code and post the result here - which should be a warning emitted at the debug panel:

var spd_diff = 0;

// please add the following line at this position
node.warn(context);
// thank you!

if (msg.topic === 'auslasstemp') {

now I changed the code to normal variables, but now nothing works anymore. I return the "temp" variable now only for testing, normally I would like to return "steuertemp" but thats 0.

var temp = 0;
var compspd = 0;
var compspd_soll = 0;
var defrost = 0;
var setting = 0;
node.warn(context);

if (msg.topic === 'auslasstemp') {
    temp = msg.payload;
} else if (msg.topic === 'compressorspeed') {
    compspd = msg.payload;
} else if (msg.topic === 'abtauen') {
    defrost = msg.payload;
} else if (msg.topic === 'heatpump/solldrehzahl') {
    compspd_soll = msg.payload;
}
var spd_diff = compspd_soll - compspd


if (spd_diff < 100 && spd_diff < (-100)) {
    setting = 0;
} else if (spd_diff >= 100 && spd_diff < 300) {
    setting = 1;
} else if (spd_diff >= 300 && spd_diff < 500) {
    setting = 2;
} else if (spd_diff >= 500 && spd_diff < 8000) {
    setting = 3;
} else if (spd_diff <= (-100) && spd_diff > (-300)) {
    setting = (-1);
} else if (spd_diff <= (-300) && spd_diff > (-500)) {
    setting = (-2);
} else if (spd_diff <= (-500) && spd_diff > (-8000)) {
    setting = (-3);
}

var steuertemp = ((setting + temp) * 10);


return { topic: 'steuertemp', payload: temp }

here is my output:

1.12.2023, 15:41:41node: function 12
function : (warn)
{ set: function, get: function, keys: function, global: object, flow: object }
1.12.2023, 15:41:41node: debug 19
steuertemp : msg.payload : number
32.4
1.12.2023, 15:41:41node: function 12
function : (warn)
{ set: function, get: function, keys: function, global: object, flow: object }
1.12.2023, 15:41:41node: debug 19
steuertemp : msg.payload : number
0
1.12.2023, 15:41:41node: function 12
function : (warn)
{ set: function, get: function, keys: function, global: object, flow: object }
1.12.2023, 15:41:41node: debug 19
steuertemp : msg.payload : number
0

If you intend to work with context, you should start with ...

let temp = context.get("temp") || 0;
let compspd = context.get("compspd") || 0;
...

... and add at the end of your script:

context.set("compspd", compspd);
context.set("temp", temp);

This pushes your latest data into context, so that you can get it back when running this node again, at the beginning of your script.

Hi @bestquaker :slightly_smiling_face:

You seem to have started in Node-red by writing everything in a function node.
That might not be the best strategy.

1 What control input does your heat pump expect? A target temperature? A speed?

2 Can you give us examples of the payload your flow needs to produce to control it:

  • When you want to increase the temperature, so the compressor must run faster.
  • When you want to decrease the temperature.

3 There seem to be four different input measurements, please describe where they come from, with examples?

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