Conditional logic with strange result

Posted on
Tue Nov 05, 2019 4:00 pm
SMUSEBY offline
Posts: 512
Joined: Sep 16, 2009
Location: California

Conditional logic with strange result

I have the following code snippet -
Code: Select all
   if lrC < lRmCld or upStrsC < upStrsCld:
      indigo.actionGroup.execute(1307638528)

which is giving a result that I would expect for 'and', not 'or'. The first condition is T, the second is F, and the actionGroup is not executed.
Do I have a syntax issue?

Posted on
Tue Nov 05, 2019 4:05 pm
FlyingDiver offline
User avatar
Posts: 7265
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Conditional logic with strange result

Not syntax, per se, but operations ordering. Use parenthesis around each comparison.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Nov 06, 2019 7:15 am
DaveL17 offline
User avatar
Posts: 6786
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Conditional logic with strange result

Also, be sure that you're comparing numeric values and not strings. Consider this:

Code: Select all
>>> print("99" > "98")
>>> print("99" > "100")
Will yield
Code: Select all
True
True

The reason why is that Python is comparing the numeric value of the strings. IIRC, Python only compares the first character of the string and (in the second example above):
Code: Select all
print(ord("9"))
print(ord("1"))

Will yield
Code: Select all
57
49
And obviously 57 is greater than 49. This can easily give unexpected results.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Nov 06, 2019 5:46 pm
SMUSEBY offline
Posts: 512
Joined: Sep 16, 2009
Location: California

Re: Conditional logic with strange result

Still not working. I suspect I got the parentheses wrong.
Code: Select all
   
if (lrC < lRmCld) or (upStrsC < upStrsCld):
      indigo.actionGroup.execute(1307638528) #htr 7, cntr, main water off

The second values are imported as integers; the first values come in as follows:
Code: Select all
dev = indigo.devices[883519697] #living room
lrC = int(dev.states["temperature"])

So maybe I'm comparing an integer with a string? However, all the other conditional tests (none of which involve an "OR") are working.

Posted on
Wed Nov 06, 2019 6:47 pm
jay (support) offline
Site Admin
User avatar
Posts: 18260
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Conditional logic with strange result

We only see how lrC is defined. Post the entire script.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Nov 06, 2019 7:03 pm
SMUSEBY offline
Posts: 512
Joined: Sep 16, 2009
Location: California

Re: Conditional logic with strange result

You asked for it...
Code: Select all
season = indigo.variables[325456157].value
hseOcc = indigo.variables[612296111].getValue(bool)
import time

# get the trigger values (trigXXX: abbreviation for FD low point trigger)
hSmpCld = indigo.variables[658575084].getValue(int) #htr sump COLD threshhold
hSmpOK = indigo.variables[1115411517].getValue(int) #htr sump OK

htWtrCld = indigo.variables[295376955].getValue(int) #htWtrTank COLD threshhold
htWtrOK = indigo.variables[154728201].getValue(int) #htWtrTank OK

lRmCld = indigo.variables[1086851778].getValue(int) #livingRm COLD threshhold
lRmOK = indigo.variables[711165647].getValue(int) #livingRm OK

stWtrCld = indigo.variables[25559805].getValue(int) #streetWater COLD threshhold
stWtrOK = indigo.variables[483412678].getValue(int) #streetWater OK

upStrsCld = indigo.variables[840732144].getValue(int) #upstairs COLD threshhold
upStrsOK = indigo.variables[63463463].getValue(int) #upstairs OK

wCabCld = indigo.variables[415421705].getValue(float) #wineCabinet COLD threshhold
wCabOK = indigo.variables[364969593].getValue(float) #wineCabinet OK

outsideCld = indigo.variables[680985657].getValue(int) #outside COLD threshhold
outsideOK = indigo.variables[78723147].getValue(int) #outside OK

crwlSpcCld = indigo.variables[427855415].getValue(int) #crawlSpace COLD threshhold
crwlSpcOK = indigo.variables[1756484536].getValue(int) #crawlSpace OK

statSetPt = indigo.variables[453519410].getValue(int) #stat set point (from var, not stat)

# get the current temperatures (xxxC: abbreviation for space temp)
dev = indigo.devices[647163782] #street water
stWtrC = int(dev.states["temperature"])

dev = indigo.devices[895358553] #wine cabinet
wineCabC = int(dev.states["temperature"])

dev = indigo.devices[883519697] #living room
lrC = int(dev.states["temperature"])

dev = indigo.devices[634066077] #upstairs
upStrsC = int(dev.states["temperature"])
 
dev = indigo.devices[1969337208] #outside
outC = int(dev.states["temperature"])

dev = indigo.devices[892742387] #hot water tank
htWtrC = int(dev.states["temperature"])

dev = indigo.devices[1386749935] #heater sump
sumpC = int(dev.states["temperature"])

dev = indigo.devices[46322275] #crawl space
crwlSpcC = int(dev.states["temperature"])

# verify that the variables have been imported properly
indigo.server.log(" season: {}".format(season))

indigo.server.log(" outsideCld: {}".format(outsideCld))
indigo.server.log(" outC: {}".format(outC))

indigo.server.log(" crwlSpcCld: {}".format(crwlSpcCld))
indigo.server.log(" crwlSpcC: {}".format(crwlSpcC))

indigo.server.log(" lRmCld: {}".format(lRmCld))
indigo.server.log(" lrC: {}".format(lrC))

indigo.server.log(" upStrsCld: {}".format(upStrsCld))
indigo.server.log(" upStrsC: {}".format(upStrsC))

indigo.server.log(" hSmpCld: {}".format(hSmpCld))
indigo.server.log(" sumpC: {}".format(sumpC))

indigo.server.log(" htWtrCld: {}".format(htWtrCld))
indigo.server.log(" htWtrC: {}".format(htWtrC))

indigo.server.log(" wCabCld: {}".format(wCabCld))
indigo.server.log(" wineCabC: {}".format(wineCabC))

indigo.server.log(" stWtrCld: {}".format(stWtrCld))
indigo.server.log(" stWtrC: {}".format(stWtrC))

# proceed with the if/then turn off heat for anything that is OK (> FD OK test) w AG
time.sleep(3) #allow time for temp variables to be updated
if stWtrC > stWtrOK:
   indigo.actionGroup.execute(358770212) #stWtr tape off
if wineCabC > wCabOK:
   indigo.actionGroup.execute(955423681) #wine can htr off
if sumpC > hSmpOK:
   indigo.actionGroup.execute(172000814) #htr sump tape off
if not hseOcc:
   if lrC > lRmOK or upStrsC > upStrsOK:
      indigo.actionGroup.execute(431011597) #htr 5C
   if htWtrC > htWtrOK:
      indigo.actionGroup.execute(460903989) #htWtr off
      
# next, proceed with the if/then turn on heat for anything that is cold (fails FD test) w AG
if season == "winter":
   if (lrC < lRmCld) or (upStrsC < upStrsCld):
      indigo.actionGroup.execute(1307638528) #htr 7, cntr, main water off
   if sumpC < hSmpCld:
      indigo.actionGroup.execute(1808251937) #htr sump tape on, cntr
   if htWtrC < htWtrCld:
      indigo.actionGroup.execute(599509902) #htWtr On, cntr
   if stWtrC < stWtrCld:
      indigo.actionGroup.execute(90585841) #stWtr tape on, cntr, water off
   if wineCabC < wCabCld:
      indigo.actionGroup.execute(1406401903) #wine can htr on, cntr

#last, send email notifications f(FD condition)
   time.sleep(3) #allow time for devices to switch on as necessary before reporting their status
   if (lrC < lRmCld or \
      upStrsC < upStrsCld or \
      crwlSpcC < crwlSpcCld or \
      outC < outsideCld or \
      sumpC < hSmpCld or \
      htWtrC < htWtrCld or \
      stWtrC < stWtrCld or \
      wineCabC < wCabCld ):
         indigo.actionGroup.execute(966172245) #if any cond fails, sends FD email

   elif (outC > outsideOK):
      indigo.actionGroup.execute(1538967727) #not cold email

   else:
      indigo.actionGroup.execute(963098824) #all temps ok email




Posted on
Wed Nov 06, 2019 7:13 pm
FlyingDiver offline
User avatar
Posts: 7265
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Conditional logic with strange result

What's the output in the log up until the test fails?

What's the reason for the time.sleep() call?

Also, there's no bonus points in Python for short variable names. You can make them as long as you want...

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Nov 06, 2019 7:35 pm
SMUSEBY offline
Posts: 512
Joined: Sep 16, 2009
Location: California

Re: Conditional logic with strange result

Events Log:
2019-11-06 06:00:00.790 Script season: winter
2019-11-06 06:00:00.790 Script outsideCld: 0
2019-11-06 06:00:00.790 Script outC: 0
2019-11-06 06:00:00.790 Script crwlSpcCld: 4
2019-11-06 06:00:00.790 Script crwlSpcC: 7
2019-11-06 06:00:00.790 Script lRmCld: 3
2019-11-06 06:00:00.791 Script lrC: 4
2019-11-06 06:00:00.791 Script upStrsCld: 5
2019-11-06 06:00:00.791 Script upStrsC: 6
2019-11-06 06:00:00.791 Script hSmpCld: 5
2019-11-06 06:00:00.791 Script sumpC: 10
2019-11-06 06:00:00.791 Script htWtrCld: 5
2019-11-06 06:00:00.791 Script htWtrC: 12
2019-11-06 06:00:00.791 Script wCabCld: 9.0
2019-11-06 06:00:00.791 Script wineCabC: 8
2019-11-06 06:00:00.791 Script stWtrCld: 4
2019-11-06 06:00:00.791 Script stWtrC: 6
2019-11-06 06:00:03.795 Action Group FD htrSump OK
2019-11-06 06:00:03.796 Action Group FD hotWtr OK
2019-11-06 06:00:03.797 Action Group FD wineCab COLD


and the email generated was:
Outside: -0.7

Crawl space: 7.3
Street water: 6.0
Main Water Valve (open=T): False
5 Drain Valves (open=T): False

HwTank: 12.7
HwTankPwr (on=T): False

lRm: 2.7
Upstairs: 6.0
Thermostat set to: 5
HtrSump: 10.7
HtrSumpTape (on=T): False

WineCab: 8.9
wine cabinet heater (on=T): True

Occupied: False

The email was created by FD Status Email (action group)


I checked independently, and the lrC (living room temp) was 2.7C.

Posted on
Wed Nov 06, 2019 7:41 pm
FlyingDiver offline
User avatar
Posts: 7265
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Conditional logic with strange result

This is the conditional you're having a problem with, right?
Code: Select all
if (lrC < lRmCld) or (upStrsC < upStrsCld):

And this data:
Code: Select all
2019-11-06 06:00:00.791   Script   lrC: 4
2019-11-06 06:00:00.790   Script   lRmCld: 3
2019-11-06 06:00:00.791   Script   upStrsC: 6
2019-11-06 06:00:00.791   Script   upStrsCld: 5


Both of those conditions fail, so the group should not execute.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Nov 06, 2019 10:51 pm
SMUSEBY offline
Posts: 512
Joined: Sep 16, 2009
Location: California

Re: Conditional logic with strange result

You have a point. I wonder where the value of lrC = 4, as the actual value is 2.7, which is < 3. I thought I checked everything, but I guess I'll do that again.
Thanks for pointing out the obvious.

Posted on
Wed Nov 06, 2019 11:09 pm
SMUSEBY offline
Posts: 512
Joined: Sep 16, 2009
Location: California

Re: Conditional logic with strange result

I still don't get it.
The value for lrC comes from the device.
Every time the device changes in value, a trigger assigns the value to a variable.
The email gets its value from the variable.
This morning, the 6am email reported the living room temp was 2.7, and that the thermostat had not been reset to 7 (per the script). I checked the device (2.7C) and the variable (2.7C).
I've confirmed the the device number matches the value in the script, that the variable value in the email also matches, and that the logged value matches the device number.
It seems to me that the source of the problem is a wrong ID for the variable and/or the device, or that the variable update trigger is failing. I have checked the device/variable IDs, and looked at the device and variable values (they matched this morning, and they match now).
I have no idea where the value of 4C is coming from (which is returning F in the script).
I'll check everything again tomorrow.
Mystery.

Posted on
Fri Nov 08, 2019 6:36 pm
SMUSEBY offline
Posts: 512
Joined: Sep 16, 2009
Location: California

Re: Conditional logic with strange result

I can't find the issue. Maybe fresh (competent) eyes can help.
The system is designed to keep the house cold, but to avoid freezing.
Every six hours, I run the python script previously discussed. I measure temperature at several locations with phidget temp sensors. Every time a temperature changes value, there is an associated trigger that takes the temperature and modifies the variable for that spot. The script gathers the cold threshold for each location, as well as the current temperature from the device.
If a location is cold, the associated heater is turned on. The last action in the script is to send an email with the temperature, threshold and device state (T/F).
For each location, there are two additional triggers - a cold one with a lower threshold that the one in the script (as a safety measure), and a temp OK one, which turns off the heater.
Everything is working, with the exception of the Living Room sensor and the house thermostat.
At 0600, the email reports the same temperature for the last 3 mornings (2.9C). The threshold for increasing the thermostat from 5C to 7C is 4C. Today, for the first time, the thermostat was set to 7C (as designed), but at 0609, it was returned to 5C.
I don't believe the temperature has ever gone below 5C - certainly not below 3C, based on values before and after 0600.
This morning's emails:
0600 #1, created by the script which runs every 6 hours:
Outside: -2.2

Crawl space: 7.8
Street water: 6.0
Main Water Valve (open=T): False
5 Drain Valves (open=T): False

HwTank: 12.0
HwTankPwr (on=T): False

lRm: 2.9
Upstairs: 5.5
Thermostat set to: 7
HtrSump: 19.1
HtrSumpTape (on=T): False

WineCab: 8.4
wine cabinet heater (on=T): True

Occupied: False

The email was created by FD Status Email (action group)



0600 #2 - when action is taken to warm up, the trigger sends an email. This time, the script actually did its job - why it did today, and not the previous two days is a mystery (or a clue?):
lR Temp: 2.9
Upstairs Temp: 5.5
Thermostat set to : 7
NB: the thermostat setting is a variable value, not necessarily the actual setting.


0609:
lR Temp: 6.0
Upstairs Temp: 5.5
Thermostat set to : 5
NB: the thermostat setting is a variable value, not necessarily the actual setting.

email sent by AG - "FD House Heater set to 5C email"


There is no way the temperature in the living room went from 2.9C to 6C in 9 minutes.
What seems to be happening is a 'rogue' temperature value is being assigned to the lRmTemp variable. However, every time I have compared the device with the variable, they always match. The backup up trigger for the living room is triggered when the lRmtemp is <2C. The script imports the lrTemp as an integer - and I'm guessing the Indigo trigger is testing against the device state, which I assume is floating.
To check for errors, I have copied the device / variable IDs, and searched for them in Indigo devices and variables. I have also searched for "lrC" in the python script, and haven't found any rogue instances. I have also looked for any variable value or phidgets device state which could be 2.9. The events log only shows the script running at 0600, and nothing that indicates a variable assignment. Is there a way to log values with a trigger? - I could add a script to do this.
What am I missing? There has to be a simple answer.

Posted on
Sat Nov 09, 2019 12:10 pm
jay (support) offline
Site Admin
User avatar
Posts: 18260
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Conditional logic with strange result

What is the exact value of:

Code: Select all
dev.states["temperature"]


when dev is this device:

Code: Select all
 dev = indigo.devices[883519697] #living room


In a script window, paste in the following then copy/paste the results:

Code: Select all
dev = indigo.devices[883519697] #living room
print(dev.states["temperature"])

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Nov 09, 2019 5:55 pm
SMUSEBY offline
Posts: 512
Joined: Sep 16, 2009
Location: California

Re: Conditional logic with strange result

Not working. I have done the following:
With BBEdit, I created a script that consists of the two lines suggested, and saved as a .py.
I created a trigger that runs the script.
Upon executing the script, the event log shows 'trigger' and the name of the trigger. - no values.
How have I managed to screw this up?

Posted on
Sat Nov 09, 2019 6:20 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Conditional logic with strange result

One of the menus in Indigo has an option to launch an indigo scripting console, or something like that.

(it’s basically terminal shell with python preloaded and Indigo imported)

Paste the commands in there.


Sent from my iPhone using Tapatalk Pro

Who is online

Users browsing this forum: No registered users and 2 guests