Simulator and Toybox.SensorHistory

I just need to make sure I'm doing everything right. Does Toybox.SensorHistory.getHeartRateHistory pass static data or should it be simulated?
No matter what I try to do in the simulator, I always get out:

SensorHistory.getHeartRateHistory({:period => 60, :order => true}); // [89, 90, 87, 82, 80, 82, 87, 90, 0, 83,
80, 80, 85, 89, 88, 84, 80, 79, 83, 87, 88, 85, 80, 78, 81, 86, 88, 85, 81, 78, 79, 84, 87, 86, 81, 77, 78, 82,
86, 86, 82, 78, 76, 80, 84, 86, 83, 78, 76, 78, 82, 85, 83, 79, 75, 76, 81, 84, 0, 80]

And this values are actually what. Last 60 records for heart rate for every minute (average?) for the last hour? Or is it 60 records in the last minute? Or?

  • In the sim you get canned data. On this screen you see data being played back for the top 3 values and the bottom one, but the Graph is from getHeartrateHistory:

  • Thank you. I like to hear that they are only default static values ​​and it will be ok on a real watch. 
    And one value means the average heart rate for one minute and it shows me the history of the last hour? Or minutes and the values ​​are for every second?

  • getHeartrateHisory has samples every minute or two (it was 96 seconds on a last device I checked it on), and the history will be on the order of 4-6 hours of data last I checked.  HR/Avg/Max in the above screen shot is from Activity.Indo, as I'm playing back and recording an activity.

    If you want to see it on your own device, the app in the screen shot is my Hike2+ app

    https://apps.garmin.com/en-US/apps/116a5b59-29ae-4397-a70e-907d7e5f8e44

  • Do you have more information, please, why the history does not work for some sensors?

    // working
    getElevationHistory
    getHeartRateHistory
    getOxygenSaturationHistory
    getPressureHistory
    getTemperatureHistory
    
    // NOT WORKING
    getBodyBatteryHistory
    getStressHistory

    Error: Unexpected Type Error

      function getHistoryIterator() {
        if ((Toybox has :SensorHistory) && (SensorHistory has :getStressHistory)) {
          return SensorHistory.getStressHistory({:period => 60, :order => true});
        }
        return null;
      }
    
      function getHistoryData() {
        var dataIterator = getHistoryIterator();
        if (dataIterator != null) {
          dataHistory = [];
          for (var i = 0; i < 60; i++) {
            var sample = dataIterator.next();
            if (sample.data != null) { // ERROR ON THIS LINE
              dataHistory.add(sample.data);
            } else {
              dataHistory.add(0);
            }
          }
          return dataHistory;
        }
        return null;
      }

  • the next() can be null, you should check for null

    so you ilne 13 :  var sample = dataIterator.next() can return null, so trying to read data on null = crash

    https://developer.garmin.com/connect-iq/api-docs/Toybox/SensorHistory/SensorHistoryIterator.html

    stress and body battery can have null data inside the history (like HR if you remove your device)

  • It depends on a couple things. 

    For example, if the devices doesn't have a baro altimeter, you can't access getPressureHistory or getTemperatureHistory.

    and it also depends on the device API level and supported devices.

    For example, getStressHistory has an API level of 3.3.0 or greater and same with getBodyBatteryHistory

    After you do the "has" check for SensorHistory, if that's true, you want do a has check for each type of history.

    Kind of like this:

        	hasSH=(Toybox has :SensorHistory);
        	if(hasSH) {
        		hasSHHr=(Toybox.SensorHistory has :getHeartRateHistory);
        		hasSHTemp=(Toybox.SensorHistory has :getTemperatureHistory);
        		hasSHElev=(Toybox.SensorHistory has :getElevationHistory);
        		hasSHPres=(Toybox.SensorHistory has :getPressureHistory);
    	    }

    And you do want to do a null check even if a has is true, as there could not be any data yet (like right after you turn on a real device) or if the sensor is turned off.

  • I tend to always use the API Doc in an SDK and you will find the info there.  In Visual C, go to the Command Palette, then Monkey C: View Documentation and then, API Docs