Reading multiple variables from PLC, how to read single variables?

Hello, I'm new to node-red. I have no idea how to go about solving my issue. I have added a PLC in which I have defined several variables. Communication works but It returns msg.payload, which contains all variable names and their values ​​(marked in yellow on ss). How can I read single variables so that I can use them in function? I tried to use

msg.payload = global.get("D09C151_02TIR01.out_scl");
return msg;

but unfortunately it doesn't work. Ultimately, it needs to read about 8,000 variables. so that each variable can be used separately in function.

Not quite sure I can see what you're asking. So your PLC is returning 8000ish variables and you're trying to address them individually? Or you're trying to request 8000ish variables individually and put them together?

If you're getting them all together and want to address them individually, simply refer to the key to get the value. Because they appear to be coming in with msg.payload, this is how you would get the individual value in a function:

var indValue = msg.payload.D09C151_02TIR01.out_scl

This will set indValue to 524.9375.

I don't know how your PLC works to get 8000ish values to come out. But I'm assuming you are trying to get to the individual value instead of the other way around. Let me know if I didn't answer what you were looking for and I can try something else.

My point is how to read the value of a single variable. From plc I get msg.payload containing all the variables and I need to refer to a specific variable.

I tried to use your example but it returns an error.

11/4/2022, 10:34:54 AM[node: Function](http://127.0.0.1:1880/#)function : (error)

"TypeError: Cannot read properties of undefined (reading 'out_scl')"

Fair enough. When dot notation doesn't work, we can use bracket notation to address it directly as well. Bracket notation also makes it possible to use a variable with a property name assigned to it to address the object property. The difference here is you're going to need a string as your value instead of just the dot notation.

var indValue = msg.payload["D09C151_02TIR01.out_scl"];

That circumvents the issues with weird property names. If you use a variable, just make sure the variable uses a string and don't use quotes with it. But that should get you your value.

1 Like

Ah, you don't mean read a single variable from the PLC, you mean how to access it within the object.

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

1 Like

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