How To Read a WSTRING From Bosh Rexroth CtrlX PLC Tag to Node Red

Hi, I'm working on some code and i want to add in a WSTRING from my PLC to a playload that would normally send NULL in the specific part of the JSON.

I've tried with a Replace and i just get nothing, I've tried by adding the PLC Tag directly to the JSON replacing the part where it would be NULL (or " ") and it just goes as "Undefined" when written as seen in the images below. any thoughts on how i could add this tag?
NOTE
This tag will change with each week therefore i cannot have it as a Constant Variable

image

image

Update: I can read the PLC tag "DateTest" in the flow when used as image below, the Function node after was for some testing but it is not going further, is there a way i can get the specified String from the tag to a specific Variable in NR?

Difficult to say from an inert screenshot.

  • Show us what comes out of the blue READ node (use the copy value button on the debug message)
  • Show us what is inside Function 8
  • Describe what you actually want to come out of function 8
  • What comes out from the Debug: D2441
  • What's Inside the Function:

  • What i want to Happen:
    I want the Value from the READ node to replace a msg.payload field in the Message To Cajo Function as it comes straight from DataBase as NULL as of now but our Customer wants that to be replaced with Date information from PLC.

I'm the worst at explaining but to simplify:
I want the DATE payload (as seen in the Node Text Below)to be changed to the payload sent from the PLC Tag

Message To Cajo Node looks like this Now:

var ART_BRAND = { payload: msg.payload[0].art_brand};
var ART_NR = { payload: msg.payload[0].art_nr};
var CUSTOMER_NR = { payload: msg.payload[0].customer_nr};
var LASER_MARKING_FILE = { payload: msg.payload[0].laser_marking_file };
var PRINT = { payload: msg.payload[0].print};
var DESCRIPTION = { payload: msg.payload[0].description};
var SAFETY_SYMBOL_TEXT = { payload: msg.payload[0].safety_symbol_text_field1};
var SAFETY_SYMBOL = { payload: msg.payload[0].safety_symbol_text_field2};
var FLEX_SYMBOL = { payload: msg.payload[0].flex_symbol_field3};
var SERIAL_NUMBER = { payload: msg.payload[0].flex_symbol_field4};
var PRODUCT = { payload: msg.payload[0].product_field5};
var TEXT_FIELD6 = { payload: msg.payload[0].text_field6};
var TEXT_FIELD7 = { payload: msg.payload[0].text_field7};
var TEXT_FIELD8 = { payload: msg.payload[0].text_field8};
var TEXT_FIELD9 = { payload: msg.payload[0].text_field9};
var TEXT_FIELD10 = { payload: msg.payload[0].text_field10};
var TEXT_FIELD11 = { payload: msg.payload[0].text_field11};
var CHARGE = { payload: msg.payload[0].charge_field12};
var DATE = { payload:msg.payload[0].date_field13};  
var SWEDEN = { payload: msg.payload[0].sweden_field14};
var BRAND = { payload: msg.payload[0].brand_field15};
var STROMS = { payload: msg.payload[0].stroms_field16};
var TRANAS = { payload: msg.payload[0].tranas_field17};
var PED_SYMBOL = { payload: msg.payload[0].ped_symbol_field18};
var CE_SYMBOL = { payload: msg.payload[0].ce_symbol_field19};
var FIFTEENKG_SYMBOL = { payload: msg.payload[0]["15kg_symbol_field20"]};
//var TEXT_FIELD9Mod = { payload: msg.payload[0].text_field9Mod};

var SerialNumberOutput = global.get("SerialNumberOuput");
//var DATE = global.get("DateTest") ;

if (SAFETY_SYMBOL_TEXT.payload === null) {
    SAFETY_SYMBOL_TEXT.payload = '';
}

if (SAFETY_SYMBOL.payload === null) {
    SAFETY_SYMBOL.payload = '';
}

if (FLEX_SYMBOL.payload === null) {
    FLEX_SYMBOL.payload = '';
}

if (SERIAL_NUMBER.payload === null) {
    SERIAL_NUMBER.payload = '';
}

if (PRODUCT.payload === null) {
    PRODUCT.payload = '';
}

if (TEXT_FIELD6.payload === null) {
    TEXT_FIELD6.payload = '';
}

if (TEXT_FIELD7.payload === null) {
    TEXT_FIELD7.payload = '';
}

if (TEXT_FIELD8.payload === null) {
    TEXT_FIELD8.payload = '';
}

if (TEXT_FIELD9.payload === null) {
    TEXT_FIELD9.payload = '';
}

if (TEXT_FIELD10.payload === null) {
    TEXT_FIELD10.payload = '';
}

if (TEXT_FIELD11.payload === null) {
    TEXT_FIELD11.payload = '';
}

if (CHARGE.payload === null) {
    CHARGE.payload = '';
}

if (DATE.payload === null) {
    DATE.payload = 'Test';
}

if (SWEDEN.payload === null) {
    SWEDEN.payload = '';
}

if (STROMS.payload === null) {
    STROMS.payload = '';
}

if (TRANAS.payload === null) {
    TRANAS.payload = '';
}

if (PED_SYMBOL.payload === null) {
    PED_SYMBOL.payload = '';
}

if (CE_SYMBOL.payload === null) {
    CE_SYMBOL.payload = '';
}

if (FIFTEENKG_SYMBOL.payload === null) {
    FIFTEENKG_SYMBOL.payload = '';
}




var templateId = '{"SetMarkingContent":{"TemplateId":"';
var templateId1 = '","Variables":[';
//var field1 = '{"Name":"Field1","Value":""},';
//var text2 = "World";
var fullmsg;

fullmsg = templateId.concat(LASER_MARKING_FILE.payload, templateId1, '{"Name":"Field1","Value":"', SAFETY_SYMBOL_TEXT.payload, '"},', '{"Name":"Field2","Value":"', SAFETY_SYMBOL.payload, '"},', '{"Name":"Field3","Value":"', FLEX_SYMBOL.payload, '"},', '{"Name":"Field41","Value":"', SerialNumberOutput, '"},', '{"Name":"Field42","Value":"', SerialNumberOutput, '"},', '{"Name":"Field43","Value":"', SerialNumberOutput,'"},','{"Name":"Field5","Value":"',PRODUCT.payload,'"},','{"Name":"Field6","Value":"',TEXT_FIELD6.payload,'"},','{"Name":"Field7","Value":"', TEXT_FIELD7.payload,'"},','{"Name":"Field8","Value":"', TEXT_FIELD8.payload,'"},','{"Name":"Field9","Value":"', TEXT_FIELD9.payload,'"},','{"Name":"Field10","Value":"', TEXT_FIELD10.payload,'"},','{"Name":"Field11","Value":"', TEXT_FIELD11.payload,'"},','{"Name":"Field12","Value":"', CHARGE.payload,'"},','{"Name":"Field13","Value":"',DATE.payload,'"},','{"Name":"Field14","Value":"', SWEDEN.payload,'"},','{"Name":"Field15","Value":"', BRAND.payload,'"},','{"Name":"Field16","Value":"', STROMS.payload,'"},','{"Name":"Field17","Value":"',TRANAS.payload,'"},','{"Name":"Field18","Value":"', PED_SYMBOL.payload,'"},','{"Name":"Field19","Value":"', CE_SYMBOL.payload,'"},','{"Name":"Field20","Value":"', FIFTEENKG_SYMBOL.payload,'"}]}}<EOT>');

msg.payload = fullmsg;

Please use the copy value button

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/value for 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




when posting code please post the code in a code block

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json




All your function does is set a variable named DATE.
Typically, you would set msg.payload not a new variable!

Alright, so if i copy the Path from The Debug then it becomes just "Payload", and the value that the PLC output presents when Read Will change as it is set as DYYWW (D= Just D, Y= Year, W = Week ) So copying the value will just bring me D2441. If i where to copy the path from the Debug how would i insert it to the "Message To Cajo" Function?

// Original
var DATE = { payload:msg.payload[0].date_field13}; 
Test 
var DATE = { payload:msg.payload[0].payload};


It is really difficult to help if you provide only snippets of info and they dont answer what I need to help you..

Please try to clearly answer the original questions

  • Show us what comes out of the blue READ node
    • use the copy value button on the debug message THEN paste that into a code block in your reply
  • Show us what is inside Function 8
    • Copy the function as text and paste it into a code block in your reply
  • Describe what you actually want to come out of function 8
    • example: "I want msg.payload to have 2024-10-11"

Read Node Directly Copied From Debug 40:

D2441

How it looks for me when i Trigger the blue READ:

That is all i get from Debug 40. The PLC tag is just a WSTRING with that value.

The Function has nothing in it, just some Code i have been trying i do not even know what to put in there to be honest.

 msg.payload[0]  ;
return msg;

I want to take the value from the Blue READ node and replace a part of another node, Specifically in the DATE variable where it would have been NULL otherwise

// PLC VALUE BELOW INSTEAD OF DATE_FIELD13 VALUE BELOW
var DATE = { payload: msg.payload[0].date_field13 };  

The code above can be found in the "Message To Cajo" Function where i have pasted the long snippet of code before, i tried making a small video of what happens when i trigger the flow but the Forums did not allow me to put it up here to show sadly

To try and specify the whole function (what i want it to be)

PLC triggers the READ nodes, One goes to a Database where it takes information of a product,
Second read is where i am at now. Out from Database goes to "Message To Cajo" where the Variable DATE is currently NULL, That's where i want the PLC:s Second READ function where it reads a WSTRING that changes with time, as of now it is D2441. The Read Functions Value shall replace the NULL value every time it triggers, i cannot have it as a Constant value as the PLC changes the value it outputs depending on Year and Week numbers

So, is it that you want data from 2 READs to be combined into 1 message so that you can access values of both READ nodes in the same function?

If yes, then you need to join them first

See this article in the cookbook for an example of how to join messages into one object.

No sir, the first READ that goes into the Database is fine, that part we do not mix with, i was trying to paint up the picture of how the full sequence goes.

My issue is that one field that come out of the Database,

Specifically date_field13 the is bound to the DATE variable is NULL, which is correct.

But the customer wants that field to have a variable which is where the second READ comes in. The math and stuff is already prepared in that tag that is being read. What i want to do is to take that tag and the ever changing variable that it sends out to replace the DATE variable so DATE will be for example D2441 This week and next week it will change automatically as it changes in the PLC to D2442 and so forth

So for example DATe below will get a new variable from the second READ that is bound to tag DateTest in the PLC

I've tried to Repalce it or change it


var TEXT_FIELD11 = { payload: msg.payload[0].text_field11};
var CHARGE = { payload: msg.payload[0].charge_field12};
//Original
var DATE = { payload: msg.payload[0].payload };
//My First Test
var DATE = { payload: msg.payload[0]["DateTest"] }; //Results = Undefined
// Second Test in the Replace 2 Node:
msg.payload[0].date_field13 = msg.payload[0]["DateTest"] // Results= Error
var SWEDEN = { payload: msg.payload[0].sweden_field14};
var BRAND = { payload: msg.payload[0].brand_field15};
var STROMS = { payload: msg.payload[0].stroms_field16};
var TRANAS = { payload: msg.payload[0].tranas_field17};
var PED_SYMBOL = { payload: msg.payload[0].ped_symbol_field18};
var CE_SYMBOL = { payload: msg.payload[0].ce_symbol_field19};
var FIFTEENKG_SYMBOL = { payload: msg.payload[0]["15kg_symbol_field20"]};

Then the join node still applies - you still need to get the value from the PLC merged with the value from the database.

Perhaps a cleaner way would be:

  1. Use a change node to store the value from the PLC into context (e.g. Set flow.DATE to value of msg.payload
  2. After the database call, use a change node to set the value of msg.whayever.path.you.need to the value of flow.DATE

So something like this?


Huge pictures with lots of whitespace, unnamed change nodes, zero annotations and no explanation are simply not helpful I'm afraid!

Also, I can see you are not grasping some of the basic concepts of Node-RED. For example you have entered flow.DATA as a string. You are supposed to select flow in the field drop down and enter DATE as the name of the context variable. You also didnt do what I said

Here is is once more with extra pointers:

  • Use a change node to store the value from the PLC into context (e.g. Set flow.DATE to value of msg.payload
    • This is to STORE the PLC value in a context variable so that you can later READ it from context
  • After the database call, use a change node to set the value of msg.whayever.path.you.need to the value of flow.DATE
    • This is to READ the PLC value FROM context and SET it into your database data

I feel you really would benefit from spending time with the official playlist:

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

1 Like

You are very correct, i'm not used to Node-RED, this project is the first time i'm working with it and i'm tryng to learn while working with it, i am a PLC programmer at core. You are also correct i set up the Change Nodes wrong. I'm doing my best to learn but going from PLC programming to just thrown in to stuff like Node-RED for one customer is a bit tricky, i've got it to work and i wanna say a huge thanks to you Steve for your aid and i'm sorry if there was any miss-communication from my end! I'll paste a snippet of code from Debug 40 (With some changes to protect my customers Stuff). :slight_smile:



{"SetMarkingContent":{"TemplateId":"Ø75_BLACK_Blank","Variables":[{"Name":"Field1","Value":"Safety Inside"},{"Name":"Field2","Value":"!{#"},{"Name":"Field3","Value":"$"},{"Name":"Field41","Value":"U24410048"},{"Name":"Field42","Value":"U24410048"},{"Name":"Field43","Value":"U24410048"},{"Name":"Field5","Value":"2487.12.02400.050"},{"Name":"Field6","Value":"Fcharge Max 2400 daN at 20°C"},{"Name":"Field7","Value":"Product"},{"Name":"Field8","Value":"TS-20/80°C, PS 373 bar"},{"Name":"Field9","Value":"Gas"},{"Name":"Field10","Value":"Product"},{"Name":"Field11","Value":"Seal Kit Changes     _______|_______|_______"},{"Name":"Field12","Value":""},{"Name":"Field13","Value":"D2441"},{"Name":"Field14","Value":"Made in Narnia"},{"Name":"Field15","Value":"§"},{"Name":"Field16","Value":"DaVizzey Inc."},{"Name":"Field17","Value":"Cave Man Bonk"},{"Name":"Field18","Value":"+"},{"Name":"Field19","V...

It is a mindset change indeed. i did PLC programming for ~25y

the biggest difference is this is NOT power flow and not scan based.
The Node-RED runtime is event based - so unlike the power flow in the ladder of a PLC where it is effectively polled with a scan rate, things happen in Node-RED based on events. I suppose you could think of the msg object as the power flow in ladder (but dont get too hung up on it.)

You're welcome. glad you got moving.
And I understand the terminology differences can be a shock to the system so I was trying to lead you to a place where you could discover the answers for yourself (better for learning ;))

1 Like

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