Custom States Cause Error

Posted on
Mon Nov 07, 2022 10:13 am
gskarp offline
Posts: 143
Joined: Apr 19, 2012

Custom States Cause Error

I have been trying to determine if. a custom state exists in a GhostXML device (real type). If there is rain there will be a Rain state. If there is no rain that state will not exist. I thought I could use hasattr, but it always results in the "else" instructions even if the custom state is there. Any thoughts about how to determine if a custom state exists for a GhostXML device?

Code: Select all
dev = indigo.devices[123456789]  # GhostXML device
state_1 = dev.states['list_0_rain_3h']
Attr_1 = 'list_0_rain_3h'

if hasattr(dev, Attr_1):
   indigo.server.log("state = %s" % (dev. list_0_rain_3h))
else:
   state_1 = 0
   indigo.server.log("hassattr1 = attr not found")

Posted on
Mon Nov 07, 2022 10:24 am
FlyingDiver offline
User avatar
Posts: 7220
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Custom States Cause Error

Code: Select all
state_1 = dev.states.get('list_0_rain_3h', None)
if state_1:
    # yes the state is there

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

Posted on
Mon Nov 07, 2022 10:36 am
gskarp offline
Posts: 143
Joined: Apr 19, 2012

Re: Custom States Cause Error

Like a charm. Thank you!

Posted on
Mon Nov 07, 2022 11:23 am
DaveL17 offline
User avatar
Posts: 6755
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Custom States Cause Error

Yes, that's the "Ghost" part in action. Only data provided by the most recent API call will have states created. Joe's recommendation is (of course) the best way to handle this situation. I might add that if you want to presume that the lack of the state's existence means that there is zero rain predicted, you can modify his code thusly:

Code: Select all
state_1 = dev.states.get('list_0_rain_3h', 0)

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

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 6 guests

cron