# Compare 2 numbers for cpu fan pwm

**URL:** <https://discourse.nodered.org/t/compare-2-numbers-for-cpu-fan-pwm/31281>\
**Category:** General\
**Created:** [7 August 2020 17:27 UTC](https://discourse.nodered.org/t/compare-2-numbers-for-cpu-fan-pwm/31281 "2020-08-07T17:27:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![noob1505](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@noob1505](https://discourse.nodered.org/u/noob1505)\
**Post date:** [7 August 2020 17:27 UTC](https://discourse.nodered.org/t/compare-2-numbers-for-cpu-fan-pwm/31281/1 "2020-08-07T17:27:15Z")

</div>

I am a Node Red noob. but learning.

I want to make a fan pwm control. which the fan controls by pwm depending on the difference between measured temperature and setpoint.

I can't get the function right. what am i doing wrong?

Link from [export](http://www.ge.tt/6sdToD63)

```auto
context.node = context.node || 0;
context.node1 = context.node1 || 0;
var pwm;
pwm = pwm || msg.payload;
pwm = msg.payload
if(msg.topic == 'a'){
    context.node = msg.payload;
} else if (msg.topic == 'b'){
    context.node1 = msg.payload;
}

if (context.node > context.node1){
    pwm = pwm -=1
}else{
    pwm = pwm +=1
}
msg.payload = pwm
return msg;

```

For inputs i have two function blocks;

```auto
str = msg.payload
msg.payload = str.substring(5,9);
msg.payload = Number(msg.payload);
return msg;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 August 2020 17:56 UTC](https://discourse.nodered.org/t/compare-2-numbers-for-cpu-fan-pwm/31281/2 "2020-08-07T17:56:15Z")

</div>

This looks like an ideal candidate for node-red-contrib-pid.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [7 August 2020 18:04 UTC](https://discourse.nodered.org/t/compare-2-numbers-for-cpu-fan-pwm/31281/3 "2020-08-07T18:04:10Z")

</div>

Funny how things work... Everyone in my house is constantly tweaking the AC settings. One says, "I am cold." Another says, "I am MELTING."

I am getting tired of 'correcting' the settings, never mind being the AC police. So, figured I would let NR own it... and what do I see, the above thread.

Now I can create a flow that can (slowly, silently) return the AC settings to the 'correct' settings, such that evil AC trolls will not realize immediately that NR is returning the AC settings to their 'correct' settings.

---

<div class="post-metadata">

**Author:** ![noob1505](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@noob1505](https://discourse.nodered.org/u/noob1505)\
**Post date:** [7 August 2020 18:38 UTC](https://discourse.nodered.org/t/compare-2-numbers-for-cpu-fan-pwm/31281/4 "2020-08-07T18:38:18Z")

</div>

How can i set the pwm with the node-red-contrib-pid ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 August 2020 19:18 UTC](https://discourse.nodered.org/t/compare-2-numbers-for-cpu-fan-pwm/31281/5 "2020-08-07T19:18:32Z")

</div>

Do you know about pid control?

The node would be passed the setpoint and measured temperature and will output a number between 0 and 1 which you can scale to the pwm range you want for the fan. Then the node (once correctly tuned) will control the fan to give the required setpoint.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 August 2020 20:47 UTC](https://discourse.nodered.org/t/compare-2-numbers-for-cpu-fan-pwm/31281/6 "2020-08-07T20:47:08Z")

</div>

This might be of interest  
[http://blog.clanlaw.org.uk/pid-loop-tuning.html](http://blog.clanlaw.org.uk/pid-loop-tuning.html)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 August 2020 20:59 UTC](https://discourse.nodered.org/t/compare-2-numbers-for-cpu-fan-pwm/31281/7 "2020-08-07T20:59:09Z")

</div>

Going back to your function, it is not clear to me what it is trying to do. It sets `pwm` to `msg.payload` then either decrements or increments it by 1. At least I think that is what the rather unusual lines like

```auto
pwm = pwm -=1

```

do. I think that is the same as  
`pwm = pwm -1`  
or  
`pwm -= 1`

But that seems a bit strange since msg.payload coming in appears to be either the setpoint or the current temperature. So the pwm sent out is going to be either temperature +- 1 or setpoint +-1, which I imagine is not what you want.

By the way, the way you are accessing the context is not the recommended way now. See [https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

---

<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:** [6 October 2020 20:59 UTC](https://discourse.nodered.org/t/compare-2-numbers-for-cpu-fan-pwm/31281/8 "2020-10-06T20:59:29Z")

</div>

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