Boolean logic question

Posted on
Wed Jun 15, 2022 4:09 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Boolean logic question

I want the script to test the condition of two devices: if both aren't on, turn them on; else, turn them off.
Testing just one device with
Code: Select all
if not (dev2.brightness > 10 ):
, it works, turning the devices on and off; when I test both devices, with
Code: Select all
# if not (dev1.onState and \
#    not dev2.brightness > 10 ):
, it never gets to the 'else' condition . What am I missing?
Code: Select all
# Device to be tested
dev1 = indigo.devices[35744300] # 1 k telLamp
dev2 = indigo.devices[1543856096] # 1 k whaleLamp

#test device condition
import time
time.sleep(1) #test device condition
if not (dev2.brightness > 10 ):

# if not (dev1.onState and \
#    not dev2.brightness > 10 ):

# if both lamps are not on, turn them both on
 indigo.actionGroup.execute(885970154) # k Lamps AG ON


else:  # if both devices are on, turn them off
 indigo.actionGroup.execute(4135325) # k Lamps AG OFF

Posted on
Wed Jun 15, 2022 4:15 pm
FlyingDiver offline
User avatar
Posts: 7216
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Boolean logic question

Your parenthesis are nested wrong. Put it all on one line and look again.

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

Posted on
Thu Jun 16, 2022 10:24 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Boolean logic question

Untested, but either of these would be my approach:

Code: Select all
# if both lamps are not on, turn them both on
if (not (dev1.onState) and dev2.brightness <= 10 ):
 indigo.actionGroup.execute(885970154) # k Lamps AG ON

else:  # if ANY device is on, turn them BOTH off
 indigo.actionGroup.execute(4135325) # k Lamps AG OFF
or

Code: Select all
# if both lamps are not on, turn them both on
if (dev1.onState == False and dev2.brightness <= 10 ):
 indigo.actionGroup.execute(885970154) # k Lamps AG ON

else:  # if ANY device is on, turn them BOTH off
 indigo.actionGroup.execute(4135325) # k Lamps AG OFF
I presume dev2 doesn't have an onState?

Having said that i'm not sure I can see why your current code doesn't work.

Posted on
Fri Jun 17, 2022 5:51 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Boolean logic question

It didn't work because it lacked a set of parentheses. This works:
Code: Select all
if not (dev1.onState) and \
   (not dev2.onState ):

But it isn't very neat.
I think your suggestion
Code: Select all
if (dev1.onState == False and dev2.onState == False ):
 indigo.actionGroup.execute(885970154) # k Lamps AG ON

is very nice. (I was using brightness under the mistaken assumption that there wasn't an onState for the z-wave device. There is.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests