Context Variables Problem

Hi to all,

I have some problem with the node red context var.
I am trying to develope a function that send continuosly the msg.payload (that arrive from a form table) only when a context flow var became equals to 10.
The problem is that now I receive the form content (that is my msg.payload that should be sent continuosly) only when the context.var is equals to 10 and only when I press the "Send" button in the Form UI.

My goal is this :

  • Write (ex.) "ABCD" in the Form Textbox
  • Press Send in the Form UI
  • If an external context.var is equal to 10 send continuosly the form textbox read ("ABCD")
  • If the external context.vat is not equal to 10 don't send nothing.

I hope to have explained my problem as clearly as possible.

Thanks in advance to everybody!

I paste here my flow and some image of the function that I have written up to now.

[{"id":"5f3f2677.c5a0d8","type":"tab","label":"Flow 10","disabled":false,"info":""},{"id":"36d903a5.8fbc0c","type":"change","z":"5f3f2677.c5a0d8","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"test","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":260,"wires":[["85ed380.215dfc8"]]},{"id":"81135576.81e3f8","type":"inject","z":"5f3f2677.c5a0d8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":190,"y":220,"wires":[["36d903a5.8fbc0c"]]},{"id":"85ed380.215dfc8","type":"function","z":"5f3f2677.c5a0d8","name":"","func":"context.set('txt',msg.test);\ny = context.get('txt');\n//var y = context.get('txt');\nflow.set(\"testcode\",y);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"/*var x = msg.token;\nvar y = msg.test;\n\nflow.set(\"tokencode\",x);\nflow.set(\"testcode\",y);\nreturn msg;*/","x":600,"y":260,"wires":[[]]},{"id":"2ddf7711.e639a8","type":"inject","z":"5f3f2677.c5a0d8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"150","payloadType":"num","x":190,"y":280,"wires":[["36d903a5.8fbc0c"]]},{"id":"9d889dde.f0264","type":"ui_form","z":"5f3f2677.c5a0d8","name":"","label":"DataTable","group":"a9584ee2.24eac","order":1,"width":"5","height":"9","options":[{"label":"Baia Inizio","value":"StartBay","type":"text","required":false,"rows":null},{"label":"Baia 2","value":"Bay_2","type":"text","required":false,"rows":null},{"label":"Baia 3","value":"Bay_3","type":"text","required":false,"rows":null},{"label":"Baia 4","value":"Bay_4","type":"text","required":false,"rows":null},{"label":"Baia 5","value":"Bay_5","type":"text","required":false,"rows":null},{"label":"Baia 6","value":"Bay_6","type":"text","required":false,"rows":null},{"label":"Baia 7","value":"Bay_7","type":"text","required":false,"rows":null},{"label":"Baia 8","value":"Bay_8","type":"text","required":false,"rows":null},{"label":"Baia 9","value":"Bay_9","type":"text","required":false,"rows":null},{"label":"Baia Fine","value":"EndBay","type":"text","required":false,"rows":null}],"formValue":{"StartBay":"","Bay_2":"","Bay_3":"","Bay_4":"","Bay_5":"","Bay_6":"","Bay_7":"","Bay_8":"","Bay_9":"","EndBay":""},"payload":"","submit":"invio","cancel":"cancel","topic":"","x":180,"y":380,"wires":[["1f22c1bc.85b76e"]]},{"id":"d277370f.ea58c8","type":"debug","z":"5f3f2677.c5a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":380,"wires":[]},{"id":"1f22c1bc.85b76e","type":"function","z":"5f3f2677.c5a0d8","name":"","func":"context.set('start',msg.payload.StartBay);\n//var StartBay = context.get('start');\nStartBay = context.get('start');\ntestcode1 = flow.get(\"testcode\");\nif (testcode1 == 10){\n    //return {payload:StartBay};\n   node.send({payload:StartBay});\n}\nelse {\nreturn null;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":380,"wires":[["d277370f.ea58c8"]]},{"id":"a9584ee2.24eac","type":"ui_group","name":"Rulliere","tab":"a4cbd78a.6d5d68","order":1,"disp":true,"width":"20","collapse":false},{"id":"a4cbd78a.6d5d68","type":"ui_tab","name":"Cantiere","icon":"dashboard","disabled":false,"hidden":false}]

The ui_form inputs ONLY send a msg.payload AFTER you press INVIO button (not continuous)

2 options are...

  1. Build your own form (UI_TEMPLATE)
  2. Use the ui_textinput nodes
1 Like

Thank you Steve!

I will try your solution this evening /tomorrow.
Could you tell me if in your opinion the code written in the function nodes are correct?

Thanks a lot in any case!

To be honest, it is not incorrect but it is not "good code". I don't understand why to write to and read from context.

If you can clearly explain what you are trying to achieve, I will try to offer advice.

1 Like

Well the full project is a bit complicated.
What I have posted is only a small part of the idea.
I need to write in a PLC (by OPCUA) the data that i put in the form textbox.
The data, with a tested code that I have done, are also used for point in the DB that i need to write.
Ex. textbox : ABCD - >
Press Send - >
It goes in a function node that will have as
output something like this : msg.topic =
'ABCD'.200.TEST.DB.Var and a msg.payload
like this : 547464 - >
All the msg. is sent to a OPCUA node
configured in write mode.

After that i have send this I need to check with an OPCUA node configured in Read mode if a variable at the end of my PLC function block is changed (that is in this case my context var that need to be equals to 10).
If this is i will send, with the same procedure explained before, the 2° code that i have written in the textbox and so on.
The problem is that for do that I need to configure the node in this mode:

Form text - > function node 1 (where i will have my address for the PLC DB) - > OPCUA node in write mode. Then

OPCUA node in read mode - > function node 2 (where i will have my address for the PLC DB and where i will check if the var is = 10)
Form text - > the same function node 2 (where i will have my address for the PLC DB and where i will check if the var is = 10)
-> OPCUA node in write mode. Then

OPCUA node in read mode - > function node 3 (where i will have my address for the PLC DB and where i will check if the var is = 10)
Form text - > the same function node 3 (where i will have my address for the PLC DB and where i will check if the var is = 10)
-> OPCUA node in write mode. And so on until the end.

The problem is that in the function node from 2 to 10 i need to have at the entrance two different msg. Payload that obliges me to use the context.var, and in addition to this i need to store the var from the text form (with your solution a text box or custom template) so that i press the send button only one time.

I hope to have explained my project clearly.

Thanks again a lot.

Now i have here only the smartphone, but when i will be on my pc, if you want i can send you my flow.

Wow - that is really difficult to follow but i am going to make some guesses.

Let me see if I can offer some advice...

So at FUNCTION1 you dont need to set context, you can simply add the OPCUA tag into the msg object and pass it to the next nodes. e.g. msg.opcuaTag = "my.opc.tag"; then in next nodes, var tag = msg.opcuaTag

As for read --> write --> read. This is not the idea way (IMO).

I would do 2 things here...

  1. Read (on change or poll) the TAG for value 10 and store it in global or flow context.
  2. When the user enters text, get the context value and check if it is == 10.
  3. As a final check (because SOMETIMES OPC values are BAD or WRONG), in the PLC, ENSURE in LADDER that the value is 10 before MOVing the values to the real destination in the PLC.

I would like to further advise but it is very difficult to understand what the end goal is.

Perhaps you are trying to do a RECIPE write and VERIFY? If so, I can advise a better method that will ensure data consistency and be performed in less steps / less transmissions.

Good luck.

Hi Steve !,

sorry for the delay in the reply, and... Merry Christmas :grin:.
With this flow Msg Resend (even if I don't know if it could be the best solution) I solved all my problems.
I send you in private msg. the flow if you want to see it (I can't paste it here), and no, my goal was not to do a recipe and verify it.

Thanks again for all your support!!

You can do most (if not all) that the resend node does using the core Trigger node, which would save installing that node (and relying on it being maintained in the future.

image

1 Like

I didn't know that! Thanks a lot!!!