Compare 2 numbers for cpu fan pwm

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

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
    pwm = pwm +=1
msg.payload = pwm
return msg;

For inputs i have two function blocks;

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

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

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.

1 Like

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

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.

This might be of interest

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

pwm = pwm -=1

do. I think that is the same as
pwm = pwm -1
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