Change a variable name with a variable

Hi there

is it psiisble to change a variable name with another variable... or is there a smarter way??

Doing son floor heating calculations I need multiple heating zones, in a function

I have defined these variables

01 var numbr=1;
02
03 var zone1flow=70; //litre/h
04 var zone1SP=msg.payload.targetValue;
05 var zone1temp=msg.payload.currentTemp;

how can i make a copy of the function and just change the number in line 01??

Best regards

Henrik

Do you mean you want to make a new function node with just that line different or do you mean you want to feed the 1 in via the message or something else. I am a bit confused as you don't seem to use the variable numbr.

When posting code please use the </> button at the top of the forum entry window and that will stop the forum from messing with it by interpreting it as Markdown.

Pass a number in msg.payload or msg.myprop or msg.anumber then use it in the function.

01 var numbr=1;
02
03 var zone1flow=70; //litre/h

03 var zone%numbr%flow=70; //litre/h

I want to skubstitude the number 1 in line 03 with the numbr value.. so I can make a copy of the function for multiple zones and just change the number i line 01..

What are you then doing with zone1flow that the name of it matters?

If what you want to do is to build up a set for zone1flow, zone2flow etc then you should be looking at javascript arrays. If you define zones as an array then you can do
zone[numbr] = {flow: 70, sp: msg.payload.targetValue, temp: msg.payload.currentTemp}

However, if you are doing this with multiple messages into a function node then you also need to look at the node red docs Writing Functions in particular the section Storing Data.

If you are interested in the dynamic variable name this will do:

var numbr = 1;
this['zone'+numbr+'flow'] = 70;
msg.payload = this['zone'+numbr+'flow'];
return msg;

Hi Collin

it has to be uniq as I am passing the value to as SVG node... and also to influxdb to show the histrorical data i grafana.

SVG dashboard

and the grafana dashboard:

all as part of a heatpump simulator..

What message do you want out of the zone1input node?

var numbr=1;
var now=(new Date().getTime());
var earlier=context.get('erlier')||now;
timeperiode=(now-earlier)/1000;
context.set('erlier',now);

//settings for room
var factor=1;
var areal=11.6;
var constant=0.05;
var wall=(3.5*2.4)+(3.23*2.4);
var windows=1.2*1.0;
var u_value=0.4;
var zone1flow=70; //liter/h
var CHTtemp=flow.get("CHTtemp")||10;
var tude=flow.get('tude');
var zone1SP=msg.payload.targetValue;
var zone1temp=msg.payload.currentTemp;
if (typeof zone1temp=="undefined"){
    zone1temp=tude
    }
var zone1heating = (msg.payload.currentHeaterStatus=="on")?1:0;
var ringcolor=(msg.payload.currentHeaterStatus=="on")?"red":"blue";
if (zone1SP<tude){
  zone1temp=tude;  
}

var zone1energy=(areal*constant*zone1temp);
if (typeof zone1energy=="undefined"){
    zone1energy=(areal*constant*tude)
    }
var zone1energyMin=areal*tude*constant;

if (zone1energy<zone1energyMin){
    zone1energy=zone1energyMin
}

var heatloss=(((wall*u_value*(zone1temp-tude)*timeperiode)/3600000)*factor);
//var heatloss=((wall*u_value*(zone1temp-tude)*timeperiode)*factor)/36000000;
var zone1input=((zone1flow/3600)*timeperiode*(CHTtemp-zone1temp+5)*4.19*zone1heating/3600);


zone1energy=(zone1energy-heatloss+zone1input);

zone1temp=Number((zone1energy/(areal*constant)));

node.status({fill:ringcolor,shape:"ring",text: zone1temp.toFixed(2)+"°C Q"+zone1energy.toFixed(2)+" P:"+zone1input.toFixed(5)+" -Q:"+heatloss.toFixed(5)});
flow.set('zone1temp',zone1temp);
flow.set('zone1input',zone1input);
flow.set('zone1flow',zone1flow*zone1heating);



var color=(zone1heating===1)?"red":"gray";
var data=zone1SP+"/"+zone1temp.toFixed(2);
msg.payload=[{
"command": "update_text",
"selector": "#zone1",
"textContent": data
},

{
    "command": "update_style",
     "selector": "#zone1_on",
     "attributeName": "fill",
     //"attributeName": "stroke",
     "attributeValue": color    
}]



return msg;

In that function you could name the variable zone1temp anything you like, zonetemp for example, there is no need to change it for the different functions. You do need to set the number in the lines like

flow.set('zone1temp',zonetemp);

but there all you need to do is to build the string zone1temp from your variable, so you could use

flow.set(`zone${nombre}temp`,zonetemp);

Similarly where you use selector later on all you need to do is to build the string.

However, I still maintain you would be much better using an array for the data in flow context rather than a large number of individually named variables. Any time when writing code that you find yourself repeating chunks of code then you are almost certainly doing it wrong. All those zoneinput function nodes can probably be replaced by one fed from all the Zone nodes.

My solution for function node:

//number of the zone
var numbre=1;
//timer
var now=(new Date().getTime());
var earlier=context.get('erlier')||now;
timeperiode=(now-earlier)/1000;
context.set('erlier',now);

//settings for room
var factor=1;
var areal=11.6;
var constant=0.05;
var wall=(3.5*2.4)+(3.23*2.4);
var windows=1.2*1.0;
var u_value=0.4;
var waterflow=70; //liter/h
var CHTtemp=flow.get("CHTtemp")||10;
var tude=flow.get("tude");
var SP=msg.payload.targetValue;
var temp=msg.payload.currentTemp;
if (typeof temp=="undefined"){
    temp=tude
    }
var heating = (msg.payload.currentHeaterStatus=="on")?1:0;
var ringcolor=(msg.payload.currentHeaterStatus=="on")?"red":"blue";
if (SP<tude){
  temp=tude;  
}

var energy=(areal*constant*temp);
if (typeof energy=="undefined"){
    energy=(areal*constant*tude)
    }
var energyMin=areal*tude*constant;

if (energy<energyMin){
    energy=energyMin
}

var heatloss=(((wall*u_value*(temp-tude)*timeperiode)/3600000)*factor);
//var heatloss=((wall*u_value*(zone1temp-tude)*timeperiode)*factor)/36000000;
var input=((waterflow/3600)*timeperiode*(CHTtemp-temp+5)*4.19*heating/3600);


energy=(energy-heatloss+input);

temp=Number((energy/(areal*constant)));

node.status({fill:ringcolor,shape:"ring",text: temp.toFixed(2)+"°C Q"+energy.toFixed(2)+" P:"+input.toFixed(5)+" -Q:"+heatloss.toFixed(5)});
flow.set('zone'+numbre+'temp',temp);
flow.set('zone'+numbre+'input',input);
flow.set('zone'+numbre+'flow',waterflow*heating);



var color=(heating===1)?"red":"gray";
var data=SP+"/"+temp.toFixed(2);





msg.payload=[{
"command": "update_text",
"selector": "#zone"+numbre,
"textContent": data
},

{
    "command": "update_style",
     "selector": "#zone"+numbre+"_on",
     "attributeName": "fill",
     //"attributeName": "stroke",
     "attributeValue": color    
}]



return msg;

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