responseCode != 200, weatherData symbol "--"?

Good day, I want to ask how to easily solve when I don't get the requested data, that means responseCode = 200, so e.g. will I have the symbol "--" for the current temperature? I'm currently doing it this way, but I have no idea if it's correct for watch memory for example?

Thanks for the advice and tips.

private var _temp  = "--" as String;

function OWM_Update(responseCode as Number, data as Dictionary or String or Null) as Void {
	
    if (responseCode != 200) 
    {
      System.println("NO WEATHER DATA");
      backgroundData["Temp"] = _temp;
	  
	  Background.exit(backgroundData);
    } else 
    {
    System.println("WEATHER DATA OK");
    backgroundData["Temp"] = ((data as Dictionary)["main"] as Dictionary)["temp"]as Number;
      
   Background.exit(backgroundData);
    }
     }

  • The question was, is this solution correct?

    Does it work for you? I feel like it doesn't, based on the code you posted.

    First of all, let's establish the format of the API response from OpenWeatherMap, so there's no confusion. Based on your code, I think you are using the 2.5 API and not the 3.x API

    Here's an example:

    https://openweathermap.org/current

    {
       "coord": {
          "lon": 7.367,
          "lat": 45.133
       },
       "weather": [
          {
             "id": 501,
             "main": "Rain",
             "description": "moderate rain",
             "icon": "10d"
          }
       ],
       "base": "stations",
       "main": {
          "temp": 284.2,
          "feels_like": 282.93,
          "temp_min": 283.06,
          "temp_max": 286.82,
          "pressure": 1021,
          "humidity": 60,
          "sea_level": 1021,
          "grnd_level": 910
       },
       "visibility": 10000,
       "wind": {
          "speed": 4.09,
          "deg": 121,
          "gust": 3.47
       },
       "rain": {
          "1h": 2.73
       },
       "clouds": {
          "all": 83
       },
       "dt": 1726660758,
       "sys": {
          "type": 1,
          "id": 6736,
          "country": "IT",
          "sunrise": 1726636384,
          "sunset": 1726680975
       },
       "timezone": 7200,
       "id": 3165523,
       "name": "Province of Turin",
       "cod": 200
    }                    
                        

    The response is a dictionary with a "main" key, which is a dictionary with a "temp" key, which is a Float, not a Number.

    - In OWM_Update (apparently the callback to makeWebRequest), data is apparently the OWM API response. backgroundData appears to be a dictionary, and backgroundData["Temp"] is set to either "--" or data["main"]["temp"]. The cast of ["temp"] to Number is wrong, but technically it doesn't matter because casts don't actually change the type of the value being casted, they only serve to placate the type checker.

    This means the background process returns a dictionary which looks like this:

    {
      "Temp" => "--"
    }

    or

    {
      "Temp" => 26.2 // or some other float
    }

    - onBackgroundData receives the above dictionary as data, but it tries to access data["main"]["temp"]?

    This will crash, as the dictionary the background process returned does not have a "main" key. onBackgroundData needs to work with the data that the background process returned via Background.exit(), not the data that was received by the callback to makeWebRequest (OWM_Update).

    It's also not clear how your view class is getting the temperature value. Sure, it could call Storage.getValue(), but that would be slow and wasteful in terms of battery. It would be better for onBackgroundData() to notify the view that an updated temperature value is available.

  • Here's one way to fix your code (I'm assuming that you only care about temperature, as suggested by the existing code).

    https://pastebin.com/awqAGfpS

  • Thanks for the great explanation. I am also interested in other weather data that I want to have on the watch, but the current temperature was only as an example.

    now I'm trying to do what user jim_m_58 described, but it's trial and error because I haven't tried this yet.

    here is what i do:

    function onBackgroundData(data) {
          System.println("Synchronize "+ data);
    	    var commStat;
    		var temp = data["main"]["temp"] as Dictionary;
    		
    		if(data instanceof Number) {
        		commStat=data;
        	} else {
            	commStat=200;
    			Application.Storage.setValue("Temp", temp as Numeric);
            }
            Application.Storage.setValue("OWMStatus", commStat);
    
            WatchUi.requestUpdate();
        }
     	

  • Move your line 4 after the else on line 8.  If "data" is a number (there was an error) the way it is now will crash.

  • Well, I did it this way, but I would also like information on how to make it so that when the temperature is zero during the call, the symbol "--" will be shown to me?


    new:

     

    function onBackgroundData(data) {
          System.println("Synchronize "+ data);
            var commStat;  
            if(data instanceof Number) {
                commStat=data;
            } else {
                commStat=200;
                var temp = data["main"]["temp"] as Dictionary;
                Application.Storage.setValue("Temp", temp as Numeric);
            }
            Application.Storage.setValue("OWMStatus", commStat);

            WatchUi.requestUpdate();
        }
       
  • 0 is a valid temperature in both C and F

  • Now I'm looking more and more at an example of what he did: FlowState is closer to me personally, so I'll probably decide to go in that direction. Now it works for me as I wanted.

    Hereby thank you for the great help.

    Jim_m_58 if possible also make an example of your code to help other users, not a bad way to call weather data.