# Assigning values to object items

**URL:** <https://discourse.nodered.org/t/assigning-values-to-object-items/41961>\
**Category:** General\
**Created:** [3 March 2021 14:53 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961 "2021-03-03T14:53:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pautorras](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pautorras](https://discourse.nodered.org/u/pautorras)\
**Post date:** [3 March 2021 14:53 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961/1 "2021-03-03T14:53:29Z")

</div>

Hi folks,

I'm doing a node-red project to upload some s7 PLC variables (Siemens Simatic industrial device ET200SP CPU) to cloud.

There are a lot of variables to upload, and some of them have the same structure, so I decided to work with objects like this one: (BNBA3 is a pump motor)

```auto
var BNBA3={OnOff:0,OpTime:0,Volt:0,Amp:0}; // Pump struct
var LSA3={H:0,M:0,L:0,LAlarm:0}; // level status

```

I'm using node-red-contrib-s7. I'm working with two nodes of S7, because I have two Siemens PLC.  
The S7 node output will be an array like msg.payload[0..49] (containing 50 or more variables each).

My goal is to abbreviate as maximum as possible the assignment of Siemens var values in my objects, so I'm trying to make two for loops to solve it.

This is how I planned to do it:

```auto
var i;
for (i = 0; i < msg.payload.length; i++) {
    if (i>=0 && i<=3){
    	for (var property in BNBA3){
    		$property=msg.payload[i];
    	}
    }

}

```

I can't test now the code, because I don't have the PLCs right now, but the doubt is if sintaxis (specially in $property line) is correct or not.

Any suggestion will be appreciated.

Best Regards,  
Pau

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [3 March 2021 15:46 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961/2 "2021-03-03T15:46:00Z")

</div>

Sure you can test it, use an` inject` node to simulate the data coming in and attach a `debug` node to the output of the `function` node to see what is coming out.

You could even add a `node.warn()` in the `function` node to see what is happening like this:

```auto
var i;
for (i = 0; i < msg.payload.length; i++) {
    if (i>=0 && i<=3){
    	for (var property in BNBA3){
    		$property=msg.payload[i];
               node.warn(`${property}: ${object[property]}`);
    	}
    }

}

```

(at least I think that node warn will work 😁)

---

<div class="post-metadata">

**Author:** ![pautorras](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pautorras](https://discourse.nodered.org/u/pautorras)\
**Post date:** [3 March 2021 16:10 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961/3 "2021-03-03T16:10:17Z")

</div>

Thanks @zenofmud , I'll test it in that way.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 March 2021 16:29 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961/4 "2021-03-03T16:29:34Z")

</div>

A good node for arranging your PLC array into nice object / properties is the [buffer-parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser) node. It does all the heavy lifting by taking all the effort out of looping, data typeing etc

---

<div class="post-metadata">

**Author:** ![pautorras](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pautorras](https://discourse.nodered.org/u/pautorras)\
**Post date:** [8 March 2021 10:26 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961/5 "2021-03-08T10:26:15Z")

</div>

Hi again folks,

I'm testing, but I'm not able to assign msg.payload to object values. As you can see, I recieve a buffer [12, 33, 26, 77, 91, 21], but is not assigned to a object properties values.

The code is:

```auto
var BNBA3={OnOff:0,OpTime:0,Volt:0,Amp:0}; // struct Motor
var LSA3={H:0,M:0,L:0,LAlarm:0}; // struct water level

var i;
for (i = 0; i < msg.payload.length; i++) {
    if (i>=0 && i<=3){
    	for (var property in BNBA3){
    		property=msg.payload[i];
    	}
    }

}
global.set("BNBA3",BNBA3);

return msg;

```

Debug message capture of input buffer:  
 ![screenshot2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d37e3f26d8eb93b0d4c3b7fa19c5c8ea00e078db.jpeg)

What I get in global context object:  
 ![screenshot1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/03335c8ce15417947ea547f48ccda262f9a367f4.jpeg)

It's possible to do what I want to do? If it's possible, whats the way to assign values into the object (whats wrong with my code)?

Thanks a lot to all.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 March 2021 10:40 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961/6 "2021-03-08T10:40:10Z")

</div>

Hi @pautorras

Your code does not touch the `BNBA3` object, so its values will never change. To help fix it, it would be helpful to understand how you intend the buffer values `[12, 33, 26, 77 ,91, 21]` to be mapped into the `BNBA3` object.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 March 2021 11:08 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961/7 "2021-03-08T11:08:01Z")

</div>

@pautorras  
No offence intended, but that function code tells me you are a bit inexperienced with JavaScript.

So I will try one last attempt to sway you to buffer-parser.

NOTE: without knowing what bits & bytes make up those properties, I have just assigned each byte to a property (and had to add 2 bytes to the buffer).

but just to demonstrate you can avoid all this looping and manual conversion, here is a quick demo...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7c78fbe402e86b4cf2b61c3c270750e863c78d46.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/52b3ac14a236602bd0e907c94e79e3921849cb66.png)

demo flow...

```auto
[{"id":"327126db.93d1ca","type":"buffer-parser","z":"4fed65e5.e87b3c","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int8","name":"BNBA3=>OnOff","offset":0,"length":1,"offsetbit":16,"scale":"1","mask":""},{"type":"int8","name":"BNBA3=>OpTime","offset":1,"length":1,"offsetbit":16,"scale":"1","mask":""},{"type":"int8","name":"BNBA3=>Volt","offset":2,"length":1,"offsetbit":16,"scale":"1","mask":""},{"type":"int8","name":"BNBA3=>Amp","offset":3,"length":1,"offsetbit":16,"scale":"1","mask":""},{"type":"int8","name":"LSA3=>H","offset":4,"length":1,"offsetbit":16,"scale":"1","mask":""},{"type":"int8","name":"LSA3=>M","offset":5,"length":1,"offsetbit":16,"scale":"1","mask":""},{"type":"int8","name":"LSA3=>L","offset":6,"length":1,"offsetbit":16,"scale":"1","mask":""},{"type":"int8","name":"LSA3=>LAlarm","offset":7,"length":1,"offsetbit":16,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":710,"y":160,"wires":[["48b72cde.b5bbb4","91d955b3.6ef278"]]},{"id":"302044c6.a2fe3c","type":"inject","z":"4fed65e5.e87b3c","name":"PLC data (faked) [12, 33, 26, 77, 91, 21, 0, 0]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[12, 33, 26, 77, 91, 21, 0, 0]","payloadType":"bin","x":790,"y":100,"wires":[["327126db.93d1ca"]]},{"id":"48b72cde.b5bbb4","type":"change","z":"4fed65e5.e87b3c","name":"store BNBA3 in global BNBA3","rules":[{"t":"set","p":"BNBA3","pt":"global","to":"payload.BNBA3","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":220,"wires":[[]]},{"id":"91d955b3.6ef278","type":"debug","z":"4fed65e5.e87b3c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":160,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![pautorras](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pautorras](https://discourse.nodered.org/u/pautorras)\
**Post date:** [8 March 2021 11:10 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961/8 "2021-03-08T11:10:01Z")

</div>

Hi, I would like to assign the buffer values into some objects values.  
In this case, I would like to assign to object BNBA3 the firts 4 values of buffer in order 0..3.

Thanks

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 March 2021 11:18 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961/9 "2021-03-08T11:18:30Z")

</div>

> [@pautorras](#):
>
> I would like to assign the buffer values into some objects values.

Okay, so I would also suggest you look at the buffer-parser node as per @Steve-Mcl 's suggestion.

But to help you understand the JavaScript a bit more, here's a bit of an explanation.

The properties of a JavaScript Object have no order - they are just named properties.  
Properties of a Buffer or Array do have an order.

So you cannot say "assign the first element of the array to the first property of the object" because there is no such thing as the first property of an object.

I assume you've listed the properties of `BNBA3` in the order you expect them to appear in the array passed into the function. From that, you end up with the following code:

```auto
var BNBA3={
   OnOff: msg.payload[0],
   OpTime: msg.payload[1],
   Volt: msg.payload[2],
   Amp: msg.payload[3]
};
var LSA3={
   H:0,
   M:0,
   L:0,
   LAlarm:0
};

global.set("BNBA3",BNBA3);
return msg;

```

---

<div class="post-metadata">

**Author:** ![pautorras](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pautorras](https://discourse.nodered.org/u/pautorras)\
**Post date:** [8 March 2021 12:09 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961/10 "2021-03-08T12:09:07Z")

</div>

Thaks you all @knolleary & @Steve-Mcl for your help.  
I'm a PLC programmer, and I started doing Node-Red projects 1 year ago aprox, not at full time (only when I have time), so yes, I can say that I'm not an expert with JS.

With my little experience with Node-Red and JS I found amazing the things that you can do with.  
I've used Node-Red and JS to read/write PLC tags, to upload PLC tags to cloud, I've also worked with SQLite and MySQL, weather APIs, dashboard, embeed grafana in Node-Red dashboard,...

I will learn/test more about buffer-parser node because I think that is perfect for my needs. I have tested before last reply, but I've not understant at all the import samples. With @Steve-Mcl code I think I can solve my problem so thank you all again.

I love learn with you!

---

<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:** [7 May 2021 12:09 UTC](https://discourse.nodered.org/t/assigning-values-to-object-items/41961/11 "2021-05-07T12:09:28Z")

</div>

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