How to pass values for "ui_switch" from Flow1 to Flow2?

Could you advise me?
I don't understand, i am beginner in node-red.
I created an example. with two flows.
In the first flow I created a global variable "Test".
And in the second flow an "ui_switch" which should to change the status depending on the "Test" variable from first flow. And I don't understand how to implement this!

Code the Flow1

[{"id":"88a65a8f.890828","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"693af34c.97d5ac","type":"inject","z":"88a65a8f.890828","name":"On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"On","payloadType":"str","x":280,"y":120,"wires":[["2b8a9837.f976d8"]]},{"id":"5922f3d9.eac7ac","type":"inject","z":"88a65a8f.890828","name":"Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Off","payloadType":"str","x":280,"y":180,"wires":[["2b8a9837.f976d8"]]},{"id":"2b8a9837.f976d8","type":"function","z":"88a65a8f.890828","name":"","func":"\nvar Test = global.get('Test') || \"\"; \n\nif (msg.payload === \"On\") {\n    global.set('Test',\"ON\");\n    return [ msg ];\n} \nelse { \n    global.set('Test',\"OFF\");\n    return [ msg ]; \n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":160,"wires":[["405e2492.3037fc"]]},{"id":"405e2492.3037fc","type":"debug","z":"88a65a8f.890828","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":160,"wires":[]},{"id":"de74ac53.99303","type":"debug","z":"88a65a8f.890828","name":"TEST","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":260,"wires":[]},{"id":"a78bd8bc.00d7a8","type":"function","z":"88a65a8f.890828","name":"Check Test","func":"var Test = global.get('Test') || \"\"; \n\nif (msg.payload === 1 && Test != \"1\") {\n    msg.payload=Test;\n    return [ msg ];\n} \nelse { return [ null ]; \n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":470,"y":260,"wires":[["de74ac53.99303"]]},{"id":"e5345f39.b0f9a","type":"inject","z":"88a65a8f.890828","name":"","props":[{"p":"payload"},{"p":"topi","v":"","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":270,"y":260,"wires":[["a78bd8bc.00d7a8"]]}]

Code the Flow2

[{"id":"32f04248.f9f5ae","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"6cc2f6b3.63dbc8","type":"function","z":"32f04248.f9f5ae","name":"","func":"var Test = global.get('Test'); \n\nif (msg.payload === \"On\") {\n    global.set('Test',\"ON\");\n    return [ msg ];\n} \nelse { \n    global.set('Test',\"OFF\");\n    return [ msg ]; \n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":120,"wires":[["974f73a2.7a89b","7c508c9a.a1b584"]]},{"id":"974f73a2.7a89b","type":"change","z":"32f04248.f9f5ae","name":"Enable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":60,"wires":[["a27fde93.1e289"]]},{"id":"7c508c9a.a1b584","type":"change","z":"32f04248.f9f5ae","name":"Disable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":180,"wires":[["a27fde93.1e289"]]},{"id":"a27fde93.1e289","type":"ui_switch","z":"32f04248.f9f5ae","name":"","label":"Test","tooltip":"","group":"45b1bd44.270cf4","order":3,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"{\"state\":\"ON\"}","onvalueType":"json","onicon":"fa-lightbulb-o fa-3x","oncolor":"red","offvalue":"{\"state\":\"OFF\"}","offvalueType":"json","officon":"fa-lightbulb-o fa-3x","offcolor":"gray","x":690,"y":120,"wires":[[]]},{"id":"45b1bd44.270cf4","type":"ui_group","z":"","name":"A1","tab":"181108e9.fe4277","order":1,"disp":true,"width":"6","collapse":false},{"id":"181108e9.fe4277","type":"ui_tab","z":"","name":"TEST","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

I'm not in a position to import the flows.

But, a couple of points.

1 - if you have made a global variable called: "Test" then the second flow should look at that with a change node.

On the second flow get a change node and get it to set the msg.payload to global.Test then feed that into the ui_switch node and have it set to allow the payload to change its state depending on the payload.

2 - You will need to set up a repeated signal getting/looking at the state of the global.Test condition.
That can be achieved with an inject node set to inject at interval.
Don't set it too fast, or you will get caught up with that loop rather than doing things.
Say every second. (Or two - you know how fast it needs to be updated.)

I won't comment on why you are doing this, as it seems to be making work for no reason.

But I hope what I said helps you.

1 Like

You can use Link nodes to send messages from one tab to another.

It is best to try and avoid global variables except where they are definitely needed. Where practical the node-red way is to pass messages. I don't know enough about your requirement to say whether using a global variable is justified in your situation.

[Edit] Also, if you have not already done so, then spend a couple of hours watching right through the node-red Essentials videos linked from the docs. That will be time well spent.

2 Likes

I was thinking the same thing. Only time I use the global is if the 2nd flow doesn't want to use the switch state right away (will use it as a condition later on) or when multiple other flows are going to use the same piece of data for something. For example, if 3 flows are going to use the switch state as a condition, rather than set 3 different flow variables I set one global. I figure the trade-off for using a global is worth it then. Would you agree, @Colin?

Personally I never use globals or flow variables for such tasks. Others think I take an extreme view. One of the problems is that it is not obvious where globals are set or used, another is that you do not get a message when a variable changes.

1 Like

Thanks for the info! With link nodes I solved my problem without using a global variables.

1 Like

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