# Simple function help

**URL:** <https://discourse.nodered.org/t/simple-function-help/47789>\
**Category:** General\
**Created:** [29 June 2021 11:15 UTC](https://discourse.nodered.org/t/simple-function-help/47789 "2021-06-29T11:15:30Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![leftymuller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leftymuller/32/102326_2.png) [@leftymuller](https://discourse.nodered.org/u/leftymuller)\
**Post date:** [29 June 2021 11:15 UTC](https://discourse.nodered.org/t/simple-function-help/47789/1 "2021-06-29T11:15:30Z")

</div>

Hey Gents,

I wrote a function that spins messages around, doesnt seem to work

function setrainfall() {  
msg.payload = global.get('rainfall');  
global.set('rotation')==2;  
}

function sethumidity() {  
msg.payload = global.get('humidity');  
global.set('rotation')==3;  
}

function setwindspeed() {  
msg.payload = global.get('windspeed');  
global.set('rotation')==1;  
}

if (global.get('rotation') ==1) { setrainfall() }  
if (global.get('rotation') ==2) { sethumidity() }  
if (global.get('rotation') ==3) { setwindspeed() }  
return msg;

any javascript help apprecciated 🙂

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 June 2021 11:22 UTC](https://discourse.nodered.org/t/simple-function-help/47789/2 "2021-06-29T11:22:18Z")

</div>

Maybe you need some else calls to go with your ifs

---

<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:** [29 June 2021 11:27 UTC](https://discourse.nodered.org/t/simple-function-help/47789/3 "2021-06-29T11:27:26Z")

</div>

> [@leftymuller](#):
>
> global.set('rotation')==1

Those should all be like `global.set('rotation', 1)`

---

<div class="post-metadata">

**Author:** ![leftymuller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leftymuller/32/102326_2.png) [@leftymuller](https://discourse.nodered.org/u/leftymuller)\
**Post date:** [29 June 2021 11:40 UTC](https://discourse.nodered.org/t/simple-function-help/47789/4 "2021-06-29T11:40:17Z")

</div>

global.set('rotation', 1);  
function setrainfall() {  
msg.payload = global.get('rainfall');  
global.set('rotation', 2);  
}

function sethumidity() {  
msg.payload = global.get('humidity');  
global.set('rotation', 3);  
}

function setwindspeed() {  
msg.payload = global.get('windspeed');  
global.set('rotation', 1);  
}

if (global.get('rotation'), 1) { setrainfall() }  
if (global.get('rotation'), 2) { sethumidity() }  
if (global.get('rotation'), 3) { setwindspeed() }  
return msg;

still get undefined payload?

---

<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:** [29 June 2021 12:37 UTC](https://discourse.nodered.org/t/simple-function-help/47789/5 "2021-06-29T12:37:09Z")

</div>

Are you setting globals rainfall etc?

Also I don't see that function is performing any useful function. You set rotation to 1 at the start which will cause setrainfall to be cause, which sets it to 2 so then sethumidity will be called which sets it 3 so then setwindspeed will be called which sets it back to 1 again. msg.payload will always be the windspeed value.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 June 2021 12:39 UTC](https://discourse.nodered.org/t/simple-function-help/47789/6 "2021-06-29T12:39:00Z")

</div>

> [@leftymuller](#):
>
> if (global.get('rotation'), 1) { setrainfall() }  
> if (global.get('rotation'), 2) { sethumidity() }  
> if (global.get('rotation'), 3) { setwindspeed() }

you seem to have edited your if statements.  
Now there are no comparisons

---

<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:** [29 June 2021 12:50 UTC](https://discourse.nodered.org/t/simple-function-help/47789/7 "2021-06-29T12:50:21Z")

</div>

I had missed that, @leftymuller you should not have edited the global.get statements, just the global.set. Go and look at the docs for using context again.

---

<div class="post-metadata">

**Author:** ![leftymuller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leftymuller/32/102326_2.png) [@leftymuller](https://discourse.nodered.org/u/leftymuller)\
**Post date:** [29 June 2021 12:58 UTC](https://discourse.nodered.org/t/simple-function-help/47789/8 "2021-06-29T12:58:57Z")

</div>

global.set('rotation', 1);  
function setrainfall() {  
msg.payload = global.get('rainfall');  
global.set('rotation', 2);  
}

function sethumidity() {  
msg.payload = global.get('humidity');  
global.set('rotation', 3);  
}

function setwindspeed() {  
msg.payload = global.get('windspeed');  
global.set('rotation', 1);  
}

if (global.get('rotation') == 1) { setrainfall() }  
if (global.get('rotation') == 2) { sethumidity() }  
if (global.get('rotation') == 3) { setwindspeed() }  
return msg;

still gives payload undefined ☹

@colin the global.set for humidity, windspeed and rainfall are set and working.. currently im using timed injects to achieve the changing of the weather but its clunky and gets out of time.. thats why i thought a function was a more elegant way of doing it

```auto
[{"id":"eb3d7fa.90ab38","type":"tab","label":"test page","disabled":false,"info":""},{"id":"fe1746ff.1fbec8","type":"function","z":"eb3d7fa.90ab38","name":"","func":"global.set('rotation', 1);\nfunction setrainfall() {\n msg.payload = global.get('rainfall');\n global.set('rotation', 2);\n }\n\nfunction sethumidity() {\n msg.payload = global.get('humidity');\n global.set('rotation', 3);\n }\n \nfunction setwindspeed() {\n msg.payload = global.get('windspeed');\n global.set('rotation', 1);\n } \n \nif (global.get('rotation') == 1) { setrainfall() }\nif (global.get('rotation') == 2) { sethumidity() }\nif (global.get('rotation') == 3) { setwindspeed() }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":220,"wires":[["1b6d5bac.08e2a4"]]},{"id":"30e85221.555c3e","type":"inject","z":"eb3d7fa.90ab38","name":"","props":[{"p":"trigger1","v":"ok","vt":"str"}],"repeat":"20","crontab":"","once":true,"onceDelay":0.1,"topic":"","x":220,"y":220,"wires":[["fe1746ff.1fbec8"]]},{"id":"1b6d5bac.08e2a4","type":"debug","z":"eb3d7fa.90ab38","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":560,"y":220,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 June 2021 13:03 UTC](https://discourse.nodered.org/t/simple-function-help/47789/9 "2021-06-29T13:03:35Z")

</div>

try

```auto
if (global.get('rotation') == 1) { setrainfall() }
else if (global.get('rotation') == 2) { sethumidity() }
else if (global.get('rotation') == 3) { setwindspeed() }
else {setrainfall()}
return msg;

```

as maybe global rotation is not set

---

<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:** [29 June 2021 13:03 UTC](https://discourse.nodered.org/t/simple-function-help/47789/10 "2021-06-29T13:03:54Z")

</div>

Add `node.warn()` statements at relevant places to work out what is happening. If you don't know how to do that I think it is referenced in the Writing Functions doc.

---

<div class="post-metadata">

**Author:** ![leftymuller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leftymuller/32/102326_2.png) [@leftymuller](https://discourse.nodered.org/u/leftymuller)\
**Post date:** [29 June 2021 13:07 UTC](https://discourse.nodered.org/t/simple-function-help/47789/11 "2021-06-29T13:07:03Z")

</div>

> [@E1cid](#):
>
> ```auto
> if (global.get('rotation') == 1) { setrainfall() }
> else if (global.get('rotation') == 2) { sethumidity() }
> else if (global.get('rotation') == 3) { setwindspeed() }
> else {setrainfall()}
> return msg;
> 
> ```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/0/a05ceca36075caca23e4afcdc36622f569d1fe1d.png)

outputs that everytime 🤣

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 June 2021 13:12 UTC](https://discourse.nodered.org/t/simple-function-help/47789/12 "2021-06-29T13:12:56Z")

</div>

Looks like global rotation is not set. Can you show the value of it in the context panel.

---

<div class="post-metadata">

**Author:** ![leftymuller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leftymuller/32/102326_2.png) [@leftymuller](https://discourse.nodered.org/u/leftymuller)\
**Post date:** [29 June 2021 13:18 UTC](https://discourse.nodered.org/t/simple-function-help/47789/13 "2021-06-29T13:18:50Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/c/ec7bbefbaaad7f8c0c706657bb7cf27376f10620.png)

set to 2 ready for next line of text

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 June 2021 13:21 UTC](https://discourse.nodered.org/t/simple-function-help/47789/14 "2021-06-29T13:21:23Z")

</div>

Why limit the info we see?

is that node, flow or global?

if that is global , then there is more happening in your flow and you will have to post it.

---

<div class="post-metadata">

**Author:** ![leftymuller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leftymuller/32/102326_2.png) [@leftymuller](https://discourse.nodered.org/u/leftymuller)\
**Post date:** [29 June 2021 13:22 UTC](https://discourse.nodered.org/t/simple-function-help/47789/15 "2021-06-29T13:22:48Z")

</div>

thanks e1cid.. i posted the flow.. here is all the context data

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/59f1103e2b549c0661538ed9bd6e2a385c4a49c7.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 June 2021 13:25 UTC](https://discourse.nodered.org/t/simple-function-help/47789/16 "2021-06-29T13:25:35Z")

</div>

The issue is you set rotation to 1 at beginning of function node, delete line 1

---

<div class="post-metadata">

**Author:** ![leftymuller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leftymuller/32/102326_2.png) [@leftymuller](https://discourse.nodered.org/u/leftymuller)\
**Post date:** [29 June 2021 13:30 UTC](https://discourse.nodered.org/t/simple-function-help/47789/17 "2021-06-29T13:30:07Z")

</div>

thats it! working now

Thanks !

One thing I love about node red is the community and the willingness to help.. thanks again 🙂

---

<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:** [28 August 2021 13:30 UTC](https://discourse.nodered.org/t/simple-function-help/47789/18 "2021-08-28T13:30:17Z")

</div>

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