let scene wait till function return valid data

I’m working on a scene that checks 2 device “groups” and based on the result acts.

The 2 devices group are defined in 2 arrays
1 generic function loops through these arrays to get the specific data from the devices and return an array with some data.
After harvesting all the data, the scene go’s into some logic to see what it need to do with the data.

Standalone all the pieces work very well, but when i combine all the pieces it doesn’t always work 100% as “designed”.

Now i have this;

gemiddeldeTempBuiten = sceneFunctions.avgTempValues(sceneVariables.tempSensorsBuiten)
gemiddeldeTempBinnen = sceneFunctions.avgTempValues(sceneVariables.tempSensorsBinnen)
-- gemiddeldeTempBinnen = returnArray = {["gemiddeld"]=value, ["optellen"]=value, ["sensors"]=value}

after that i do something like;

luup.log("vent "..gemiddeldeTempBuiten["gemiddeld"])
luup.log("vent "..gemiddeldeTempBinnen["gemiddeld"])

if(gemiddeldeTempBinnen["gemiddeld"]>25 and gemiddeldeTempBuiten["gemiddeld"]<25)then
sendemail("lets ventilate")
else
sendemail("no ventilation")
end

But it looks the sceneFunctions.avgTempValues() function not always response that quick and basic scene is already working ahead. This results in that is “gemiddeldeTempBinnen” is valid and “gemiddeldeTempBuiten” is not valid and the if condition generates an error. Also the luup.log generates an concat error of an empty/nil value.

When i run the scene several times, in most cases i get errors and in some cases (it looks like) the vera3 is idle enough to run the functions quickly enough to not generate errors.

Now my question is;
How could i force the scene to wait on a valid result of the 2 functions calls?

edit1;
i tried after each function line an luup.sleep(500), but this slow the scene to much down and i hope that there is a better solution.