Data Conversion - Java Script Expression - EtherNetIP node

I am reading PLC tags using the Ethernet/IP node. The output is something like:

{Recipe.Ingredient[0]: 1234.5, Recipe.Ingredient[1]: 678.9}

There will be hundreds of tags.

Right now, I can read them and get a nice log.csv

I cannot, however use the tags (values).

I think I'd have the same problem if I read from log.csv as well?

I don't have a problem when I read an atomic data type. I have an entire data structure map. I just can't reference these array members as "msg.payload.Recipe.Ingredient[0]". I can reference the tag "Ingredient_12_Weight" by "var Ingredient12Weight=msg.payload.Ingredient_12_Weight".

If what I'm trying to do is possible, I think it's pretty simple for a good js programmer. It's hard to believe that it's not possible.

Welcome to the forum.

Feed the message into a debug node and expand it enough that we can see the format of the data and post a screenshot here.

Have you read the node red docs page Working with Messages? It shows you how to get the path to properties from the debug pane.

I've posted screenshots of the flow that works with Atomic tags and the flow that doesn't when I try to use Array/UDT Tags.

So far, my best solution is to use the CSV node to strip header names and then another CSV node to add new headers without "." or "[", or "]".

EIP_SIM_DATA

[{"id":"69adde37.42636","type":"tab","label":"EIP_TagError","disabled":false,"info":""},{"id":"57f9dae4.fbcfc4","type":"debug","z":"69adde37.42636","name":"EIP_Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":260,"wires":[]},{"id":"b6fcfc11.0173d","type":"function","z":"69adde37.42636","name":"EIP DATA SIM","func":"\n    \nmsg.payload = {\n\"TestTag1\":1.11,\n\"TestTag2\":2.22,\n\"ActiveRecipe.Ingredient[0]\":3.33,\n\"ActiveRecipe.Ingredient[1]\":4.44,\n\"ActiveRecipe.Ingredient[2]\":5.55\n    }\n    \n    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":260,"wires":[["57f9dae4.fbcfc4","e680d9b2.5de2a8"]]},{"id":"6e7b7fee.901cd","type":"inject","z":"69adde37.42636","name":"PokeTest","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":260,"wires":[["b6fcfc11.0173d"]]},{"id":"e680d9b2.5de2a8","type":"function","z":"69adde37.42636","name":"Structure","func":"\nlet Test1=msg.payload.TestTag1;\nlet Test2=msg.payload.TestTag2;\n\n//let Test3=msg.payload.ActiveRecipe.Ingredient[0];\n//let Test4=msg.payload.ActiveRecipe.Ingredient[1];\n//let Test5=msg.payload.ActiveRecipe.Ingredient[2];\n\n\nmsg.payload={\n    \"WarpSpeedEfficiencyFactor\":Test1,\n    \"HyperDriveSuperConductorTemperature (K)\":Test2,\n//    \"SimpleTest1\":Test3,\n//    \"SimpleTest2\":Test4,\n//    \"SimpleTest3\":Test5\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":320,"wires":[["3a1826a2.a6a26a"]]},{"id":"3a1826a2.a6a26a","type":"debug","z":"69adde37.42636","name":"Structure_Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":320,"wires":[]}]

If you have property names (which I think is what you mean by tags) with special characters in then you have to use the square bracket notation rather than the dot shorthand. msg.payload is a shorthand way of writing msg["payload"]. So your function needs to be something like

let Test1=msg.payload.TestTag1;
let Test2=msg.payload.TestTag2;

let Test3=msg.payload["ActiveRecipe.Ingredient[0]"];
let Test4=msg.payload["ActiveRecipe.Ingredient[1]"];
let Test5=msg.payload["ActiveRecipe.Ingredient[2]"];


msg.payload={
    "WarpSpeedEfficiencyFactor":Test1,
    "HyperDriveSuperConductorTemperature (K)":Test2,
    "SimpleTest1":Test3,
    "SimpleTest2":Test4,
    "SimpleTest3":Test5
}

return msg;
1 Like

Are you sure you don't what ActiveRecipe to be an object containing an Ingredient array?

Ingredient is an array that is a member of ActiveRecipe in the PLC which is the source of my data.


let Test1=msg.payload.TestTag1;
let Test2=msg.payload.TestTag2;

let Test3=msg.payload["ActiveRecipe.Ingredient[0]"];
let Test4=msg.payload["ActiveRecipe.Ingredient[1]"];
let Test5=msg.payload["ActiveRecipe.Ingredient[2]"];

msg.payload={
"WarpSpeedEfficiencyFactor":Test1,
"HyperDriveSuperConductorTemperature (K)":Test2,
"SimpleTest1":Test3,
"SimpleTest2":Test4,
"SimpleTest3":Test5
}

return msg;


I had to add quotes as well but now it works. Thank you so much.

It may be an array in the plc but it is not an array in the code you posted.

This code that you posted

msg.payload = {
"TestTag1":1.11,
"TestTag2":2.22,
"ActiveRecipe.Ingredient[0]":3.33,
"ActiveRecipe.Ingredient[1]":4.44,
"ActiveRecipe.Ingredient[2]":5.55
    }

creates a property in msg.payload called ActiveRecipe.Ingredient[0]. If you wanted to create an array you would use

msg.payload = {
"TestTag1":1.11,
"TestTag2":2.22,
"ActiveRecipe": {"Ingredient": [3.33, 4.44, 5.55]}
}

Then your original code would work

let Test3=msg.payload.ActiveRecipe.Ingredient[0];
let Test4=msg.payload.ActiveRecipe.Ingredient[1];
let Test5=msg.payload.ActiveRecipe.Ingredient[2];

What quotes did you have to add?

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