# Context Variables Problem

**URL:** <https://discourse.nodered.org/t/context-variables-problem/37878>\
**Category:** General\
**Created:** [22 December 2020 14:16 UTC](https://discourse.nodered.org/t/context-variables-problem/37878 "2020-12-22T14:16:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![LucaCarminati](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@LucaCarminati](https://discourse.nodered.org/u/LucaCarminati)\
**Post date:** [22 December 2020 14:16 UTC](https://discourse.nodered.org/t/context-variables-problem/37878/1 "2020-12-22T14:16:04Z")

</div>

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.

```auto
[{"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}]

```

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e164aadc29ddba78b01e8c16a40cc9ef8e6e57cf.png)

 ![ui](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/18794c97e964f8cfa1019fdeb30d81f76121a9ec.png)

---

<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:** [22 December 2020 15:13 UTC](https://discourse.nodered.org/t/context-variables-problem/37878/2 "2020-12-22T15:13:01Z")

</div>

> [@LucaCarminati](#):
>
> msg.payload that should be sent continuosly

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

---

<div class="post-metadata">

**Author:** ![LucaCarminati](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@LucaCarminati](https://discourse.nodered.org/u/LucaCarminati)\
**Post date:** [22 December 2020 18:50 UTC](https://discourse.nodered.org/t/context-variables-problem/37878/3 "2020-12-22T18:50:54Z")

</div>

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!

---

<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:** [22 December 2020 18:52 UTC](https://discourse.nodered.org/t/context-variables-problem/37878/4 "2020-12-22T18:52:47Z")

</div>

> [@LucaCarminati](#):
>
> Could you tell me if in your opinion the code written in the function nodes is correct

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.

---

<div class="post-metadata">

**Author:** ![LucaCarminati](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@LucaCarminati](https://discourse.nodered.org/u/LucaCarminati)\
**Post date:** [22 December 2020 19:20 UTC](https://discourse.nodered.org/t/context-variables-problem/37878/6 "2020-12-22T19:20:34Z")

</div>

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.

---

<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:** [23 December 2020 10:17 UTC](https://discourse.nodered.org/t/context-variables-problem/37878/7 "2020-12-23T10:17:02Z")

</div>

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

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

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

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.

---

<div class="post-metadata">

**Author:** ![LucaCarminati](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@LucaCarminati](https://discourse.nodered.org/u/LucaCarminati)\
**Post date:** [27 December 2020 11:33 UTC](https://discourse.nodered.org/t/context-variables-problem/37878/8 "2020-12-27T11:33:01Z")

</div>

Hi Steve !,

sorry for the delay in the reply, and... Merry Christmas 😁.  
With this flow [Msg Resend](https://flows.nodered.org/node/node-red-contrib-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!!

---

<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:** [27 December 2020 14:03 UTC](https://discourse.nodered.org/t/context-variables-problem/37878/9 "2020-12-27T14:03:58Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2ddd49477201d882858d3d87c15d9fcc57ebba92.png)

---

<div class="post-metadata">

**Author:** ![LucaCarminati](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@LucaCarminati](https://discourse.nodered.org/u/LucaCarminati)\
**Post date:** [27 December 2020 17:11 UTC](https://discourse.nodered.org/t/context-variables-problem/37878/10 "2020-12-27T17:11:58Z")

</div>

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

---

<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:** [25 February 2021 17:12 UTC](https://discourse.nodered.org/t/context-variables-problem/37878/11 "2021-02-25T17:12:05Z")

</div>

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