After processing JSON data incorrect value in variable

I am trying to process some OpenWeatherMap data to display on an e-ink screen. For the forecast I am interested only in the data from 03:00 (night time) and 15:00 (day time) to display the expected temperature for both. I request (through the API) 24 rows of data from OpenWeatherMap to make sure I have 3 full days.
Once I have the 24 rows of data I parse them and select only the 03:00 and 15:00 entries for my forecast. This means that for the current day (after 03:00) I only have 1 entry which is at 15:00, then I have 2 days of both 03:00 and 15:00 entries. The strange thing is that when checking the value of the day time condition it shows the incorrect value.

Raw OpenWeatherMap data:

{"cod":"200","message":0,"cnt":24,"list":[{"dt":1642496400,"main":{"temp":-0.25,"feels_like":-3.77,"temp_min":-0.25,"temp_max":2.92,"pressure":1037,"sea_level":1037,"grnd_level":1036,"humidity":80,"temp_kf":-3.17},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":9},"wind":{"speed":2.97,"deg":193,"gust":4.04},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-01-18 09:00:00"},{"dt":1642507200,"main":{"temp":2.2,"feels_like":-1.08,"temp_min":2.2,"temp_max":7.11,"pressure":1037,"sea_level":1037,"grnd_level":1036,"humidity":76,"temp_kf":-4.91},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":8},"wind":{"speed":3.28,"deg":213,"gust":5.63},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-01-18 12:00:00"},{"dt":1642518000,"main":{"temp":4.42,"feels_like":2.2,"temp_min":4.42,"temp_max":6.76,"pressure":1036,"sea_level":1036,"grnd_level":1034,"humidity":79,"temp_kf":-2.34},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":6},"wind":{"speed":2.51,"deg":206,"gust":4.47},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-01-18 15:00:00"},{"dt":1642528800,"main":{"temp":3.74,"feels_like":1.1,"temp_min":3.74,"temp_max":3.74,"pressure":1035,"sea_level":1035,"grnd_level":1033,"humidity":89,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":32},"wind":{"speed":2.86,"deg":224,"gust":3.3},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-18 18:00:00"},{"dt":1642539600,"main":{"temp":3.8,"feels_like":1.08,"temp_min":3.8,"temp_max":3.8,"pressure":1033,"sea_level":1033,"grnd_level":1032,"humidity":93,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":100},"wind":{"speed":2.98,"deg":207,"gust":4.65},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-18 21:00:00"},{"dt":1642550400,"main":{"temp":3.04,"feels_like":-0.04,"temp_min":3.04,"temp_max":3.04,"pressure":1031,"sea_level":1031,"grnd_level":1030,"humidity":94,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":85},"wind":{"speed":3.25,"deg":225,"gust":7.08},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-19 00:00:00"},{"dt":1642561200,"main":{"temp":2.52,"feels_like":-1.06,"temp_min":2.52,"temp_max":2.52,"pressure":1029,"sea_level":1029,"grnd_level":1027,"humidity":94,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":28},"wind":{"speed":3.8,"deg":222,"gust":9.9},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-19 03:00:00"},{"dt":1642572000,"main":{"temp":3.01,"feels_like":-0.81,"temp_min":3.01,"temp_max":3.01,"pressure":1026,"sea_level":1026,"grnd_level":1025,"humidity":93,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":49},"wind":{"speed":4.38,"deg":223,"gust":10},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-19 06:00:00"},{"dt":1642582800,"main":{"temp":5.45,"feels_like":2.1,"temp_min":5.45,"temp_max":5.45,"pressure":1025,"sea_level":1025,"grnd_level":1024,"humidity":92,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":81},"wind":{"speed":4.59,"deg":251,"gust":11.74},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-01-19 09:00:00"},{"dt":1642593600,"main":{"temp":8.07,"feels_like":4.61,"temp_min":8.07,"temp_max":8.07,"pressure":1025,"sea_level":1025,"grnd_level":1024,"humidity":85,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":89},"wind":{"speed":6.53,"deg":292,"gust":12.19},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-01-19 12:00:00"},{"dt":1642604400,"main":{"temp":7.14,"feels_like":3.07,"temp_min":7.14,"temp_max":7.14,"pressure":1026,"sea_level":1026,"grnd_level":1024,"humidity":72,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":36},"wind":{"speed":7.6,"deg":316,"gust":12.36},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-01-19 15:00:00"},{"dt":1642615200,"main":{"temp":4.32,"feels_like":0.31,"temp_min":4.32,"temp_max":4.32,"pressure":1027,"sea_level":1027,"grnd_level":1026,"humidity":80,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":22},"wind":{"speed":5.38,"deg":309,"gust":13.21},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-19 18:00:00"},{"dt":1642626000,"main":{"temp":3.19,"feels_like":-0.7,"temp_min":3.19,"temp_max":3.19,"pressure":1029,"sea_level":1029,"grnd_level":1028,"humidity":88,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":18},"wind":{"speed":4.57,"deg":301,"gust":12.07},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-19 21:00:00"},{"dt":1642636800,"main":{"temp":2.52,"feels_like":-1.54,"temp_min":2.52,"temp_max":2.52,"pressure":1031,"sea_level":1031,"grnd_level":1030,"humidity":84,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":17},"wind":{"speed":4.58,"deg":312,"gust":12.56},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-20 00:00:00"},{"dt":1642647600,"main":{"temp":1.63,"feels_like":-2.49,"temp_min":1.63,"temp_max":1.63,"pressure":1033,"sea_level":1033,"grnd_level":1031,"humidity":86,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":5},"wind":{"speed":4.33,"deg":330,"gust":12.29},"visibility":10000,"pop":0.01,"sys":{"pod":"n"},"dt_txt":"2022-01-20 03:00:00"},{"dt":1642658400,"main":{"temp":0.99,"feels_like":-3.39,"temp_min":0.99,"temp_max":0.99,"pressure":1034,"sea_level":1034,"grnd_level":1032,"humidity":92,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":31},"wind":{"speed":4.5,"deg":331,"gust":11.23},"visibility":10000,"pop":0.01,"sys":{"pod":"n"},"dt_txt":"2022-01-20 06:00:00"},{"dt":1642669200,"main":{"temp":0.82,"feels_like":-4,"temp_min":0.82,"temp_max":0.82,"pressure":1036,"sea_level":1036,"grnd_level":1035,"humidity":84,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":16},"wind":{"speed":5.19,"deg":329,"gust":11.75},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2022-01-20 09:00:00"},{"dt":1642680000,"main":{"temp":2.99,"feels_like":-1.93,"temp_min":2.99,"temp_max":2.99,"pressure":1037,"sea_level":1037,"grnd_level":1036,"humidity":69,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":14},"wind":{"speed":6.65,"deg":333,"gust":10.27},"visibility":10000,"pop":0.01,"sys":{"pod":"d"},"dt_txt":"2022-01-20 12:00:00"},{"dt":1642690800,"main":{"temp":3.18,"feels_like":-1.44,"temp_min":3.18,"temp_max":3.18,"pressure":1037,"sea_level":1037,"grnd_level":1036,"humidity":70,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":60},"wind":{"speed":6.05,"deg":336,"gust":10.69},"visibility":10000,"pop":0.07,"sys":{"pod":"d"},"dt_txt":"2022-01-20 15:00:00"},{"dt":1642701600,"main":{"temp":0.9,"feels_like":-3.76,"temp_min":0.9,"temp_max":0.9,"pressure":1038,"sea_level":1038,"grnd_level":1037,"humidity":75,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":48},"wind":{"speed":4.94,"deg":329,"gust":11.57},"visibility":10000,"pop":0.07,"sys":{"pod":"n"},"dt_txt":"2022-01-20 18:00:00"},{"dt":1642712400,"main":{"temp":-0.34,"feels_like":-4.26,"temp_min":-0.34,"temp_max":-0.34,"pressure":1039,"sea_level":1039,"grnd_level":1037,"humidity":79,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":21},"wind":{"speed":3.41,"deg":325,"gust":10.28},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-20 21:00:00"},{"dt":1642723200,"main":{"temp":-0.81,"feels_like":-4.79,"temp_min":-0.81,"temp_max":-0.81,"pressure":1039,"sea_level":1039,"grnd_level":1038,"humidity":78,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":17},"wind":{"speed":3.36,"deg":308,"gust":9.63},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-21 00:00:00"},{"dt":1642734000,"main":{"temp":-0.8,"feels_like":-4.47,"temp_min":-0.8,"temp_max":-0.8,"pressure":1039,"sea_level":1039,"grnd_level":1038,"humidity":76,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":9},"wind":{"speed":3.01,"deg":298,"gust":6.69},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-21 03:00:00"},{"dt":1642744800,"main":{"temp":-0.7,"feels_like":-3.97,"temp_min":-0.7,"temp_max":-0.7,"pressure":1038,"sea_level":1038,"grnd_level":1037,"humidity":73,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":5},"wind":{"speed":2.61,"deg":289,"gust":4.33},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2022-01-21 06:00:00"}],"city":{"id":2653941,"name":"Cambridge","coord":{"lat":52.2,"lon":0.1167},"country":"GB","population":0,"timezone":0,"sunrise":1642492724,"sunset":1642522839}}

Code to process the deserialisedJSON:

    int forecast=0;
    int set_low=0;
    int condition[3];
    int temp_low[3];
    int temp_high[3];
    char icon_code[3][4];
    long time[3];

    /* Forecast returns weather data every 3 hours for the next 5 days, we only want the next 3 days for every 24 hours*/
    for(int i=0; i<=23; i++){
      String x=root["list"][char(i)]["dt_txt"];
      /* Search only for next 3 forecasts at 03:00 */
      if(x.substring(11,13) == "03")
      {
        temp_low[forecast]=root["list"][char(i)]["main"]["temp"];
        set_low=1;
      }
      /* Search only for next 3 forecasts at 15:00 */
      if(x.substring(11,13) == "15")
      {
        condition[forecast]=root["list"][char(i)]["weather"][0]["id"];
        temp_high[forecast]=root["list"][char(i)]["main"]["temp"];
        String temp=root["list"][char(i)]["weather"][0]["icon"];
        temp.toCharArray(icon_code[forecast],4);
        time[forecast]=root["list"][char(i)]["dt"];
        time[forecast]=time[forecast] + (time_zone*60*60);
        if(set_low == 0)
        {
          //If no 03:00 temperature set for 1st day
          temp_low[forecast]=-99;
        }
        forecast++;
      }
    }
  1. At first I check of the timestamp for the entry is 03 hours, if so populate the temp_low value
  2. Then I check if the timestamp for the entry is 15 hours, if so populate the temp_high value and set the weather condition variables.
  3. During the 15 hours check, also check if the 03 hours variable has been set (by checking set_low) and if not, set temp_low to -99 (so that later I can convert it to not display it)
  4. Increase the forecast counter, to move to the next day.

So when processing the data I don't have any entry for 03 hours, and keep going to 15 hours. Once that is found populate the relevant daytime fields. Then increase for the next day. This means for the current day there is only 1 entry I am interested in which is the 15 hours one.

But the strange thing is, all variables look correct except the condition one. From the provided example it should be 800. But when I read out the variable condition I get a 0, 1 or 2. This means the rest of the sketch will retrieve the incorrect weather icon for it.

The stranger thing is even that, for debugging purposes, if I then have a line with:

temp_low[forecast]=condition[forecast];
instead of the line near the bottom with:
temp_low[forecast]=-99;

I then see the correct value (800) on the display for the night temperature. This would indicate to me that the variable condition does contain the correct value, but when trying to read it directly I can't retrieve it.

When there are 3 full days (after 15:00 of the current day) all 3 forecast entries show the correct condition values for the relevant days.

As a workaround now I have used the temp_low field to ' duplicate' the condition field, and when it is time to retrieve the weather icon I have an extra if statement to check if temp_low is higher than 200 (which I hope the outside temperature would be) and if that is the case use temp_low instead of condition to get the correct icon.

Code to get the weather icon:

      //Weather icon
      const unsigned char *icon;
      if (temp_low[i] > 200) {
        icon = getIcon(temp_low[i], icon_code[i], true);
      } else {
        icon = getIcon(condition[i], icon_code[i], true);
      }

I am wondering if anybody can explain or help why the condition variable is not working for the current day when there is no temp_low value.

Why char(i) instead of just i?

That sounds like you're "retrieving" the variable wrongly. As you didn't post where you do that we cannot check it.

Post complete code and we can try to find the error.

I think your problem is that you have:

      "dt_txt": "2022-01-18 15:00:00"
      "dt_txt": "2022-01-19 03:00:00"
      "dt_txt": "2022-01-19 15:00:00"
      "dt_txt": "2022-01-20 03:00:00"
      "dt_txt": "2022-01-20 15:00:00"
      "dt_txt": "2022-01-21 03:00:00"

Each time you process a 15:00 entry you increment 'forecast'. That means 'forecast' is set to 3 when you process the last 03:00 entry and you write into: temp_low[3], which does not exist. Increase the 'temp_low' array to 4 elements to avoid writing off the end of your array.

Thank you for pointing that out. I was so focussed on condition variable I didn't look into the temp_low one. I did check the JSON data and saw I had 3 entries for it, but not thinking it would be of the 4th day.

I have now added a check that if forecast=4 I break out of the for loop.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.