The triggerNotification() will definitely happen I promise!
Here is a code example with limiting the rate of notifications and it's also more generic so you can use it for multiple components. This will also work not just for notifications but if you have multiple devices controlling each other like a door bell and a sound device or motion sensor and lights etc...
1. I will use a component I've called "alert"
2. Action
IF Trigger Device > Test
Trigger Component > alert
Value is != "ok"
THEN Send notification > "Alert >> "
✔️Include Component Status
or you can control another device
Device 2 >> target component >> do something
Example 1This will limit the notifications on a specified interval but if you try to send something between the allowed interval it will not go through
// specify the minimum time between alerts/notifications.
// I wouldn't go less than 20 sec as ChipChop may refuse it and also it would be annoying getting notifications too quickly
unsigned long alert_frequency = 30000; //30 sec
unsigned long last_alert_time = 0;
void sendAlert(String message){
if(millis() - last_alert_time >= alert_frequency){
ChipChop.triggerEvent("alert",message);
ChipChop.updateStatus("alert","ok");
last_alert_time = millis();
}
}
void loop(){
if(something == HIGH){
sendAlert("high");
}else if(something == LOW){
sendAlert("low");
}else{
/// something else
sendAlert("some other message");
}
}
Example 2This will limit the notifications on a specified interval but will keep buffering up to 3 notifications and will send them one after another
When the buffer reaches 3 it will erase the first one and add the new one at the end
//we can set this to 21 sec to stay within 3 notifications per minute
unsigned long alert_frequency = 21000; //21 sec
unsigned long last_alert_time = 0;
int buffer_length = 3;
String buffer[3] = {"","",""};
void clearAlerts(){
for(byte i = 1; i <= 2;i++){
buffer[i] = "";
}
}
void shiftBuffer(){
for(byte i = 0; i <= 1;i++){
buffer[i] = buffer[i + 1];
}
buffer[2] = "";
}
void addAlert(String message){
int freeSlot = buffer_length + 1;
for(byte i = 0; i <= 2;i++){
if(buffer[i] == ""){
freeSlot = i;
break;
}
}
if(freeSlot > buffer_length){
shiftBuffer();
freeSlot = 2;
}
buffer[freeSlot] = message;
}
void alertCheck(String message){
if(millis() - last_alert_time >= alert_frequency && buffer[0] != ""){
ChipChop.triggerEvent("alert",buffer[0]);
ChipChop.updateStatus("alert","ok");
last_alert_time = millis();
}
}
void loop(){
if(something == HIGH){
addAlert("high"); //add the alert to the buffer
}else if(something == LOW){
addAlert("low");
}else{
/// something else
// or
//you can also cancel any queued alerts if they are not relevant anymore
clearAlerts();
}
// keep running this function and if there is something to send it will be sent on a correct interval
alertCheck();
}
There are probably other logical possibilities how to handle the notifications. Both options should work ok depending on what you need, Option 2 is providing more of a guarantee that an alert will happen but it could also be annoying.
I've written this quickly, so it may not be 100% correct :-)