# Node function problem

**URL:** <https://discourse.nodered.org/t/node-function-problem/50548>\
**Category:** General\
**Created:** [2 September 2021 20:20 UTC](https://discourse.nodered.org/t/node-function-problem/50548 "2021-09-02T20:20:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![theSushi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thesushi/32/47459_2.png) [@theSushi](https://discourse.nodered.org/u/theSushi)\
**Post date:** [2 September 2021 20:20 UTC](https://discourse.nodered.org/t/node-function-problem/50548/1 "2021-09-02T20:20:10Z")

</div>

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.

```auto
[
    {
        "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"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 September 2021 20:42 UTC](https://discourse.nodered.org/t/node-function-problem/50548/2 "2021-09-02T20:42:34Z")

</div>

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

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

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

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

```

... which is EXACTLY the same as

```auto
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/343dc22481a17e3116ea9c389b2e6c3b5005c33e.gif)

---

<div class="post-metadata">

**Author:** ![theSushi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thesushi/32/47459_2.png) [@theSushi](https://discourse.nodered.org/u/theSushi)\
**Post date:** [2 September 2021 20:58 UTC](https://discourse.nodered.org/t/node-function-problem/50548/3 "2021-09-02T20:58:09Z")

</div>

> [@Steve-Mcl](#):
>
> the first 2 lines do absolutely nothing

I know this is only reminder for me.

> [@Steve-Mcl](#):
>
> the formatting makes it very difficult to understand

Sorry I am not programer. I still learn.

> [@Steve-Mcl](#):
>
> If that is what you are trying to do then an RBE node will be the simplest solution...

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 September 2021 21:02 UTC](https://discourse.nodered.org/t/node-function-problem/50548/4 "2021-09-02T21:02:23Z")

</div>

> [@theSushi](#):
>
> I know this is only reminder for me.

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

> [@theSushi](#):
>
> Sorry I am not programer. I still learn.

No need to apologise 🙂

> [@theSushi](#):
>
> I didn't know for RBE. They do what i need.

Good

  

Is this now solved for you?

---

<div class="post-metadata">

**Author:** ![theSushi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thesushi/32/47459_2.png) [@theSushi](https://discourse.nodered.org/u/theSushi)\
**Post date:** [2 September 2021 21:13 UTC](https://discourse.nodered.org/t/node-function-problem/50548/5 "2021-09-02T21:13:05Z")

</div>

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.

---

<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:** [16 September 2021 21:13 UTC](https://discourse.nodered.org/t/node-function-problem/50548/6 "2021-09-16T21:13:32Z")

</div>

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