Read msg.payload[variable], identifier as variable string

Hi,

is his possible in a function to read the msg.payload
with a variable identifier ?
eg.

var tagname = node.name;

// Get name of node eg. LSH3-1.0 (level switch high)
// in the msg.payload there is a value
// LSH_3_1_0 : 40
// So var value could be
// value = msg.payload.LSH_3_1_0

var tag
tag = tagname.replace(".", "");
tag = tag.replace("-", "
");
var value = msg.payload[tag];
var ON = (value & (1 << 27)) === 0 ? 0 : 1;
msg.payload = {};
msg.payload = ON;
msg.value = value;
msg.tagname = tagname;
return msg;

Whats is the correct syntax for this ?

Gr.

As you say, the name of the function is LSH3-1.0 then calling .replace(".", ""); and .replace("-", "") will create LSH310 which is NOT equal to LSH_3_1_0 (so wouldnt work)

However, if you name the function LSH_3_1_0 then this would work...

var tagname = node.name;
var value = msg.payload[tagname];
var ON = (value & (1 << 27)) === 0 ? 0 : 1; 
msg.payload = ON;
msg.value = value;
msg.tagname = tagname;
return msg;

Ok thx for pointing that out, got it working now

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