ChipChop Support Forum
Log in
Log out
Join the forum
My Details
REPLIES: 2
VIEWS: 93
BACK
REPLY TO THIS POST
bruckbar
01 Nov 2024
Update status but only once?
Hi guys, not sure if I can explain this correctly, I would like to show a motion sensor status but only when it detects movement so I can see the exact time when it happened?

What I mean is when the sensor detects motion I send a triggerEvent("motion_sensor", "DETECTED") and when I look in the app it shows it straight away with the time, but 10 seconds later the status is re-updated and of course the time is now different.
If I reset the status to Not Detected then I have no idea that the motion was detected unless I send myself a notification which is not always an option.

Do you see what I mean?

Is it possible to send the sensor status so it get's marked in the app with the time and then only update it when the sensor detects something again so I can see when was the last time it happened?

Probably asking for too much but thanks anyway
Gizmo
02 Nov 2024

Hi Bruck,

What you are looking for is ChipChop.removeStatus("motion_sensor");

It's explained in the examples folder of the ChipChop library in the all-functions.ino, pretty much covers exactly what you are looking for! 🙃

When you do a ChipChop.updateStatus() or ChipChop.triggerEvent() the library will remember the component you've specified and keep sending its status on every heartbeat even if it doesn't change
The ChipChop.removeStatus() removes a specific component from the status sending buffer until updateStatus() or triggerEvent() is called on that component again

Example:



void loop(){
    

    // this is the point when you need to "mark" your motion sensor detection event

    //this will happen instantly and will show in the app with the current timestamp
    ChipChop.triggerEvent("motion_sensor","DETECTED");

    //remove the "motion_sensor" from the heartbeat buffer immediately after the triggerEvent so the timestamp won't be overwritten on future heartbeats
    ChipChop.removeStatus("motion_sensor");


}




That's pretty much all you have to do

Note:

This wouldn't work



void loop(){

    ChipChop.updateStatus("motion_sensor","DETECTED");

    ChipChop.removeStatus("motion_sensor");


}




The reason is that updateStatus() only tells the library to remember that particular component state but we don't know exactly when the library will send that status so calling removeStatus() would most likely cancel out the updateStatus()

That's why we need to use triggerEvent() as it bypasses the heartbeat cycle and is sent immediately guaranteeing that the status will be delivered and then we can safely use removeStatus() to stop future status updates until explicitly called again.

Hope this helps

tc

G ✌️

bruckbar
03 Nov 2024

I can confirm that that works 🥳🍾 thnx!