Combine 2 payloads with Auto mode function and data

Hi everyone,
I'm new to Nodered, but i have played around with it for 2 days. I think i know some basic things. But i'm struggling with the following problems:

I want to build a flow that can handle two separated outputs. One is set ON/OFF of the Auto/Manual mode. The other is the data to be processed.
The flows i post here is just my sample for testing:

[{"id":"319a8fa5.9db74","type":"inject","z":"d2dd1bf4.363198","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":157.5,"y":599,"wires":[["5168bcc0.ed15e4"]]},{"id":"2523ee3e.b34762","type":"debug","z":"d2dd1bf4.363198","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":967,"y":637,"wires":[]},{"id":"5168bcc0.ed15e4","type":"ui_switch","z":"d2dd1bf4.363198","name":"","label":"Manual/Auto","tooltip":"","group":"2b2c4836.f67468","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":379.5,"y":600,"wires":[["f3261285.dfc9e"]]},{"id":"ff10b8f0.4974a8","type":"function","z":"d2dd1bf4.363198","name":"Data","func":"msg.topic = \"Data\";\n//msg.message = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":421,"y":454,"wires":[["69d44bc3.eb2044"]]},{"id":"f3261285.dfc9e","type":"switch","z":"d2dd1bf4.363198","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":591.5,"y":596,"wires":[["bfdc1e4.ece91e"],["8d9d24a7.626c88"]]},{"id":"8d9d24a7.626c88","type":"function","z":"d2dd1bf4.363198","name":"Auto_OFF","func":"msg.payload = \"Manual\";\nreturn msg;","outputs":1,"noerr":0,"x":760,"y":629,"wires":[["2523ee3e.b34762"]]},{"id":"9d38685b.9f5a08","type":"inject","z":"d2dd1bf4.363198","name":"","topic":"","payload":"10","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":188,"y":441,"wires":[["ff10b8f0.4974a8"]]},{"id":"ae6b13d4.88df2","type":"inject","z":"d2dd1bf4.363198","name":"","topic":"","payload":"4","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":185,"y":482,"wires":[["ff10b8f0.4974a8"]]},{"id":"5c8beae7.342d64","type":"function","z":"d2dd1bf4.363198","name":"Control","func":"if(msg.payload.State == true){\n    if(msg.payload.Data <= 5){\n        msg.payload = 1;\n        return msg;\n    }else{\n        msg.payload = 0;\n        return msg;\n    }\n}\n","outputs":1,"noerr":0,"x":1094.5,"y":503,"wires":[["c9983fbf.06c01"]]},{"id":"e5b1531c.6fbcf","type":"debug","z":"d2dd1bf4.363198","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1101.5,"y":602,"wires":[]},{"id":"bfdc1e4.ece91e","type":"function","z":"d2dd1bf4.363198","name":"Auto_ON","func":"msg.topic = \"State\";\nreturn msg;","outputs":1,"noerr":0,"x":747.5,"y":546,"wires":[["2523ee3e.b34762","69d44bc3.eb2044"]]},{"id":"83dd3e04.46089","type":"inject","z":"d2dd1bf4.363198","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":156,"y":645,"wires":[["5168bcc0.ed15e4"]]},{"id":"69d44bc3.eb2044","type":"join","z":"d2dd1bf4.363198","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":898.5,"y":457,"wires":[["81468a2e.e31fd8","5c8beae7.342d64"]]},{"id":"81468a2e.e31fd8","type":"debug","z":"d2dd1bf4.363198","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1077,"y":458,"wires":[]},{"id":"c9983fbf.06c01","type":"ui_slider","z":"d2dd1bf4.363198","name":"","label":"Fan","tooltip":"0 to Full Speed","group":"2b2c4836.f67468","order":0,"width":0,"height":0,"passthru":true,"outs":"end","topic":"Fan Control","min":0,"max":10,"step":1,"x":1099,"y":557,"wires":[["e5b1531c.6fbcf"]]},{"id":"2b2c4836.f67468","type":"ui_group","z":"","name":"Test","tab":"66e1dcb.e5b8924","order":1,"disp":true,"width":"6","collapse":false},{"id":"66e1dcb.e5b8924","type":"ui_tab","z":"","name":"Test","icon":"dashboard","disabled":false,"hidden":false}]


Ideally, when the auto mode is ON, the flows will process the data and change the value of the slider. When auto mode is OFF, the flows will do nothing.
My problem for this flow is: When i turn the auto mode ON, it's working fine. But when i turn it back to OFF, it still process data.
Does anyone have any idea how to fix this?

Hi Kevin, welcome to the forum & to Node-RED.

Might be useful to include a screenshot of your flow, for people looking through many posts, it is time consuming to load flows unless we really need to.

As I understand it, you want to use a value as a gate to enable/disable processing?

Generally, this is done by pushing your gate value to a flow variable which you then check either in a switch node or in a function node. The other main way of doing this is to use one of the finite state machine nodes.

Thank you for your response. I have unloaded picture of the flow.

And you're right, i want to use a value as a gate to enable/disable the data processing. I have tried the function node with two if statement but it did not work. I'm not sure how the flow variable works. I'll have a closer look at this.
Thank you for your advises.

Hello,
Ok there is several problems. Lets start with syntax and technical difficulties.
First of all your Auto_OFF function wasn’t connected to the join node so it could never turn off. Second you set the payload instead of the topic that you need for the join node in that Auto_OFF function node that is not connected to anything.
So if you change those two things it should work but there is a few questions that remain and i’m not quite sure the join node and this method is what you want.

First a little problem with the join node like this is that the command to the fan will be repeated when you change the auto manual/mode. This is because the join mode will send a message with the content of both inputs if one arrives on either.
The next problem is that the join mode will in the beginning send no messages at all till it has received a message from both topics. So you would have to initialize auto or manual on redeploy.
Have a look at something like https://flows.nodered.org/node/node-red-contrib-simple-gate instead of a join node which will do exactly what you want and less headaches for you.
If you would prefer to stay with core nodes only the classic way would be to use a change node behind the auto/manual switch to set a flow.var to its value. The two flows wouldn’t need to be connected and instead of a join node you would use a switch node that checks this flow.var.
Both those approaches would also simplify your Control function as you wouldn’t have to check for auto or manual there as messages would only arrive there in the first place when auto is activated.
Than there is one further question i have. Why do you use a slider for your fan but you only ever send 1 or 0 to it due to the control node? wouldn’t a switch make more sense?
I hope this gave you some pointers to think about.

Johannes

Thanks Johannes,
The simple gate is working very well for my application.
Thank you for pointing out the problems with my flow too. I wasn't really sure about making the join node work for my application. The problems are now fixed by using simple-gate node.
On the other hand, i use the slider for the fan because eventually i will change the Control function to set speed for the fan (not only ON/OFF, but also running at 0 speed, 25% speed, 50% speed and full speed...).
Thank you again, i appreciate for your help.

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