Pressure difference last 3 hours

Hi,

I found on the internet a small program and I want to modify it to display the value of the barometer difference between current pressure and the last 3 hours.

Does anyone have an idea what I need to change.?

Thanks a lot

if ((Toybox has :SensorHistory) && (Toybox.SensorHistory has :getPressureHistory)) {

  var last1=null, last2=null;

  var itr = Toybox.SensorHistory.getPressureHistory({});

  var sample = itr.next();

  while (sample != null) {

    if (sample.data != null) {

      last1 = sample.data;

      break;

    }

    sample = itr.next();

  }

  sample = itr.next();

  while (sample != null) {

    if (sample.data != null) {

      last2 = sample.data;

      break;

    }

    sample = itr.next();

  }

  if (last1 != null && last2 != null) {

    return last1<last2 ? "decrease" : last1>last2 ? "increase" : "same";

  }

}

return null;

  • So what does your code actually do when you run it?  Are you using the sim or a real device? In the sim, what you see in SensorHistory is canned data - here's a graph from the sim.  Notice it starts low, gets the highest in the middle, and then drops back to the same low.

  • It looks like the code you posted:

    - grabs the 2 most recent valid pressure samples

    - compares them and returns the result of the comparison: "decrease", "increase" or "same"

    I want to modify it to display the value of the barometer difference between current pressure and the last 3 hours.

    I'll assume that:

    - the most recent valid pressure sample in the history is the "current pressure"

    - we want to compare the "current pressure" with any valid sample that's at least 3 hours older

    - you want to change the existing code as little as possible

    Try this:

    if ((Toybox has: SensorHistory) && (Toybox.SensorHistory has: getPressureHistory)) {
      var currentSample = null, previousSample = null;
      var itr = Toybox.SensorHistory.getPressureHistory({});
      var sample = itr.next();
      while (sample != null) {
        if (sample.data != null) {
          currentSample = sample.data;
          break;
        }
        sample = itr.next();
      }
      sample = itr.next();
      while (sample != null) {
        if (
          sample.data != null &&
          currentSample.when.value() - sample.when.value() >= 3 * 60 * 60 // 3 hours, in seconds
        ) {
          previousSample = sample.data;
          break;
        }
        sample = itr.next();
      }
      if (currentSample != null && previousSample != null) {
        return currentSample < previousSample ? "decrease" : currentSample > previousSample ? "increase" : "same";
      }
    }
    return null;

    I made the following changes:

    - renamed last1 to currentSample and last2 to previousSample

    - for the code which sets the second sample (last2/previousSample), added the following condition:

    currentSample.when.value() - sample.when.value() >= 3 * 60 * 60 // 3 hours, in seconds