ChipChop Support Forum
Log in
Log out
Join the forum
My Details
REPLIES: 3
VIEWS: 85
BACK
REPLY TO THIS POST
SebyLT
04 Jan 2025
Sending notification from Sketch
Hi, I created a notification using "Actions". However, when the value drops below 31%, I receive the notification but am inundated with messages.

I would prefer to send the notification directly from the sketch, for example as shown below, is this possible?

if (Livello <= 30 && flagACQ == 0 && ++LivelloBassoCount == 15 ) {
ChipChop.triggerEvent("Livello basso","Livello acqua basso");
flagACQ = 1 ;}

if (Livello <= 30 && flagACQ == 1 && ++LivelloBassoCount == 43200 ) {//COUNT 12ore
ChipChop.triggerEvent("Livello basso","Livello acqua basso");
flagACQ = 1 ; LivelloBassoCount = 0 ; }

if (Livello >= 40 ){flagACQ = 0; LivelloBassoCount = 0 ;}


Attached images
Gizmo
04 Jan 2025

Hi Seby,

Yes, it should be quite easy to do that,

Instructions:

1. in the Dev Console an extra component for your device called "livello_acqua_alert" , component type "text"
2. change the action like this:

IF
    Trigger Device > Test
    Trigger Component > livello_acqua_alert
    Value is != "no"

THEN
    
    Send notification > "Livelllo dell'acqua "
    ✔️Include Component Status
    
    (this will send you a notification with text like: "Livelllo dell'acqua basso" or "Livelllo dell'acqua alto")

3. in your code do this



    if (Livello <= 30 && flagACQ == 0 && ++LivelloBassoCount == 15 ) {
        
        // this will happen straight away and trigger the action
        ChipChop.triggerEvent("livello_acqua_alert","basso");
        
        // immediately change the status to avoid flood of notifications
        ChipChop.updateStatus("livello_acqua_alert","no");

        flagACQ = 1 ;
    }

    if (Livello <= 30 && flagACQ == 1 && ++LivelloBassoCount == 43200 ) {//COUNT 12ore
        // this will happen straight away and trigger the action
        ChipChop.triggerEvent("livello_acqua_alert","basso");
        
        // immediately change the status to avoid flood of notifications
        ChipChop.updateStatus("livello_acqua_alert","no");

        flagACQ = 1 ;
        LivelloBassoCount = 0 ;
    }

    if (Livello >= 40 ){flagACQ = 0; LivelloBassoCount = 0 ;}



Doing it this way you can also have a notification for other water levels alto, basso etc. The action will only execute when the livello_acqua_alert is not "no"

The important thing to do is after sending a triggerEvent() to immediately use updateStatus() to "reset" the alert to "no" (you can use whatever word you want it doesn't have to be "no", can be "nessuno", "ok" ...anything that is not "basso" or "alto" because those you want to trigger the alert )


Notifications can be tricky to implement to avoid flooding yourself with messages and this is a pretty safe way.


I will be adding a direct notification functionality from the device (triggerNotification) in the next library release but this is still couple of months away as I have a lot more of work to do on the servers side (I have been working on this new release for 6 months now)


Let me know if this works ok or if you have any problems








SebyLT
04 Jan 2025

Thanks Gizmo I will try as recommended by you :)


I was thinking that it might be useful to implement sending notifications from Sketch, as in my case using flags to avoid continuous sending of notifications. Or, in the "Actions" section, you could add a function to manage the frequency of notifications . For example, in the "TIME IS" section, if the example value in my case is 30, the system could send only one notification every configurable interval (e.g. 1 second, 1 hour, 3 hours, 12 hours, etc.).

Thanks for your work can't wait to see the new updates :)


Attached images
Gizmo
04 Jan 2025

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 1
This 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 2
This 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 :-)