Display temperature in datafield

Can someone please help me. I'm trying to get the temperature sensor reading from an edge 1030 to display on a datafield.

According to the API documentation, sensor toybox is not available with datafields and sensor history is not compatible with edge devices. However, I know you can do it because I have a datafield installed on my device that displays the temperature. I have tried reading the API documentation, but I am lost.

  • Thanks Jim, works perfectly now! You still don't have a Paypal account, so that I can send you some money? I really appriciate your help. The last time I asked you about it, you suggested the best thing to do was to help out others in this forum, but as non-programmer I find that quite difficult.

  • Former Member
    Former Member in reply to GHG23

    Hi,

    I am trying to display temperature on EDGE Datafield and I would like to request if you can share your code?

    Thanks in advance,

  • The Edge has an internal temperature sensor and is not connected to a human body, which makes its temperature readings as good as Garmin Tempe temperature readings. Getting the internal temperature of the edge however doesn't require a background process and a Tempe, so I think you are best helped with code for that:

    var sensorIter = getIterator();
    
    fieldValue = (sensorIter != null) ? sensorIter.next().data : 0;
    fieldValue = (utempunits == false) ? fieldValue : fieldValue*1.8+32;
    
    
    function getIterator() {
        //! Check device for SensorHistory compatibility
        if ((Toybox has :SensorHistory) && (Toybox.SensorHistory has :getTemperatureHistory)) {
            return Toybox.SensorHistory.getTemperatureHistory({});
        }
        return null;
    }

    The code is for onUpdate, the function is in the same class as onUpdate. The variable utempunits is based on user input in the settings. When it is true the user wants to use Fahrenheit instead of Celcius.

    Also you need to have a permission in the manifest file:

    <iq:uses-permission id="Sensor"/>

  • makes its temperature readings as good as Garmin Tempe temperature readings

    Just a small remark. The temperature sensor readings in the Edge aren't bad, it's definitly better than wearing a watch and then looking at the internal temp, but the readings on the edge aren't as good as from a Tempe. Not to mention that your Edge is exposed to direct sunlight, where you can hide the tempe underneath your saddle.

  • Well, I have been wondering about the accuracy of the Tempe. Based on your post I found: https://forums.garmin.com/apps-software/mobile-apps-web/f/garmin-connect-web/67004/garmin-tempe---accuracy

    So Garmin says accuracy is 1 degree Celcius. Having it under the saddle is a good place, as the device is black and shouldn't be measuring temperature in the direct sunlight. When running I have it on my shoe laces and it often has higher readings then my weather station. It is also closer to the warmer asphalt.

    The accuracy of an Edge is unknown to me. If one is cycling in the direct sun, having a temperature reading from the Edge is also helpful however, as one's body experiences the heat of the sun as well.

    Edit: the Edge 1030 temperature readings aren't very well indeed, according to the post in this thread, where it is compared to the Tempe:

    https://forums.garmin.com/sports-fitness/cycling/f/edge-1030/239201/temperature-sensor-correction

    The difference isn't linear as well, so a compensation algorithm would be fiddling

  • In the App.mc file add this

    function initialize() {
    AppBase.initialize();
    if(Toybox.System has :ServiceDelegate) {
    Background.registerForTemporalEvent(new Time.Duration(5*60));
    }
    }

    function onBackgroundData(Temperature) {
    Storage.setValue("mytemp", Temperature);
    }

    In View.mc file add

    (:background)
    class TemperatureServiceDelegate extends System.ServiceDelegate {
    function initialize() {
    ServiceDelegate.initialize();
    }

    function onTemporalEvent() {
    Sensor.setEnabledSensors([Sensor.SENSOR_TEMPERATURE]);
    Sensor.enableSensorEvents(method(:onSensor));
    }

    function onSensor(sensorInfo) {
    var sensorInfo = Sensor.getInfo();
    if (sensorInfo has :temperature && sensorInfo.temperature != null) {
    Temperature = sensorInfo.temperature;
    } else {
    Temperature = 0.0f;
    }
    Background.exit(Temperature);
    }
    }

    Under function onUpdate(dc) add

    Temperature = Storage.getValue("mytemp");
     

  • Former Member
    Former Member in reply to GHG23

    Thank you so much!!!!

  • Actually, in the background for DF, you don't need to enable the sensors, as they are already on, so you can get by with

    function onTemporalEvent() {
        var sinfo=Sensor.getInfo();
        var temp=sinfo.temperature;
        if(temp==null) {temp=-999.0;}
        Background.exit(temp);
    }

    I can't think of a device where the "has" would be needed.

    For edge devices, you get the internal sensor value, for others, you'll get the value from a connected Tempe or null if there isn't one. Also, remember that 0.0 is a valid temperature, so not the best way to indicate there is no temperature.  Maybe use something like -999.0

  • Former Member
    Former Member in reply to jim_m_58

    Hi jim_m_58,

    If I implement your function I am getting the error....

    Error: Permission Required
    Details: Symbol 'setEnabledSensors' not available to 'Data Field'

    Altough I have checkbox Sensor on in manifest.xml file. Do you know what's wrong? I am dev a datafield for EDGE 1030.

    Thanks in advance,

  • My code doesn't use setEnabledSensors.  The code I posted is for a background process, not the main DF.