Homey Community Forum

Homeyscript retrieving json weatherdata with Continue and Async Function gives a perpetual loop

I am working on a script with the intention to retrieve JSON data with a api call for several api’s. The first intention I have is using Openweather and MeteoServer. I got it working with Openweather for the JSON file with the RETURN function. First the api call succeeds and after that all the data will be retrieved automatically. So far so good. But when I add Meteoserver, there must be made another api call. I made a async function. This works and retrieving data also works for the JSON file, but… The script doesn’t stop any more. I have to shut it, down to stop working. I know the RETURN option is not the best solution according to developers, but i couldn’'t come up with a better solution. Help would be appreciated

var Variable = 'temp'// Adding the first needed data tot start the script
    var Temperature = 'Yes'//Openweather temperature for the next 24 hours. Fill in 'Yes' or 'No' if you (don't) want Server temperature forecast for the next 24 hours in variables
    var FeelsLike = 'Yes'//Openweather feels like temperature for the next 24 hours.Fill in 'Yes' or 'No' if you (don't) want Server temperature forecast for the next 24 hours in variables
    var Humidity = 'Yes'//Openweather humidity for the next 24 hours.Fill in 'Yes' or 'No' if you (don't) want Server temperature forecast for the next 24 hours in variables
    var Clouds = 'Yes'//Openweather clouds for the next 24 hours. Fill in 'Yes' or 'No' if you (don't) want Server temperature forecast for the next 24 hours in variables
    var Visibility = 'Yes'//Openweather visibility for the next 24 hours.Fill in 'Yes' or 'No' if you (don't) want Server temperature forecast for the next 24 hours in variables
    var WindSpeed = 'Yes'//Openweather windspeed for the next 24 hours.Fill in 'Yes' or 'No' if you (don't) want Server temperature forecast for the next 24 hours in variables
    var WindInBeaufort = 'No'//Meteoserver wind in beaufort for the next 24 hours. Fill in 'Yes' or 'No' if you (don't) want MeteoServer windb forecast for the next 24 hours in variables
    var UvToday = 'No'//Openweather uv today for today.Fill in 'Yes' or 'No' if you (don't) want Server temperature forecast for the next 24 hours in variables
    var UvTomorrow = 'No'//Openweather uv tomorrow for tomorrow.Fill in 'Yes' or 'No' if you (don't) want Server temperature forecast for the next 24 hours in variables
    var Precipation = 'Yes'//Fill in 'Yes' or 'No' if you (don't) want Server precipation forecast for the next 24 hours in variables
   

    var MeteoServer = 'https://data.meteoserver.nl/api/uurverwachting.php?lat='+Latitude +'&long=' +Longitude +'&key=' + ApiMeteoServer
    var OpenWeatherServer = 'https://api.openweathermap.org/data/2.5/onecall?lat=' +Latitude +'&lon=' + Longitude +'&units=metric&lang=nl&exclude=daily,minutely,current&appid='+ ApiOpenWeatherServer
    var WeatherServer = OpenWeatherServer
    var UvTodayServer = 'http://api.openweathermap.org/data/2.5/uvi?lat=' +Latitude +'&lon=' +Longitude +'&appid=' +ApiOpenWeatherServer;
    var UvTomorrowServer = 'https://api.openweathermap.org/data/2.5/uvi/forecast?lat=' +Latitude +'&lon=' +Longitude +'&appid=' +ApiOpenWeatherServer;
    var HourlyOrData = 'hourly'

let BLApp = await Homey.apps.getApp({id:"net.i-dev.betterlogic" });//Activating better logic
    
var Today = new Date();//Actual Date
       console.log(Today);
    var Year = Today.getFullYear();// Actual Year in numbers
    var Month = Today.getMonth()+1;//Actual Month in numbers
    var Day = Today.getDate();//Actual day in a Month in numbers
      console.log('What day is it today: '+Day);//Just logging for control purposes
    var Hours = Today.getHours();//Actual hours in numbers
      console.log('What hour is it now: '+Hours);
    var GetDaysInMonth = function(month,year){//Calculates amount of days in this month
  return new Date(year, month, 0).getDate();};

  function wait(ms){//Declaring function wait
    var start = new Date().getTime();
    var end = start;
  while(end < start + ms) {
  end = new Date().getTime();}}

Weather11Hour = 0
Weather12Hour = 0
Weather13Hour = 0
Weather14Hour = 0
Weather15Hour = 0
Weather11Hour = 0
Weather12Hour = 0
Weather13Hour = 0
Weather14Hour = 0
Weather18Hour = 0


if(24 - Hours == 24){var OneHour = 0}else{var OneHour = 24 - Hours}
if(OneHour + 1 == 24){var TwoHour = 0}else{var TwoHour = OneHour +1}
if(TwoHour + 1 == 24){var ThreeHour = 0}else{var ThreeHour = TwoHour +1}
if(ThreeHour + 1 == 24){var FourHour = 0}else{var FourHour = ThreeHour +1}
if(FourHour + 1 == 24){var FiveHour = 0}else{var FiveHour = FourHour +1}
if(FiveHour + 1 == 24){var SixHour = 0}else{var SixHour = FiveHour +1}
if(SixHour + 1 == 24){var SevenHour = 0}else{var SevenHour = SixHour +1}
if(SevenHour + 1 == 24){var EightHour = 0}else{var EightHour = SevenHour +1}
if(EightHour + 1 == 24){var NineHour = 0}else{var NineHour = EightHour +1}
if(NineHour + 1 == 24){var TenHour = 0}else{var TenHour = NineHour +1}
if(TenHour + 1 == 24){var ElevenHour = 0}else{var ElevenHour = TenHour +1}
if(ElevenHour + 1 == 24){var TwelfHour = 0}else{var TwelfHour = ElevenHour +1}
if(TwelfHour + 1 == 24){var ThirteenHour = 0}else{var ThirteenHour = TwelfHour +1}
if(ThirteenHour + 1 == 24){var FourteenHour = 0}else{var FourteenHour = ThirteenHour +1}
if(FourteenHour + 1 == 24){var FifteenHour = 0}else{var FifteenHour = FourteenHour +1}
if(FifteenHour + 1 == 24){var SixteenHour = 0}else{var SixteenHour = FifteenHour +1}
if(SixteenHour + 1 == 24){var SeventeenHour = 0}else{var SeventeenHour = SixteenHour +1}
if(SeventeenHour + 1 == 24){var EighteenHour = 0}else{var EighteenHour = SeventeenHour +1}
if(EighteenHour + 1 == 24){var NineteenHour = 0}else{var NineteenHour = EighteenHour +1}
if(NineteenHour + 1 == 24){var TwentyHour = 0}else{var TwentyHour = NineteenHour +1}
if(TwentyHour + 1 == 24){var TwentyoneHour = 0}else{var TwentyoneHour = TwentyHour +1}
if(TwentyoneHour + 1 == 24){var TwentytwoHour = 0}else{var TwentytwoHour = TwentyoneHour +1}
if(TwentytwoHour + 1 == 24){var TwentythreeHour = 0}else{var TwentythreeHour = TwentytwoHour +1}
if(TwentythreeHour + 1 == 24){var TwentyfourHour = 0}else{var TwentyfourHour = TwentythreeHour +1}

console.log(OneHour + ' ' + TwoHour + ' ' + ThreeHour + ' ' + FourHour + ' ' + FiveHour + ' ' + SixHour + ' ' + SevenHour + ' ' + EightHour + ' ' + NineHour + ' ' + TenHour + ' ' + ElevenHour + ' ' + TwelfHour + ' ' + ThirteenHour + ' ' + FourteenHour + ' ' + FifteenHour + ' ' + SixteenHour + ' ' + SeventeenHour + ' ' + EighteenHour + ' ' + NineteenHour + ' ' + TwentyHour + ' ' + TwentyoneHour + ' ' + TwentytwoHour + ' ' + TwentythreeHour + ' ' + TwentyfourHour)



//https://api.openServermap.org/data/2.5/onecall?lat=53.0469354&lon=5.6675576&units=metric&lang=nl&exclude=daily,minutely,current&appid=1a260ce90356187232bdf683a397047c


async function ApiCallWeather(){

    WeatherServer = WeatherServer
    response = await fetch(WeatherServer);
    Server = await response.json();//Read response body and parse as JSON
    console.log(Server);



Start:
for (let i = 0; i <= 8; i++){

    var Weather01Hour = Server[HourlyOrData][OneHour][Variable];//Processing Weather 01:00 Hour 
    var Weather02Hour = Server[HourlyOrData][TwoHour][Variable];//Processing Weather 02:00 Hour 
    var Weather03Hour = Server[HourlyOrData][ThreeHour][Variable];//Processing Weather 03:00 Hour 
    var Weather04Hour = Server[HourlyOrData][FourHour][Variable];//Processing Weather 04:00 Hour 
    var Weather05Hour = Server[HourlyOrData][FiveHour][Variable];//Processing Weather 05:00 Hour 
    var Weather06Hour = Server[HourlyOrData][SixHour][Variable];//Processing Weather 06:00 Hour 
    var Weather07Hour = Server[HourlyOrData][SevenHour][Variable];//Processing Weather 07:00 Hour 
    var Weather08Hour = Server[HourlyOrData][EightHour][Variable];//Processing Weather 08:00 Hour 
    var Weather09Hour = Server[HourlyOrData][NineHour][Variable];//Processing Weather 09:00 Hour  
    var Weather10Hour = Server[HourlyOrData][TenHour][Variable];//Processing Weather 10:00 Hour 
    var Weather11Hour = Server[HourlyOrData][ElevenHour][Variable];//Processing Weather 11:00 Hour 
    var Weather12Hour = Server[HourlyOrData][TwelfHour][Variable];//Processing Weather 12:00 Hour 
    var Weather13Hour = Server[HourlyOrData][ThirteenHour][Variable];//Processing Weather 13:00 Hour 
    var Weather14Hour = Server[HourlyOrData][FourteenHour][Variable];//Processing Weather 14:00 Hour 
    var Weather15Hour = Server[HourlyOrData][FifteenHour][Variable];//Processing Weather 15:00 Hour 
    var Weather16Hour = Server[HourlyOrData][SixteenHour][Variable];//Processing Weather 16:00 Hour 
    var Weather17Hour = Server[HourlyOrData][SeventeenHour][Variable];//Processing Weather 17:00 Hour  
    var Weather18Hour = Server[HourlyOrData][EighteenHour][Variable];//Processing Weather 18:00 Hour 
    var Weather19Hour = Server[HourlyOrData][NineteenHour][Variable];//Processing Weather 19:00 Hour 
    var Weather20Hour = Server[HourlyOrData][TwentyHour][Variable];//Processing Weather 20:00 Hour 
    var Weather21Hour = Server[HourlyOrData][TwentyoneHour][Variable];//Processing Weather 21:00 Hour 
    var Weather22Hour = Server[HourlyOrData][TwentytwoHour][Variable];//Processing Weather 22:00 Hour 
    var Weather23Hour = Server[HourlyOrData][TwentythreeHour][Variable];//Processing Weather 23:00 Hour 
    var Weather24Hour = Server[HourlyOrData][TwentyfourHour][Variable];//Processing Weather 24:00 Hour 
    
      console.log('Weather01Hour is : '+Weather01Hour);
      console.log('Weather02Hour is : '+Weather02Hour);
      console.log('Weather03Hour is : '+Weather03Hour);
      console.log('Weather04Hour is : '+Weather04Hour);
      console.log('Weather05Hour is : '+Weather05Hour);
      console.log('Weather06Hour is : '+Weather06Hour);
      console.log('Weather07Hour is : '+Weather07Hour);
      console.log('Weather08Hour is : '+Weather08Hour);
      console.log('Weather09Hour is : '+Weather09Hour);
      console.log('Weather10Hour is : '+Weather10Hour);
      console.log('Weather11Hour is : '+Weather11Hour);
      console.log('Weather12Hour is : '+Weather12Hour);
      console.log('Weather13Hour is : '+Weather13Hour);
      console.log('Weather14Hour is : '+Weather14Hour);
      console.log('Weather15Hour is : '+Weather15Hour);
      console.log('Weather16Hour is : '+Weather16Hour);
      console.log('Weather17Hour is : '+Weather17Hour);
      console.log('Weather18Hour is : '+Weather18Hour);
      console.log('Weather19Hour is : '+Weather19Hour);
      console.log('Weather20Hour is : '+Weather20Hour);
      console.log('Weather21Hour is : '+Weather21Hour);
      console.log('Weather22Hour is : '+Weather22Hour);
      console.log('Weather23Hour is : '+Weather23Hour);
      console.log('Weather24Hour is : '+Weather24Hour);
       
  
  
  BLApp.apiPut(Variable +'01Hour/'+Weather01Hour);//Updating better logic variable Weather01Hour
  BLApp.apiPut(Variable +'02Hour/'+Weather02Hour);//Updating better logic variable Weather02Hour
  BLApp.apiPut(Variable +'03Hour/'+Weather03Hour);//Updating better logic variable Weather03Hour
  BLApp.apiPut(Variable +'04Hour/'+Weather04Hour);//Updating better logic variable Weather04Hour
  BLApp.apiPut(Variable +'05Hour/'+Weather05Hour);//Updating better logic variable Weather05Hour
  BLApp.apiPut(Variable +'06Hour/'+Weather06Hour);//Updating better logic variable Weather06Hour
  BLApp.apiPut(Variable +'07Hour/'+Weather07Hour);//Updating better logic variable Weather07Hour
  BLApp.apiPut(Variable +'08Hour/'+Weather08Hour);//Updating better logic variable Weather08Hour
  BLApp.apiPut(Variable +'09Hour/'+Weather09Hour);//Updating better logic variable Weather09Hour
  BLApp.apiPut(Variable +'10Hour/'+Weather10Hour);//Updating better logic variable Weather10Hour
  BLApp.apiPut(Variable +'11Hour/'+Weather11Hour);//Updating better logic variable Weather11Hour
  BLApp.apiPut(Variable +'12Hour/'+Weather12Hour);//Updating better logic variable Weather12Hour
  BLApp.apiPut(Variable +'13Hour/'+Weather13Hour);//Updating better logic variable Weather13Hour
  BLApp.apiPut(Variable +'14Hour/'+Weather14Hour);//Updating better logic variable Weather14Hour
  BLApp.apiPut(Variable +'15Hour/'+Weather15Hour);//Updating better logic variable Weather15Hour
  BLApp.apiPut(Variable +'16Hour/'+Weather16Hour);//Updating better logic variable Weather16Hour
  BLApp.apiPut(Variable +'17Hour/'+Weather17Hour);//Updating better logic variable Weather17Hour
  BLApp.apiPut(Variable +'18Hour/'+Weather18Hour);//Updating better logic variable Weather18Hour
  BLApp.apiPut(Variable +'19Hour/'+Weather19Hour);//Updating better logic variable Weather19Hour
  BLApp.apiPut(Variable +'20Hour/'+Weather20Hour);//Updating better logic variable Weather20Hour
  BLApp.apiPut(Variable +'21Hour/'+Weather21Hour);//Updating better logic variable Weather21Hour
  BLApp.apiPut(Variable +'22Hour/'+Weather22Hour);//Updating better logic variable Weather22Hour
  BLApp.apiPut(Variable +'23Hour/'+Weather23Hour);//Updating better logic variable Weather23Hour
  BLApp.apiPut(Variable +'24Hour/'+Weather24Hour);//Updating better logic variable Weather24Hour
  wait(2000);

  if (i === 1) {if(FeelsLike == 'Yes'){
    Variable = 'feels_like';//1st ApiCall Openweather, data call 2
    console.log('feels_like')
    continue Start;
  }
break;}

if (i == 2){if(Humidity == 'Yes') {
    Variable = 'humidity';//1st ApiCall Openweather, data call 3
    console.log('humidity')
    continue Start;
  }
break;}

if (i == 3){if(Visibility == 'Yes') {
    Variable = 'visibility';//1st ApiCall Openweather, data call 4
    console.log('visibility')
    continue Start;
  }
break;}

if (i == 4){if(WindSpeed == 'Yes') {
    Variable = 'wind_speed';//1st ApiCall Openweather, data call 5
    console.log('wind_speed')
    continue Start;
  }
break;}

if (i == 5){if(Clouds == 'Yes') {
    Variable = 'clouds';//1st ApiCall Openweather, data call 6
    console.log('clouds')
    continue Start;
  }
break;}


if (i == 6){if(WindInBeaufort == 'Yes' && Hours !== 1 && Hours !== 2 && Hours !== 3 && Hours !== 4 && Hours !== 5 && Hours !== 6 && Hours !== 7 ){
    WeatherServer = MeteoServer;//1st ApiCall Meteoserver, data call 7
    HourlyOrData = 'data'
    Variable = 'windb';
    console.log('windb')
    //ApiCallWeather();
  }
break;}


  Variable = 'pressure';//1st ApiCall Openweather, data call 1
    console.log('pressure');
 
}}

ApiCallWeather();

Just some pointers (because to be honest, the script is a bit spaghetti :stuck_out_tongue_winking_eye: ):

  • your wait function does a busy-wait, which is a no-no in Javascript. It needs to be an asynchronous wait:
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
…
await wait(2000); // wait 2000ms = 2s
  • the for loop is strange, it seems to be you can use either switch or else:
for (let i = 0; i <= 8; i++) {
  Variable = null;
  switch(i) {
    case 0:
      Variable = 'temp';
      break;
    case 1:
      if (FeelsLike === 'Yes') { // XXX: why not just a boolean?
        Variable = 'feels_like';
      }
      break;
    case 2:
      if (Humidity === 'Yes') {
        Variable = 'humidity';
      }
      break;
    case 3:
       … //etc etc
  }
  if (Variable === null) continue;
  var Weather01Hour = Server[HourlyOrData][OneHour][Variable];//Processing Weather 01:00 Hour 
  var Weather02Hour = Server[HourlyOrData][TwoHour][Variable];//Processing Weather 02:00 Hour 
  var Weather03Hour = Server[HourlyOrData][ThreeHour][Variable];//Processing Weather 03:00 Hour 
  var Weather04Hour = Server[HourlyOrData][FourHour][Variable];//Processing Weather 04:00 Hour 
  …
  BLApp.apiPut(Variable +'01Hour/'+Weather01Hour);//Updating better logic variable Weather01Hour
  BLApp.apiPut(Variable +'02Hour/'+Weather02Hour);//Updating better logic variable Weather02Hour
  BLApp.apiPut(Variable +'03Hour/'+Weather03Hour);//Updating better logic variable Weather03Hour
  BLApp.apiPut(Variable +'04Hour/'+Weather04Hour);//Updating better logic variable Weather04Hour
  …
}

To actually try to fix your problem, try adding an await before all those BLApp.apiPut() calls, because you’re now starting 24 API calls at once which may overwhelm Homey:

await BLApp.apiPut(Variable +'01Hour/'+Weather01Hour);//Updating better logic variable Weather01Hour
await BLApp.apiPut(Variable +'02Hour/'+Weather02Hour);//Updating better logic variable Weather02Hour
await BLApp.apiPut(Variable +'03Hour/'+Weather03Hour);//Updating better logic variable Weather03Hour

That way you can get rid of the wait entirely.

I absolute acknowledge it is spaghetti😊. But trying, trying. A lot is copy paste from the internet. So thx for your input.

But when i use switch, case does it only activate case 1,2 or three? What i tried to achieve with continue was first get the variable temp, then variable feels-like etc etc. That way i only use 1 api call and get all the variables which are inside the Json file.

Good tip :+1:for BLApp will use this also in my other scripts.

Obviously you have to implement all the others as well, I’m just giving an example. Ideally you’d use a lookup array there the index corresponds to the variable name, that would require basically a complete rewrite.

When i add await for BLApp. apiPut( after adding for about 4x await before BLApp.apiPutt, not with the first await) I get an Script error; An unknown error has occured [unknown_error]. When I delete AWAIT the script runs. Any idea?

Huh?

From the 24 BLApp.apiPut i have added 4 x await, this results in a error. Adding only 1 time await didn’t give an error

1 Like

I don’t understand why you would put 4 await’s in the code when you have 24 calls. If you add only one await, you don’t get an error but does the full script actually work?

I ment it this way

24x times await also gives the error. No await gives no error.

The script is working fine

In that case, remove the await’s and you’re done :stuck_out_tongue: