# Rate limiting for notifications?

**URL:** <https://discourse.nodered.org/t/rate-limiting-for-notifications/51929>\
**Category:** General\
**Created:** [6 October 2021 15:30 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929 "2021-10-06T15:30:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![daxliniere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daxliniere/32/42187_2.png) [@daxliniere](https://discourse.nodered.org/u/daxliniere)\
**Post date:** [6 October 2021 15:30 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/1 "2021-10-06T15:30:30Z")

</div>

Hey gang,  
I'm working on a flow that sends me a notification when my 3D printer job is almost finished. This is determined by an http-request every 30 seconds and a threshold of 98%.  
The problem I'm encountering is that after it reaches 98%, it continues to send me notifications every 30 seconds.  
I haven't been able to work out what I should do to solve this. Does anyone have a suggestion I can test out?

```auto
if (msg.payload.progress >=98){
msg.priority = 0;
msg.timestamp = new Date().getTime();
msg.topic = "3D print finishing";
msg.payload = "Mars 2 Pro is about to finish the print job<br>-M2P server";
msg.sound = "magic";

return msg;
}

```

All the best,  
Dax.

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [6 October 2021 15:48 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/2 "2021-10-06T15:48:03Z")

</div>

Hi, you need to set a flag variable at the end of your code that toggles once when \>= 98, and test the flag at the start of the block:  
(bad syntax, to illustrate the idea)

```auto
if (msg.payload.progress >=98){
**if flag = false**
msg.priority = 0;
msg.timestamp = new Date().getTime();
msg.topic = "3D print finishing";
msg.payload = "Mars 2 Pro is about to finish the print job<br>-M2P server";
msg.sound = "magic";
**set flag = true**
return msg;

```

---

<div class="post-metadata">

**Author:** ![daxliniere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daxliniere/32/42187_2.png) [@daxliniere](https://discourse.nodered.org/u/daxliniere)\
**Post date:** [6 October 2021 15:56 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/3 "2021-10-06T15:56:26Z")

</div>

Ahh! That's a great idea. When, when a print has started (let's say, progress = 1%), the flag is reset.

Awesome. I love the logic.

Thank you so much!

Dax.

---

<div class="post-metadata">

**Author:** ![daxliniere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daxliniere/32/42187_2.png) [@daxliniere](https://discourse.nodered.org/u/daxliniere)\
**Post date:** [6 October 2021 15:58 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/4 "2021-10-06T15:58:23Z")

</div>

Out of interest, where is the flag variable accessible? As in, does it exist in that node only, the flow or all flows in NR?

Thanks again,  
Dax.

---

<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:** [6 October 2021 15:59 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/5 "2021-10-06T15:59:54Z")

</div>

That depends where you store it. Normally for something like this you would put it in the node context, then it will be available only in that function. Alternatively you could put it in flow or global context. Look in the docs for writing functions and for working with context for details.

---

<div class="post-metadata">

**Author:** ![daxliniere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daxliniere/32/42187_2.png) [@daxliniere](https://discourse.nodered.org/u/daxliniere)\
**Post date:** [6 October 2021 16:15 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/6 "2021-10-06T16:15:49Z")

</div>

Great. Thank you, Colin.

---

<div class="post-metadata">

**Author:** ![daxliniere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daxliniere/32/42187_2.png) [@daxliniere](https://discourse.nodered.org/u/daxliniere)\
**Post date:** [6 October 2021 16:18 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/7 "2021-10-06T16:18:12Z")

</div>

So something like this?

```auto
if (msg.payload.progress <=2){
CompletedFlag = 0;
}

if (msg.payload.progress >=98 && CompletedFlag = 0){
msg.priority = 0;
msg.timestamp = new Date().getTime();
msg.topic = "3D print finishing";
msg.payload = "Mars 2 Pro is about to finish the print job<br>-M2P server";
msg.sound = "magic";
CompletedFlag = 1;

return msg;
}

```

---

<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:** [6 October 2021 16:29 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/8 "2021-10-06T16:29:21Z")

</div>

> [@daxliniere](#):
>
> `if (msg.payload.progress >=98 && CompletedFlag = 0){`

That should be  
`if (msg.payload.progress >=98 && CompletedFlag === 0){`

Strictly, at the start it should be  
`let CompletedFlag = 0`  
but that won't work anyway, because you are not saving the value in context. As you have it then each time the function is run it starts by setting completed to 0

---

<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:** [6 October 2021 16:57 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/9 "2021-10-06T16:57:54Z")

</div>

This would be simple using filter , switch and change node.  
e.g.

```auto
[{"id":"1d88c4fe.66b62b","type":"inject","z":"b779de97.b1b46","name":"1","props":[{"p":"payload.progress","v":"1","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":220,"y":4020,"wires":[["779122e.80aca5c"]]},{"id":"779122e.80aca5c","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"payload.progress","pt":"msg","to":"payload.progress >= 98","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":4020,"wires":[["76a0a2c8.3a55fc"]]},{"id":"96b6e747.44ec1","type":"inject","z":"b779de97.b1b46","name":"98","props":[{"p":"payload.progress","v":"98","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":4060,"wires":[["779122e.80aca5c"]]},{"id":"de3c627.5bceaa","type":"inject","z":"b779de97.b1b46","name":"99","props":[{"p":"payload.progress","v":"99","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":4100,"wires":[["779122e.80aca5c"]]},{"id":"76a0a2c8.3a55fc","type":"rbe","z":"b779de97.b1b46","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload.progress","x":550,"y":4060,"wires":[["72800bf2.7bb214"]]},{"id":"72800bf2.7bb214","type":"switch","z":"b779de97.b1b46","name":"","property":"payload.progress","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":700,"y":4060,"wires":[["b1ee000d.6bb718"]]},{"id":"b1ee000d.6bb718","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"priority","pt":"msg","to":"0","tot":"str"},{"t":"set","p":"timestamp","pt":"msg","to":"","tot":"date"},{"t":"set","p":"topic","pt":"msg","to":"3D print finishing","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Mars 2 Pro is about to finish the print job<br>-M2P server","tot":"str"},{"t":"set","p":"sound","pt":"msg","to":"magic","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":4060,"wires":[["d147b3bb.224868"]]},{"id":"d147b3bb.224868","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":640,"y":4120,"wires":[]}]

```

---

<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:** [6 October 2021 17:07 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/10 "2021-10-06T17:07:18Z")

</div>

What printer software are you using? I know that octoprint can send the status of the print job via mqtt with some plugins.

---

<div class="post-metadata">

**Author:** ![daxliniere](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daxliniere/32/42187_2.png) [@daxliniere](https://discourse.nodered.org/u/daxliniere)\
**Post date:** [6 October 2021 17:18 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/11 "2021-10-06T17:18:15Z")

</div>

Hey @zenofmud, it's Mariner3D - [GitHub - luizribeiro/mariner: Web interface for controlling MSLA 3D Printers based on Chitu controllers, such as the ones by Elegoo and Phrozen.](https://github.com/luizribeiro/mariner)

---

<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:** [20 October 2021 17:18 UTC](https://discourse.nodered.org/t/rate-limiting-for-notifications/51929/12 "2021-10-20T17:18:38Z")

</div>

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