# One function cancels the other

**URL:** <https://discourse.nodered.org/t/one-function-cancels-the-other/7719>\
**Category:** General\
**Created:** [7 February 2019 16:32 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719 "2019-02-07T16:32:15Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mambui](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mambui](https://discourse.nodered.org/u/mambui)\
**Post date:** [7 February 2019 16:32 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/1 "2019-02-07T16:32:15Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 February 2019 17:08 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/2 "2019-02-07T17:08:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mambui](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mambui](https://discourse.nodered.org/u/mambui)\
**Post date:** [7 February 2019 17:16 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/3 "2019-02-07T17:16:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [7 February 2019 17:34 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/4 "2019-02-07T17:34:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 February 2019 17:51 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/5 "2019-02-07T17:51:43Z")

</div>

I second @ukmoose recommendation

---

<div class="post-metadata">

**Author:** ![mambui](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mambui](https://discourse.nodered.org/u/mambui)\
**Post date:** [10 February 2019 16:36 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/6 "2019-02-10T16:36:56Z")

</div>

Thanks to you both for your amazing contribution. 🙂

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 :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/99559ccf2c9a3f43a53727822338f606bc820d56.png)

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!

---

<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:** [10 February 2019 16:51 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/7 "2019-02-10T16:51:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [10 February 2019 18:18 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/8 "2019-02-10T18:18:12Z")

</div>

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:-)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 February 2019 18:48 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/9 "2019-02-10T18:48:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [11 February 2019 00:23 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/10 "2019-02-11T00:23:13Z")

</div>

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

 ![r-02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/87c9823f5adc2a34ea146e41e5c1538fc05a13e9.png)

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

 ![r-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0f58e0d6862af1d81fed40ecac5c6eee7aa40c1e.png)

Flow

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

```

---

<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:** [11 February 2019 08:28 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/11 "2019-02-11T08:28:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mambui](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mambui](https://discourse.nodered.org/u/mambui)\
**Post date:** [12 February 2019 11:42 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/12 "2019-02-12T11:42:32Z")

</div>

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".

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e19aa4b94d2d3575611ffa93f04e261adcc76303.png)

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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 February 2019 11:57 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/13 "2019-02-12T11:57:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mambui](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mambui](https://discourse.nodered.org/u/mambui)\
**Post date:** [12 February 2019 21:45 UTC](https://discourse.nodered.org/t/one-function-cancels-the-other/7719/14 "2019-02-12T21:45:47Z")

</div>

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