Global INT set vars -> NaN

Hi,

I think I've tried almost everything like Number(var), ParseInt(var,10), toFixed(1),...., but I'm getting always NaN in the context menu of Global vars.

See the function code:
var status=msg.payload.weather;
var xRain=0;
var iAuxCount,iRainCount=0;
var newmsg;

if (status.includes("Rain") || status.includes("rain")){
xRain=true;
iAuxCount=iAuxCount+1;
iRainCount=iAuxCount;
}
else{
xRain=false;
iAuxCount=0;
iRainCount=iAuxCount;
}

global.set("iAuxCount", iAuxCount);
global.set("iRainCount", iRainCount);
global.set("xRain", xRain);

return [iAuxCount,iRainCount];

And in the global context:

|iAuxCount|NaN|
|iRainCount|NaN|
|xRain|true|

Hi all,

I've made some changes, and it's working:

var status=msg.payload.weather;
var xRain=global.get('xRain')||0;
var iAuxCount=global.get('iAuxCount')||0;
var iRainCount=global.get('iRainCount')||0;

if (status.includes("Rain") || status.includes("rain")){
xRain=true;
iAuxCount+=1;
iRainCount=iAuxCount;
}
else{
xRain=false;
iAuxCount=0;
iRainCount=iAuxCount;
}

global.set("iAuxCount", iAuxCount);
global.set("iRainCount", iRainCount);
global.set("xRain", xRain);

msg.payload = {iAuxCount,iRainCount,xRain};
return msg;

Please read How to share code or flow json

Thank you @Steve-Mcl. I'll use it!

1 Like

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