Syntax for multiple lines in an if statement

Posted on
Thu Oct 06, 2022 6:06 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Syntax for multiple lines in an if statement

I am attempting to right a script that tests several loads: if all the devices are off, turn off a device(123); else, turn on the device(123). I think the logic is correct, as I am able to make it work if the IF statement is on one line, but there are too many devices for it all to fit on one line. My problem is that I am unable to find the right syntax for multiple lines for the IF statement.
I hope someone can tell me how to edit the following piece of code that always fails the if condition, regardless of one or more devices returning true.
Code: Select all
if not (devA.onState and \
 not devB.onState and \
 not devC.onState and \
 not devD.onstate):
 indigo.device.turnOff(123)
else:
 indigo.device.turnn(123)

Posted on
Thu Oct 06, 2022 7:56 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Syntax for multiple lines in an if statement

I think this is the logic you're shooting for:

Code: Select all
a = False
b = False
c = False
d = False

if (not a and not b and not c and not d):
    print("off")
else:
    print("on")
You can also do with "any":

Code: Select all
a = False
b = False
c = False
d = False

my_list = [a, b, c, d]
if not any(my_list):
    print("off")
else:
   print("on")
So:

Code: Select all
my_list = [devA.onState, devB.onState, devC.onState, devD.onState]
if not any(my_list):
    print("off")
else:
   print("on")

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

[My Plugins] - [My Forums]

Posted on
Thu Oct 06, 2022 8:40 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Syntax for multiple lines in an if statement

I like the list idea. Always something new and useful. Adding it to my repertoire.
But unless I change my device names to shorten them (my actual script uses characters with device names), I will need the syntax for enumerating a list that takes more than one line of code - same problem, I think.
(I'm using the script to manage a Lutron keypad LED that is associated with an all-off button: - if any of the devices are on, the LED is active; press the button, and the devices and the LED all are turned off. - that's the plan, but I can't make it work if it takes more than one line of code in the IF statement.)

Posted on
Fri Oct 07, 2022 5:12 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Syntax for multiple lines in an if statement

You can break a list across lines with no special characters:

Code: Select all
my_list = [devA.onState,
           devB.onState,
           devC.onState,
           devD.onState
           ]
if not any(my_list):
    print("off")
else:
   print("on")
There's (almost) always more than one way to skin a cat in Python.

Code: Select all
devA = indigo.devices[12345678]  # My device
devB = indigo.devices[23456789]  # Another device
devC = indigo.devices[09876543]  # Yet another device
devD = indigo.devices[98765432]  # THe last device

my_list = [devA.onState, devB.onState, devC.onState, devD.onState]
if not any(my_list):
    print("off")
else:
   print("on")
Common practice in Python is to favor readability over conciseness. You might want to look at something like Microsoft Visual Studio Code (VCS). It's a free integrated development environment that runs on macOS, Linux and Windows. It helps with syntax and formatting as you write.

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

[My Plugins] - [My Forums]

Posted on
Fri Oct 07, 2022 10:51 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Syntax for multiple lines in an if statement

Code: Select all
if not (devA.onState and \
 not devB.onState and \
 not devC.onState and \
 not devD.onstate):
 indigo.device.turnOff(123)
else:
 indigo.device.turnn(123)


Your first not being outside of the parenthesis is incorrect (the result would be something like if devA is off and (devB or devC or devD is on). But to answer your question, the PEP8 way (Python syntax formatting style guide) of formatting multiple if conditions across multiple lines is:

Code: Select all
if (
    not devA.onState and
    not devB.onState and
    not devC.onState and
    not devD.onstate
):
    indigo.device.turnOff(123)
else:
    indigo.device.turnOn(123)


Each condition is indented to the next level and lined up together. BTW, Python indentation standard is 4 spaces. While other numbers of spaces (or tabs) work, it's standard to use 4 spaces.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Oct 07, 2022 4:27 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Syntax for multiple lines in an if statement

Mixed success
This version did not work (failed to recognize onState of a device):
Code: Select all
if (
    not xFyrdPath.onState and
    not bRcvr.onState and
    not kStove.onState and
    not kTelLamp.onState and
    not fHallCh.onState and
    not fHallWW.onState and
    not lRmPianoLamp.onState
):
    indigo.device.turnOff(1279768341) # uHall kPd b6 OFF
else:
    indigo.device.turnOn(1279768341) # uHall kPd b6 ON


and the list version did work:
Code: Select all
list = [xFyrdPath.onState,
        bRcvr.onState,
        kStove.onState,
        kTelLamp.onState,
        fHallCh.onState,
        fHallWW.onState,
        lRmPianoLamp.onState
        ]
if not any(list):
    indigo.device.turnOff(1279768341) # uHall kPd b6 OFF
else:
    indigo.device.turnOn(1279768341) # uHall kPd b6 ON


My problem has been solved, but if you have a moment, I'm curious what is wrong with the first version?
Thank you both.
Bob

Posted on
Sat Oct 08, 2022 6:06 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Syntax for multiple lines in an if statement

Off the cuff, I don't see anything wrong with the syntax of the former block that generated the error. What was the exact error?

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

[My Plugins] - [My Forums]

Posted on
Sat Oct 08, 2022 8:25 am
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Syntax for multiple lines in an if statement

No error. With a control on, the test failed - I.e., it didn’t skip to ELSE….

Posted on
Sat Oct 08, 2022 8:48 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Syntax for multiple lines in an if statement

Obviously, I can't test it directly -- but a modified version runs as expected:

Code: Select all
xFyrdPath = False
bRcvr = False
kStove = False
kTelLamp = False
fHallCh = False
fHallWW = False
lRmPianoLamp = True

if (
    not xFyrdPath and
    not bRcvr and
    not kStove and
    not kTelLamp and
    not fHallCh and
    not fHallWW and
    not lRmPianoLamp
):
    print("indigo.device.turnOff(1279768341)") # uHall kPd b6 OFF
else:
    print("indigo.device.turnOn(1279768341)") # uHall kPd b6 ON

Code: Select all
>>>{indigo.device.turnOn(1279768341)
Not sure why it didn't run as expected.

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

[My Plugins] - [My Forums]

Posted on
Sat Oct 08, 2022 3:19 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Syntax for multiple lines in an if statement

Mystery solved:
Code: Select all
import time
time.sleep(1)
# if not (lRmPianoLamp.onState) and not (kStove.onState):
if (
    not xFyrdPath.onState and
    not bRcvr.onState and
    not kStove.onState and
    not kTelLamp.onState and
    not fHallCh.onState and
    not fHallWW.onState and
    not lRmPianoLamp.onState
):
    indigo.device.turnOff(1279768341) # uHall kPd b6 OFF
else:
    indigo.device.turnOn(1279768341) # uHall kPd b6 ON

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests

cron