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

**URL:** <https://discourse.nodered.org/t/reading-multiple-variables-from-plc-how-to-read-single-variables/70036>\
**Category:** General\
**Tags:** function-node\
**Created:** [3 November 2022 15:19 UTC](https://discourse.nodered.org/t/reading-multiple-variables-from-plc-how-to-read-single-variables/70036 "2022-11-03T15:19:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bart92](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@bart92](https://discourse.nodered.org/u/bart92)\
**Post date:** [3 November 2022 15:19 UTC](https://discourse.nodered.org/t/reading-multiple-variables-from-plc-how-to-read-single-variables/70036/1 "2022-11-03T15:19:58Z")

</div>

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

```auto
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.

 ![nodered2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/40c55f737919692323170f3046feedf06aad5464.png)

---

<div class="post-metadata">

**Author:** ![madhouse](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@madhouse](https://discourse.nodered.org/u/madhouse)\
**Post date:** [3 November 2022 17:48 UTC](https://discourse.nodered.org/t/reading-multiple-variables-from-plc-how-to-read-single-variables/70036/2 "2022-11-03T17:48:48Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![bart92](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@bart92](https://discourse.nodered.org/u/bart92)\
**Post date:** [4 November 2022 10:38 UTC](https://discourse.nodered.org/t/reading-multiple-variables-from-plc-how-to-read-single-variables/70036/3 "2022-11-04T10:38:37Z")

</div>

> [@madhouse](#):
>
> 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.

```auto
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')"

```

---

<div class="post-metadata">

**Author:** ![madhouse](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@madhouse](https://discourse.nodered.org/u/madhouse)\
**Post date:** [4 November 2022 11:35 UTC](https://discourse.nodered.org/t/reading-multiple-variables-from-plc-how-to-read-single-variables/70036/4 "2022-11-04T11:35:16Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 November 2022 11:52 UTC](https://discourse.nodered.org/t/reading-multiple-variables-from-plc-how-to-read-single-variables/70036/5 "2022-11-04T11:52:19Z")

</div>

> [@bart92](#):
>
> My point is how to read the value of a single variable

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](https://nodered.org/docs/user-guide/messages)) 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 November 2022 11:53 UTC](https://discourse.nodered.org/t/reading-multiple-variables-from-plc-how-to-read-single-variables/70036/6 "2022-11-18T11:53:15Z")

</div>

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