Node function problem

I have problem with function... I can read is it my temperature to high or to low. But I wish to dont send me status if is not change. ( if is status Hight, High i dont need info only if my status is changed. Can any one sugest me what I am wrong make.

[
    {
        "id": "3bb13491680faca4",
        "type": "function",
        "z": "d9f52850f7c1c06f",
        "name": "",
        "func": "\"var kut0 = 2\"\n\"var kut90 = 7\"\nvar temp=msg.payload;\nif (temp>=30) { msg.payload = \"7\";}\nelse {msg.payload = \"2\";}\n\nvar old = msg.payload; \nif (old != msg.payload) return 0;\nelse {old = msg.payload;\nreturn msg;\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 1000,
        "wires": [
            [
                "8dd4c576a9272708",
                "d7372593905113f2"
            ]
        ]
    }
]

There was no real need to post a flow with 1 function node - it would have been better to just post the code...

"var kut0 = 2"
"var kut90 = 7"
var temp=msg.payload;
if (temp>=30) { msg.payload = "7";}
else {msg.payload = "2";}

var old = msg.payload; 
if (old != msg.payload) return 0;
else {old = msg.payload;
return msg;
}

Ok, so where to start...

  1. the first 2 lines do absolutely nothing
  2. the formatting makes it very difficult to understand

So lets fix them first...

var temp = msg.payload;
if (temp >= 30) { 
    msg.payload = "7";
} else {
    msg.payload = "2";
}

var old = msg.payload; 
if (old != msg.payload) {
    return 0;
} else {
    old = msg.payload;
    return msg;
}

Better - now I can read the code but unfortunately I cant really see what you are trying to do!

var old = msg.payload; 
if (old != msg.payload) {

... which is EXACTLY the same as

if (msg.payload != msg.payload) {

... which will ALWAYS be false

Also, to halt a flow, you should return null not return 0


Lets step back a bit...

You say "But I wish to dont send me status if is not change"
which I think you mean "Only send value if it changes"?

If that is what you are trying to do then an RBE node will be the simplest solution...

fHxW5BoqSF

1 Like

I know this is only reminder for me.

Sorry I am not programer. I still learn.

I didn't know for RBE. They do what i need. But not everything works the way I thought.

ok, thats fine, however you should use // or /* */ for comments and reminders.

No need to apologise :slight_smile:

Good


Is this now solved for you?

1 Like

it is solved for with RBE. But now I see that my signal is standing all the time and I should just send that impulse (1s -2s) and it is gone. Until a different one comes along. Because it goes to a GPIO that is connected to a servo motor and now the servo motor receives a pulse all the time and complains a bit because the PWM of the GPIO is not perfect.

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