State change detection based on emonlib current only

I am learning and at the very beginning in Arduino world.

I have a very simple sct013 000 current sensor that I would like to monitor (on and off) my electrical appliance and I can easily monitor this by serial monitor and logging over time in sdcard module and rtc module.

However, I need to log this only when the change occurs (1 or 0) and also record the time between each change. The current method that I’m using which cycles every sensor readings so many seconds throws up too much data.

My current sketch is emonlib for current only measurements from openenergymonitor.org

Any help or recommendations would be very helpful.

A full current measuring device is probably far more than you need. However, you are using this to learn about the Arduino, and I’m OK with that.

How often does your appliance switch on and off? The real question is - how much inaccuracy and delay can you accept between it switching and you detecting it?

The standard current-only sketch measures for approx 200 ms every 10 s. As you are not interested in measuring the current, but only knowing that there is some current, you can easily reduce the time it takes for the measurement - down to but not less than 1 mains cycle, and you can decrease the time between measurements - down to whatever you wish, but would 1 s be adequate?
(Note: the current-only sketch will measure about 100 samples per mains cycle.)

Having done that, as your Arduino is permanently powered, the obvious way to reduce the amount of data sent is to remember the last state (current / no current) in the Arduino and only send the new state when there is a change.

You can use the millis( ) function to record the time elapsed since switch-on, but read the Arduino documentation to be sure you get it right when that counter rolls over (after 50 days, from memory), so it is quite easy to determine the elapsed time between events.

Your other difficulty might well be deciding when your appliance is on or off. Checking for zero will probably not work, you will need to set a fixed threshold well above any noise and pick-up but far enough below the normal running current so that you don’t get false readings.

Thanks Robert,…for the knowledge…great job…!!

my final sketch :
/ EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance
int status="OFF";
int val=0;
int prevstat="OFF";
int currstat="OFF";
int countstat=0;

void setup()
{  
  Serial.begin(9600);
  
  emon1.current(1, 60.606);             // Current: input pin, calibration.
}

void loop()
{
  double Irms = (emon1.calcIrms(1480))/2;  // Calculate Irms only
//   countstat=countstat+1;
   
  if (Irms>0.31) //&& (countstat>2))
  {
  currstat="ON";
 
  }
   else {
  if (Irms<0.31) //&& (countstat>2))
  currstat="OFF"; 
  
   }

  if(currstat != prevstat)
  {
    if((currstat="ON")&&(prevstat !="ON"))
     { Serial.println("ON");
  }else
  {
     if((currstat="OFF")&&(prevstat !="OFF")) {
    Serial.println("OFF");
  }
   }
 }
   prevstat=currstat;
  delay(1000);

}

Something not good here:
“OFF” and “ON” aren’t strings, they are “symbolic constants” that are defined for you somewhere in the Arduino header files. So they don’t have quotes around them. The quotes are legal in a definition - they become part of the constant - but it makes them look like strings, and they are not. I often define my own:
#define LEDON LOW
#define LEDOFF HIGH
No quotes there. That looks silly but if you have a LED that is active-low, i.e. the LED is connected to the supply and is on when the output is at 0 V, it makes the state of the output that drives the LED ‘sensible’ in the logic. digitalWrite(LEDPin, LEDON); drives the output low and turns the LED on.

And why test again inside the “else”? What you’re doing is (a) wasting time and effort and adding complication (and maybe confusion), and (b) locking out the possibility of the current being exactly equal to 0.31. I know that’s a practical improbability with real numbers, but if you ever do it with an integer, you could be in big trouble. It’s a habit get out of, fast! But if you were measuring temperature that changed slowly, you would almost certainly need two different values to define a ‘dead band’ so that tiny changes as it crossed the set point didn’t cause the output to flutter. Check how a real thermostat works.

Why not make the states “boolean”? They can have only two values - it makes it clearer and easier to understand.
If you did that, you could shorten those 9 lines to:

currstat = (Irms > 0.31);

Do you understand how that can work? (Irms > 0.31) evaluates to either true or false, and you assign that to a Boolean, which can only be true or false. So currstat will be true for Irms greater than 0.31, false otherwise. If you don’t like currstat being true and false, define two new names at the top of your sketch:
#define CURRENT_ON true
#define CURRENT_OFF false

There’s also redundancy here if(currstat != prevstat)
If it’s ON (or OFF) now and not that the last time, it must be different!

Why divide the current by 2? You could just as easily change the detection level. And it’s bad practice to put numbers like 0.31 buried deep in the code. Much clearer and safer to use a const variable, declared and defined at the top:
const double setpoint = 0.31 // The current at which the appliance is surely running.
(Look up what const does for you.)

Get rid of all the variables you thought you wanted but never used. The compiler will most likely ignore them, but they’ll confuse you in a few months or years time.