Rate limiting for notifications?

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?

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.

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)

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;
1 Like

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.

1 Like

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.

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.

1 Like

Great. Thank you, Colin.

So something like this?

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;
}

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

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

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

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

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.

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