Python - getting a device state

Posted on
Sun Oct 06, 2019 10:03 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Python - getting a device state

The Phidgets devices have a device state, 'state' - see attachment.
The following code -
Code: Select all
season = indigo.variables[525524114].value
lrTemp = indigo.devices[883519697].state.getValue(int)
if season == "winter":
   if lrTemp < 7:
      indigo.actionGroup.execute(1011316617)
 

produces the error message, "hseHeaterON13.py, line 2, at top level
AttributeError: 'Device' object has no attribute 'state'.'

I get the same error substituting 'temperature' for 'state'. I've verified the device number.
What error am I making now?

Note: I'm attempting to use Python because Indigo's conditional logic is failing (giving true when it should be false). My guess is that Indigo is treating the integer temperature value as a string.
Attachments
2019-10-06_2056.png
2019-10-06_2056.png (186.03 KiB) Viewed 1945 times

Posted on
Sun Oct 06, 2019 10:52 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Python - getting a device state

this should work:
Code: Select all
season = indigo.variables[525524114].value
dev = indigo.device[883519697]
lrTemp = int(dev.state["temperature"])
if season == "winter":
   if lrTemp < 7:
      indigo.actionGroup.execute(1011316617)

Posted on
Mon Oct 07, 2019 7:33 am
matt (support) offline
Site Admin
User avatar
Posts: 21416
Joined: Jan 27, 2003
Location: Texas

Re: Python - getting a device state

Kar'ls has a typo – needs and 's' on the end of state:
Code: Select all
season = indigo.variables[525524114].value
dev = indigo.device[883519697]
lrTemp = int(dev.states["temperature"])
if season == "winter":
   if lrTemp < 7:
      indigo.actionGroup.execute(1011316617)

Looks like that device has two states "temperature" and "state." In your screenshot they both have the same value so I'm not sure how or if they are different, but the 3rd line above could also be:
Code: Select all
lrTemp = int(dev.states["state"])

Image

Posted on
Mon Oct 07, 2019 3:33 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python - getting a device state

Code: Select all
dev = indigo.device[883519697]


Error message: 'DeviceCmds' object does not support indexing
Exception Traceback (most recent call shown last):

test hseHeaterON13.py, line 6, at top level
TypeError: 'DeviceCmds' object does not support indexing

No idea how to approach this error message.

Posted on
Mon Oct 07, 2019 3:44 pm
CliveS offline
Posts: 770
Joined: Jan 10, 2016
Location: Medomsley, County Durham, UK

Re: Python - getting a device state

Replace
SMUSEBY wrote:
Code: Select all
dev = indigo.device[883519697]

with
Code: Select all
dev = indigo.devices[883519697]

(another missing 's')

CliveS

Indigo 2023.2.0 : macOS Ventura 13.6.3 : Mac Mini M2 : 8‑core CPU and 10‑core GPU : 8 GB : 256GB SSD
----------------------------------------------------------------------------------
The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer

Posted on
Mon Oct 07, 2019 5:18 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python - getting a device state

Code: Select all
season = indigo.variables[525524114].value
dev = indigo.devices[883519697]
lrTemp = int(dev.states["temperature"])
if season == "winter":
   if lrTemp > 7:
      indigo.device.turnOn(166867032) #counter light
   else:
      indigo.device.turnOff(1230957473) #doorlight


Adding the 's' fixed the error message. Now it simply doesn't do as intended.
Testing various formulations of the code, i have determined there are now 2 problems:
1) The season test passes 'false' when it is 'true',
2) the else syntax is not working

Posted on
Mon Oct 07, 2019 5:43 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Python - getting a device state

Code: Select all
season = indigo.variables[525524114].value
dev = indigo.devices[883519697]
lrTemp = int(dev.states["temperature"])

## add a logging statement to see what is happening:
indigo.server.log(" lrTemp: {}".format(lrTemp))
indigo.server.log(" season: {}".format(season))

if season == "winter":
   if lrTemp > 7:
      indigo.device.turnOn(166867032) #counter light
   else:
      indigo.device.turnOff(1230957473) #doorlight

Posted on
Mon Oct 07, 2019 6:34 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python - getting a device state

Brilliant - I was struggling the the log statement, and it did the trick.
Thank you!

Posted on
Mon Oct 07, 2019 8:02 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Python - getting a device state

some times you have a blank at the end in a variable
then use:

Code: Select all
indigo.server.log(" season:>{}<".format(season))

you can see if there are any spaces present eg >winter <

Posted on
Mon Oct 07, 2019 8:21 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Python - getting a device state

and to get rid of spaces and if you want to ignore upper / lower case:

Code: Select all
if season.strip().lower()== "winter":


Karl

Posted on
Mon Oct 07, 2019 8:45 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python - getting a device state

I meant that I was struggling with logging info to debug my little script, and suddenly, there was your snippet. Your snippet ran right out of the box.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests