# Combination of Rate Limit and RBE node?

**URL:** https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034
**Category:** General
**Created:** [29 October 2020 16:08 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034 "2020-10-29T16:08:48Z")
**Posts on this page:** 18
**Page:** 1

<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: [29 October 2020 16:08 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/1 "2020-10-29T16:08:49Z")

</div>

I have a message coming in every 5 seconds containing 0 or 1 (it is the required state of a relay output). I want to log it to Influx but I only want to store it every five minutes unless it changes. It seems I need a sort of combined Rate Limit node and RBE where it rate limits but lets through immediately when the value changes. I haven't managed to come up with a neat solution to this, so this afternoons competition is to see who can come up with the most pleasing suggestion.  
I don't want to use an RBE followed by a message repeater as, if the input messages stop, then I want to stop logging to the database.  
I could do it quite easily in a function node, but that is cheating.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [29 October 2020 16:59 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/2 "2020-10-29T16:59:36Z")

</div>

```auto
[{"id":"e29e4e5b.2918e","type":"inject","z":"eeee575.b4b80a8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":160,"y":1000,"wires":[["b78580a3.76d19"]]},{"id":"b78580a3.76d19","type":"rbe","z":"eeee575.b4b80a8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":310,"y":1000,"wires":[["7d113250.989adc","15bdbeaa.83fe21"]]},{"id":"7d113250.989adc","type":"trigger","z":"eeee575.b4b80a8","name":"","op1":"","op2":"","op1type":"pay","op2type":"pay","duration":"-2","extend":false,"overrideDelay":false,"units":"s","reset":"reset","bytopic":"all","topic":"topic","outputs":1,"x":520,"y":1000,"wires":[["39a0220.c3dc8de"]]},{"id":"93163cd.0580bc","type":"inject","z":"eeee575.b4b80a8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":170,"y":1040,"wires":[["b78580a3.76d19"]]},{"id":"15bdbeaa.83fe21","type":"trigger","z":"eeee575.b4b80a8","name":"","op1":"","op2":"reset","op1type":"nul","op2type":"str","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":410,"y":1080,"wires":[["7d113250.989adc"]]},{"id":"39a0220.c3dc8de","type":"debug","z":"eeee575.b4b80a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":1000,"wires":[]}]

```

---

<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: [29 October 2020 17:28 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/3 "2020-10-29T17:28:29Z")

</div>

If I keep inputting the same value then after a bit that flow stops until I send a change. All the time I keep inputting the same value it should output it regularly but at a slower rate. Then interject extra ones when the input changes.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [29 October 2020 17:58 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/4 "2020-10-29T17:58:58Z")

</div>

that is what that flow does... once you send something it sends every two seconds (change to 5 mins in your case) - but should send changes (let through by rbe)... but stops after a while (set reset trigger to whenever you want to stop it logging)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [29 October 2020 19:22 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/5 "2020-10-29T19:22:18Z")

</div>

Here is my effort needs context store though.

```auto
[{"id":"11b606e8.7ca019","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":90,"y":380,"wires":[["2f87e709.5ed798","f5131913.a601a8"]]},{"id":"b6b0490a.f74158","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":90,"y":440,"wires":[["2f87e709.5ed798","f5131913.a601a8"]]},{"id":"f5131913.a601a8","type":"delay","z":"8d22ae29.7df6d","name":"","pauseType":"delay","timeout":"200","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":220,"y":440,"wires":[["ab2045ad.f67ec"]]},{"id":"2f87e709.5ed798","type":"rbe","z":"8d22ae29.7df6d","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":310,"y":380,"wires":[["c7d100e2.fea9","73b7e636.b031c8"]]},{"id":"ab2045ad.f67ec","type":"switch","z":"8d22ae29.7df6d","name":"","property":"time","propertyType":"flow","rules":[{"t":"lt","v":"$toMillis($now()) ","vt":"jsonata"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":380,"y":420,"wires":[["179f8199.4651a6"],[]]},{"id":"c7d100e2.fea9","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"time","pt":"flow","to":"($toMillis($now()) + 5000)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":380,"wires":[[]]},{"id":"179f8199.4651a6","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"time","pt":"flow","to":"($toMillis($now()) + 5000)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":420,"wires":[["73b7e636.b031c8"]]},{"id":"73b7e636.b031c8","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"msg","x":670,"y":380,"wires":[]}]

```

---

<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: [29 October 2020 21:09 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/6 "2020-10-29T21:09:21Z")

</div>

All the time I keep clicking 0 for example it should keep sending out (rate limited) zeros.

---

<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: [29 October 2020 21:10 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/7 "2020-10-29T21:10:47Z")

</div>

> [@E1cid](#):
>
> Here is my effort

Yes, that does work, I am sure you would agree that it isn't very pretty though.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [29 October 2020 21:31 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/8 "2020-10-29T21:31:33Z")

</div>

Don't know about pretty, It seems to be more robust. I tried @dceejay way, but the ugly route worked better for me.

---

<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: [29 October 2020 21:49 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/9 "2020-10-29T21:49:20Z")

</div>

How about this one

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

The second rbe is to suppress duplicate messages when the same one is emitted by both the rate limit and the rbe nodes.

```auto
[{"id":"a8ab2ec3.838e38","type":"delay","z":"bdd7be38.d3b55","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":280,"y":4620,"wires":[["baaa13ea.e41a6"]]},{"id":"fc72e97d.d02f78","type":"rbe","z":"bdd7be38.d3b55","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":280,"y":4660,"wires":[["baaa13ea.e41a6"]]},{"id":"fa1143fa.826cb","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":90,"y":4620,"wires":[["a8ab2ec3.838e38","fc72e97d.d02f78"]]},{"id":"8f6dc68d.e4811","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":90,"y":4660,"wires":[["a8ab2ec3.838e38","fc72e97d.d02f78"]]},{"id":"baaa13ea.e41a6","type":"rbe","z":"bdd7be38.d3b55","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"_msgid","x":450,"y":4620,"wires":[["bc41c698.c69a7"]]},{"id":"bc41c698.c69a7","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":4680,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [29 October 2020 22:03 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/10 "2020-10-29T22:03:48Z")

</div>

> [@Colin](#):
>
> [{"id":"a8ab2ec3.838e38","type":"delay","z":"bdd7be38.d3b55","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":280,"y":4620,"wires":[["baaa13ea.e41a6"]]},

bad boy corrupt flow.

---

<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: [29 October 2020 22:08 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/11 "2020-10-29T22:08:42Z")

</div>

Aargh! Hoist with mine own petard.

Fixed now.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [29 October 2020 22:14 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/12 "2020-10-29T22:14:11Z")

</div>

Yes i tried that earlier, never thought of using \_msgid though.

Edit/ Yes pretty and robust.

---

<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: [29 October 2020 22:23 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/13 "2020-10-29T22:23:08Z")

</div>

> [@E1cid](#):
>
> never thought of using \_msgid though.

Not sure that is actually a good idea though, it is possible for multiple messages to have the same id, it depends how they originated. To make it safe I think I would have to put a change node on the front putting a reasonably unique id in the message. Can I do that reasonably efficiently with jsonata in a change node?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [29 October 2020 22:28 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/14 "2020-10-29T22:28:19Z")

</div>

just add a timestamp to the message.

---

<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: [31 October 2020 09:37 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/15 "2020-10-31T09:37:33Z")

</div>

> [@E1cid](#):
>
> add a timestamp to the message.

Good thinking. So this seems to be the best so far

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f67f60c703adaacc7823b4c26cca81841f1d9847.png)

```auto
[{"id":"a8ab2ec3.838e38","type":"delay","z":"bdd7be38.d3b55","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":470,"y":4620,"wires":[["baaa13ea.e41a6"]]},{"id":"fc72e97d.d02f78","type":"rbe","z":"bdd7be38.d3b55","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":450,"y":4660,"wires":[["baaa13ea.e41a6"]]},{"id":"fa1143fa.826cb","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":90,"y":4620,"wires":[["7ba6c786.a8ab3"]]},{"id":"8f6dc68d.e4811","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":90,"y":4660,"wires":[["7ba6c786.a8ab3"]]},{"id":"baaa13ea.e41a6","type":"rbe","z":"bdd7be38.d3b55","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"timestamp","x":630,"y":4620,"wires":[["bc41c698.c69a7"]]},{"id":"bc41c698.c69a7","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":4680,"wires":[]},{"id":"7ba6c786.a8ab3","type":"change","z":"bdd7be38.d3b55","name":"Set msg.timestamp","rules":[{"t":"set","p":"timestamp","pt":"msg","to":"$millis()\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":4640,"wires":[["a8ab2ec3.838e38","fc72e97d.d02f78"]]}]

```

Though, having also tried writing it in a Function node, it turns out to be very simple, so perhaps it is better just to use that.

```auto
const minInterval = 5000 // min interval between outputs, msec
const previous = context.get("previous") || {timestamp: 0}
const now = new Date().getTime()
if ( msg.payload == previous.value && (now - previous.timestamp) < minInterval )
{
    // same value as last one and too soon to send again, so send nothing
    msg = null
} else {
    previous.timestamp = now
    previous.value = msg.payload
    context.set("previous", previous)
}
return msg;

```

---

<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: [31 October 2020 10:00 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/16 "2020-10-31T10:00:40Z")

</div>

Colin, the way I look at it is, if the visual/node version does nothing to aid comprehension & uses many more nodes, then there is nowt wrong with a function node. Just litter it with comments and give it a good name.

---

<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: [31 October 2020 11:07 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/17 "2020-10-31T11:07:13Z")

</div>

Yes, I tend to agree.

---

<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: [30 December 2020 11:07 UTC](https://discourse.nodered.org/t/combination-of-rate-limit-and-rbe-node/35034/18 "2020-12-30T11:07:13Z")

</div>

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