One function cancels the other


#1

Hi folks,
Here is my first message on the Node-Red forum.
I would like to improve my flow. I have one switch called "Buy or Sell" that push a trade into two differents functions called"Buy" and the other called "Sell". You can see the image attached.
image

My issue is the following, I would like that as long as the trade goes to the same function, no action done by the flow. If the trade goes into the other function, activate the flow.

In practice, I can explain it that way: A first "buy" trade enters into the function "Buy", so as long as my information entering into the flows is a "buy" no action will be made. If suddenly, one "sell" trade is coming the flow get activated, and will be locked until a "buy" trade appear.

Thank you very much for your support.

Fabien


#2

So does the first one get passed on? i.e if you have buy1, buy2, buy3, sell4, sell5, buy6, sell7
Would buy1 exit the function node
buy 2 and buy 3 are discarded,
Sell 4 exits the function node
sell 5 is ignored
buy6 is processed
sell 7 is processed


#3

Hi zenofmud,
Thanks for your quick reply.

You are correct, the following statement is what I am looking for :

Buy 1 : processed
Buy 2 : ignored
Buy 3 : ignored
Sell 1 : processed
Sell 2 : ignored
Buy 1 : processed
And so on.


#4

Sounds like a job for the RBE node (report by exception)


#5

I second @ukmoose recommendation


#6

Thanks to you both for your amazing contribution. :slight_smile:

According to my flow, the RBE should be attached to the switch or the function ?

Therefore, I am trying to use a switch because some of the tweets I follow is using different wording such as bought/buy or purchase. I would like the switch to analyse this tweet information and send it to the correct functions buy or sell. Here is a picture of my flows :

In the split node, under string I've inserted \buy\bought\purchase\sell\sold (all the tweets wording)

In the switch node, I've inserted contains \buy\bought\purchase go to function 1 -> buy and contains \sell\sold go to function 2 -> sell

Thanks a lot for your support, what a community!


#7

What is it in the message that indicates buy or sell? Is it just text in the payload along with other text? Can you show some examples?


#8

Well the RBE node is certainly in the wrong place.

What I would do is use a function node to
set a msg property if msg.payload contains x,y,z
so for example msg.trade=“buy” is any of the buy terms

and msg.trade=“sell” for others

then a change node to filter any messages that don’t include either

then as you have one stream of messages you can then use a RBE node to only get the ones where msg.trade changes

For the function node you should be able to find out enough using google,( include javascript )and after all this forum should be a pout helping you not doing all the work:-)


#9

the output of the RBE should go to both the 'Set to Buy' and 'Set to Sell' nodes


#10

Combining several ideas from previous posts, you could use these nodes together.

I recommend using the regex expression so you can ignore case (sell == Sell)

Flow

[{"id":"9a49079a.7e1598","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"42a12abe.d104f4","type":"rbe","z":"9a49079a.7e1598","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":450,"y":120,"wires":[["ccc6d5b9.e153d8"]]},{"id":"ccc6d5b9.e153d8","type":"debug","z":"9a49079a.7e1598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":120,"wires":[]},{"id":"5f363d2f.f0f3f4","type":"switch","z":"9a49079a.7e1598","name":"regex","property":"payload","propertyType":"msg","rules":[{"t":"regex","v":"sell","vt":"str","case":true},{"t":"regex","v":"buy","vt":"str","case":true},{"t":"regex","v":"purchase","vt":"str","case":true}],"checkall":"true","repair":false,"outputs":3,"x":170,"y":120,"wires":[["442bdd02.fd1cd4"],["27824a1b.54fec6"],["27824a1b.54fec6"]]},{"id":"27824a1b.54fec6","type":"change","z":"9a49079a.7e1598","name":"buy","rules":[{"t":"set","p":"payload","pt":"msg","to":"buy","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":140,"wires":[["42a12abe.d104f4"]]},{"id":"442bdd02.fd1cd4","type":"change","z":"9a49079a.7e1598","name":"sell","rules":[{"t":"set","p":"payload","pt":"msg","to":"sell","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":100,"wires":[["42a12abe.d104f4"]]}]

#11

If you search for "sell" or "buy" in the string watch out for the case when whatever is being bought or sold contains those sequences of characters. If the sell/buy is always at the start of the string (or always just preceded by white space or something) then you could make the regex more selective to allow only that.


#12

Amazing and very well developped/precise answers here. thank you guys for your help.

I am testing the flow, the Regex is working very well, however I can notice the RBE doesn't work in my flow. A "buy" can be follwed by a "purchase" and by a "bought".

I have tried multiple different solutions but cannot find a workable one.

In my regex switch there is the whole wording used (purchase, buy, sell, sold, bought)

In my buy and sell switch, prior the RBE, there is specific wording (set - payload - to purchase, to buy, to bought) for the switch buy.

For the switch sell I have that specific wording (set payload to sell, to sell, to sold).

I try to replicate the exact wording in these two functions (buy and sell) that the working in the regex.


#13

First off, the two nodes connected to the RBE node are - according to your image - change nodes not switch nodes

you have two function `Set to Buy' and 'Set to Sell', why not have each of them set msg.topic to eithor 'buy' or 'sell' then have the RBE node trigger off of msg.topic?

You could then probably eliminate the two change nodes.


#14

Both RBE and Regex work well right now, that's perfect.
Thank you very much for your help.