Simple function help

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 :slight_smile:

Maybe you need some else calls to go with your ifs

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

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?

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.

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

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.

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 :frowning:

@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

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

try

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

1 Like

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.

image

outputs that everytime :rofl:

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

1 Like

image

set to 2 ready for next line of text

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.

1 Like

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

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

1 Like

thats it! working now

Thanks !

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

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