# Function node no negative values

**URL:** <https://discourse.nodered.org/t/function-node-no-negative-values/83349>\
**Category:** General\
**Tags:** function-node\
**Created:** [1 December 2023 13:44 UTC](https://discourse.nodered.org/t/function-node-no-negative-values/83349 "2023-12-01T13:44:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bestquaker](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@bestquaker](https://discourse.nodered.org/u/bestquaker)\
**Post date:** [1 December 2023 13:44 UTC](https://discourse.nodered.org/t/function-node-no-negative-values/83349/1 "2023-12-01T13:44:56Z")

</div>

Hello together,

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

```auto
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) }

```

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [1 December 2023 13:50 UTC](https://discourse.nodered.org/t/function-node-no-negative-values/83349/2 "2023-12-01T13:50:50Z")

</div>

> [@bestquaker](#):
>
> `context.temp = context.temp || 0.0;`

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

```auto
let xy = context.get("temp")

```

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

---

<div class="post-metadata">

**Author:** ![bestquaker](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@bestquaker](https://discourse.nodered.org/u/bestquaker)\
**Post date:** [1 December 2023 13:54 UTC](https://discourse.nodered.org/t/function-node-no-negative-values/83349/3 "2023-12-01T13:54:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [1 December 2023 13:59 UTC](https://discourse.nodered.org/t/function-node-no-negative-values/83349/4 "2023-12-01T13:59:33Z")

</div>

> [@bestquaker](#):
>
> 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.

Understood.

> [@bestquaker](#):
>
> so for doing calculations I have to define variables first that get the values from the context variable?

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:

```auto
var spd_diff = 0;

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

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

```

---

<div class="post-metadata">

**Author:** ![bestquaker](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@bestquaker](https://discourse.nodered.org/u/bestquaker)\
**Post date:** [1 December 2023 14:42 UTC](https://discourse.nodered.org/t/function-node-no-negative-values/83349/5 "2023-12-01T14:42:10Z")

</div>

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.

```auto
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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [1 December 2023 15:36 UTC](https://discourse.nodered.org/t/function-node-no-negative-values/83349/6 "2023-12-01T15:36:05Z")

</div>

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

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

```

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

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 December 2023 16:08 UTC](https://discourse.nodered.org/t/function-node-no-negative-values/83349/7 "2023-12-01T16:08:21Z")

</div>

Hi @bestquaker 🙂

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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 January 2024 16:09 UTC](https://discourse.nodered.org/t/function-node-no-negative-values/83349/8 "2024-01-30T16:09:20Z")

</div>

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