Weather for Datafield

I think it´s not possible by now because edge devices has not 3.2 but if i wish to get weather on a data field i should be doing this?

    using Toybox.Weather;

    var makeTemp;
    var windDir;
    var windSpd;

    function compute(info) {
        if (info has :windBearing) {
            windDir = info.windBearing;
        }

        if (info has :windSpeed) {
            windSpd = info.windSpeed;
        }

        if (info has :temperature) {
            makeTemp = info.temperature;
        }
    }

  • it does not work with if(hasWeather) i mean the code works fine on 3.2 devices but it does not work on 3.1 devices i mean i want to use these values when is not a 3.2

    windDir = null;
    windSpd = 0;
    makeTemp = 18;



  • You want to move your default values to the else section of the hasWeather check in order to use if as the default for non 3.2 devices.

    Something like this: (edited to show full picture based on Kurev's comments)

    using Toybox.Weather;
    
    class WeatherTest {
    
    var hasWeather=false;
    
        function initialize() {
            if (Toybox has :Weather) {
                    hasWeather=true;
            }
        }
    
        function compute(info) {
            if(hasWeather) {
                var cc = Weather.getCurrentConditions();
                if(cc!=null) {
                    windDir = cc.windBearing.toFloat();
                    windSpd = cc.windSpeed.toNumber();
                    makeTemp = cc.temperature.toNumber();
                }
            } else {
                windDir = null;
                windSpd = 0;
                makeTemp = 18;
            }
        }
    }

  • still do not works, the only solution I see I to use different codes for 3.2 and 3.1

  • You are also mixing up namespace and variables in a bad way:

      using Toybox.Weather as hasWeather;

    and then:

      var hasWeather=(Toybox has :Weather);

    I'm not sure why this even compiles, but they must be different names. 

    Also var hasWeather must be defined outside from initialize() as class member variable, so it is visible also in compute()

  • GENIUS!! now it´s working just do not work de else value but now at least runs on 3.1 devices!!

  • Kurev, good call on the namespace and variable.  I have updated the code to show the proper full class for future reference.

  • I guess I should have posted the full code instead of just the compute function, as it seems "hasWeather" is confusing folks, as are my helper functions.

    In the case of "hasWeather", that's my top rule when using "has". You only need to do a "has" once, as it will never change when an app is running, and it's actually checking a dictionary each time. It's more efficient to just check a boolean.

    Next time I'll post the whole thing,

  • Now the app is working , but i have a report that sometimes works and sometimes not, i do not have a 3.2 device to check so my question is:

    does weather need to be connected to garmin connect? and if i need to check positioning or something else on permissions?? thanks a lot

  • You need to be in contact with a phone running GCM, and the times the weather updates may vary

  • thanks a lot jim, i´ll specify that on the requirements.