0

Can't fire if statement from IO switch
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Can't fire if statement from IO switch

by jefish on Sun Dec 16, 2018 7:27 pm

After querying the state of a switch on AdafruitIO, I can’t seem to get an if statement to fire based on the result.

Code:
Code: Select all | TOGGLE FULL SIZE
import time
from Adafruit_IO import Client, Feed, RequestError

ADAFRUIT_IO_USERNAME = ‘my_user’
ADAFRUIT_IO_KEY = ‘my_key’

aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

temperature_feed = aio.feeds('temperature')
humidity_feed = aio.feeds('humidity')
powerswitch_feed = aio.feeds('powerswitch')
powerlight_feed = aio.feeds('powerlight')

feeds = aio.feeds()
for f in feeds:
    print('Feed: {0}'.format(f.name))
   
while True:

    powerswitch = aio.receive('powerswitch')
    print(powerswitch.value)

    if powerswitch.value is "ON":
        print("Switch is ON")

    else:
        print("Switch is OFF")

    time.sleep(5)


This gives me the following result:
Code: Select all | TOGGLE FULL SIZE
pi@raspberrypi:~/Python $ python3 test.py
Feed: Temperature
Feed: Humidity
Feed: sig
Feed: powerswitch
Feed: powerlight
OFF            <-- switch is OFF in IO, and is printed correctly
Switch is OFF      <-- else is correctly fired
OFF            <-- switch is still OFF in IO
Switch is OFF      <-- else is again correctly fired
ON               <-- switch is turned ON in IO, and is printed correctly
Switch is OFF      <-- else is incorrectly fired
ON               <-- switch is still ON in IO
Switch is OFF      <-- else is incorrectly fired

I’ve tried:
if powerswitch is "ON":
if powerswitch.value is "ON":
if (powerswitch is "ON”):
if (powerswitch.value is "ON”):

None of the above make any change to the result at all. It’s so strange, because the return from the aio.receive is simply “ON” so it shouldn’t be tough to hit the if statement.

Helllllp?

Note that I've been Arduinoing off and on for a couple years, CircuitPythoning for a month, and Pythoning for about a week, so be gentle. ;)

jefish
 
Posts: 21
Joined: Fri Sep 12, 2014 2:08 am

Re: Can't fire if statement from IO switch

by franklin97355 on Sun Dec 16, 2018 8:37 pm

Try printing powerswitch.value and see what it thinks it is.

franklin97355
 
Posts: 19712
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Can't fire if statement from IO switch

by jefish on Sun Dec 16, 2018 11:26 pm

Yep, did that, it’s in the code above with the resulting output. Seems to work fine, but the if never gets hooked.

jefish
 
Posts: 21
Joined: Fri Sep 12, 2014 2:08 am

Re: Can't fire if statement from IO switch

by franklin97355 on Sun Dec 16, 2018 11:41 pm

OK, put this in your code and see what you get.
Code: Select all | TOGGLE FULL SIZE
if powerswitch.value is "ON":
        print("Switch is ON")
elif powerswitch.value is "OFF":
        print("Switch is OFF")
else:
        print("Switch is UNKNOWN")

franklin97355
 
Posts: 19712
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Can't fire if statement from IO switch

by jefish on Mon Dec 17, 2018 12:05 am

The following code:
Code: Select all | TOGGLE FULL SIZE
while True:

    powerswitch = aio.receive('powerswitch')
    print(powerswitch.value)

    if powerswitch.value is "ON":
        print("Switch is ON")
    elif powerswitch.value is "OFF":
        print("Switch is OFF")
    else:
        print("Switch is UNKNOWN")

Gives me the following result as I toggle the switch:
Code: Select all | TOGGLE FULL SIZE
pi@raspberrypi:~/Python $ python3 test.py
ON
Switch is UNKNOWN
ON
Switch is UNKNOWN
OFF
Switch is UNKNOWN
OFF
Switch is UNKNOWN

The value prints, but the code doesn't seem to consider ON to equal ON (or OFF to OFF).

jefish
 
Posts: 21
Joined: Fri Sep 12, 2014 2:08 am

Re: Can't fire if statement from IO switch

by jefish on Tue Dec 18, 2018 2:36 pm

After much fiddling and reading various examples last night, I realized that I had to make a simple fix...

change
if powerswitch.value is "ON":
to
if powerswitch.value == "ON":

Works great now.

How embarrassing. :/

jefish
 
Posts: 21
Joined: Fri Sep 12, 2014 2:08 am

Please be positive and constructive with your questions and comments.