Weird possible race condition problem with variables

Posted on
Thu Aug 31, 2017 12:25 pm
jmdraper offline
Posts: 103
Joined: Sep 11, 2014
Location: Surrey, UK

Weird possible race condition problem with variables

I have an irrigation routine which I want to suppress based on actual and forecast rainfall. I don't yet have my own weather station so I am using data from Weather Underground using the Weather Underground plugin. On a daily schedule, I first execute the script below and then start a timer which kicks off the irrigation schedule. The other triggers that control the actual irrigation devices then respect the 'suppression' Indigo variable that is set at the end of this script. The script takes the actual rainfall in the past 24 hours and then adds up the hourly rainfall in each of the next 24 forecast hours and compares them to a threshold before setting the suppression variable appropriately.

The weird problem I am having is that sometimes when this script runs, the suppression variable doesn't get set to true even though the rain amounts clearly exceed the threshold then when I immediately manually run the script a second time, it get set correctly (nothing else has changed in the meantime). The only thing I can think of is that there is some ordering problem here with the way the Python script is reading and updating Indigo variables. But I am not at all sure. Can anyone else see what I am doing wrong?

Thanks,
Jon

Code: Select all
# SET UP VARIABLES

rainTodayVar = indigo.variables[298150352] # "rainToday"
rainTomorrowVar = indigo.variables[375236753] # "rainTomorrow"
suppressVar = indigo.variables[88571208] # "suppressZ1irrigation"
rainThreshold = indigo.variables[1822895225].value # "rainThreshold"

# UPDATE RAIN TODAY FROM WEATHER UNDERGROUND

rainToday = indigo.devices[417831836].states["precip_today"] # State "precip_today" of "Home Weather WUnderground"
indigo.variable.updateValue(rainTodayVar,unicode(rainToday))

# ADD UP RAIN TOMORROW FROM WEATHER UNDERGROUND

totalRainTomorrow = 0

for i in range(24):
   if i < 9:
      hourQpf = "h0" + str(i+1) + "_qpf"
   else:
      hourQpf = "h" + str(i+1) + "_qpf"
   
   totalRainTomorrow += indigo.devices[1542636177].states[hourQpf] # State "hXX_qpf" of "Hourly forecast WUnderground"
   
indigo.variable.updateValue(rainTomorrowVar,unicode(totalRainTomorrow))

# SET SUPPRESS VAR TO TRUE IF EITHER RAIN TODAY OR RAIN TOMORROW > THRESHOLD, OTHERWISE FALSE

if rainTodayVar.value > rainThreshold or rainTomorrowVar.value > rainThreshold:
   indigo.variable.updateValue(suppressVar,"true")
else:
   indigo.variable.updateValue(suppressVar,"false")

Posted on
Thu Aug 31, 2017 2:33 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Weird possible race condition problem with variables

You update rainTomorrowVar, but then you use the old value in the if condition at the end. Remember that when you get an Indigo device/variable, you're getting a copy of it not a "live" version. So when you call updateValue, it tells the server to update it, but your local copy is still the old value. For instance:

Code: Select all
rainTomorrowVar = indigo.variables[375236753] # "rainTomorrow"
# Let's say that rainTomorrowVar.value == 2
indigo.variable.updateValue(rainTomorrowVar,unicode(10))
# you update the variable on the server to be 10
# however, rainTomorrowVar.value is still == 2 because it's a copy made before you updated
print rainTomorrowVar.value
# would print 2
rainTomorrowVar.refreshFromServer()
print rainTomorrowVar.value
# would print 10


However, you don't really need to do that since you already know what the value is - just use totalRainTomorrow since you calculated it earlier.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Sep 01, 2017 3:54 am
jmdraper offline
Posts: 103
Joined: Sep 11, 2014
Location: Surrey, UK

Re: Weird possible race condition problem with variables

Perfect - thanks. I hadn't realised I was getting a value rather than a pointer to a variable. That explains it.

Cheers,
Jon

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests

cron