View unanswered posts | View active topics It is currently Tue May 21, 2013 1:50 pm



Reply to topic  [ 41 posts ]  Go to page: 1, 2, 3  Next
 Help converting a simple applescript to Python 
Author Message
User avatar

Joined: Jun 14, 2006
Posts: 500
Post Help converting a simple applescript to Python
I've started having a go at using Python in Indigo. I've looked at the documentation in the wiki but I find it all very confusing. Anyway I can't seem to find the information I need to get the on state of a device and write it to a variable to use in an embedded script . Can you show me what this would be in Python and maybe the documentation will start to make more sense.

Code: Select all
--device is an applianceLinc
if on state of device "coffee" then
set value of variable "coffeeON_" to (current date) as string
end if


thanks


Thu Mar 22, 2012 1:18 pm
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6651
Location: Austin, Texas
Post Re: Help converting a simple applescript to Python
Under the Device Example section of the Indigo Scripting Tutorial:

Get the on state of a device if it has the onState property (uses Python's hasattr() introspection method):

Code: Select all
lamp = indigo.devices["office desk lamp"]
if hasattr(lamp, 'onState'):
   isOn = lamp.onState


Strictly speaking, since you're sure what you're getting is an ApplianceLinc, you don't need to test to see if it has the 'onState' property. And, you should really use the ID of the device rather than the name so that the script will continue to work even if you change the name.

And under the Variable Example section of the Indigo Scripting Tutorial:

Create a new Indigo variable named fooMonster, change its value multiple times, and delete it:

Code: Select all
newVar = indigo.variable.create("fooMonster", "default value")
indigo.variable.updateValue(newVar, "asleep")
indigo.variable.updateValue(newVar, "awake")
indigo.variable.delete(newVar)


You can use either a variable instance, as shown above, in the updateValue method, or you can use the variable ID (or name). Again, using the ID of the variable rather than the name is the more future-proof way to go.

Put the examples together with the relevant parts and:

Code: Select all
applianceLinc = indigo.devices[DEVICE_ID_HERE] # device ID of device "coffee"
if applianceLinc.onState:
    indigo.variable.updateValue(VAR_ID_HERE, str(isOn)) # variable ID of variable "coffeeON_"


You need to explicitly cast the value of isOn (which is a Python boolean) to a string - just like in AppleScript. You could have eliminated the first line and just done "if indigo.devices[DEVICE_ID_HERE].onState" but I think the former is more readable.

_________________
Jay (Indigo Support)
Image


Thu Mar 22, 2012 2:20 pm
Profile WWW
User avatar

Joined: Jun 14, 2006
Posts: 500
Post Re: Help converting a simple applescript to Python
Thanks Jay but could you explain your statement;

Strictly speaking, since you're sure what you're getting is an ApplianceLinc, you don't need to test to see if it has the 'onState' property


Also I tried running your example from the shell and had no luck or is the syntax different when running in the shell?

Code: Select all
lamp = indigo.devices["office desk lamp"]
if hasattr(lamp, 'onState'):
   isOn = lamp.onState



Thu Mar 22, 2012 2:29 pm
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6651
Location: Austin, Texas
Post Re: Help converting a simple applescript to Python
The example in the tutorial is checking the "lamp" device to see if it has the 'onState' property before it tries to get it and set it to the local variable isOn:

Code: Select all
 lamp = indigo.devices["office desk lamp"]
if hasattr(lamp, 'onState'):
    isOn = lamp.onState


You don't really have to do that as long as you know that the device you're getting has an onState - and an ApplianceLinc does.

I don't know what "had no luck" means - did you get an error? Post the code you tried to run - it should be this with the appropriate IDs substituted:

Code: Select all
 applianceLinc = indigo.devices[DEVICE_ID_HERE] # device ID of device "coffee"
if applianceLinc.onState:
    indigo.variable.updateValue(VAR_ID_HERE, str(isOn)) # variable ID of variable "coffeeON_"


Which is the last code I posted above.

_________________
Jay (Indigo Support)
Image


Thu Mar 22, 2012 2:34 pm
Profile WWW
User avatar

Joined: Jun 14, 2006
Posts: 500
Post Re: Help converting a simple applescript to Python
Jay,

If I enter this line to change the variable - in the console this is the result;

Code: Select all
>>> indigo.variable.updateValue(1487962293, str(isOn))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'isOn' is not defined


If I enter this in the console there is no error but the variable is not updated, this is the result;

Code: Select all
>>> applianceLinc = indigo.devices["coffee"] # device ID of device "coffee"
>>> if applianceLinc.onState:
...     indigo.variable.updateValue(1487962293, str(isOn))
...
>>>


Fri Mar 23, 2012 6:34 am
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6651
Location: Austin, Texas
Post Re: Help converting a simple applescript to Python
Doh - after simplifying the script I forgot to change what the variable is being set to. Try this (be sure to substitute the IDs):

Code: Select all
import datetime # this is the module that does all sorts of date/time manipulations
applianceLinc = indigo.devices[DEVICE_ID_HERE] # device ID of device "coffee"
if applianceLinc.onState:
    indigo.variable.updateValue(VAR_ID_HERE, str(datetime.datetime.now())) # variable ID of variable "coffeeON_"

_________________
Jay (Indigo Support)
Image


Fri Mar 23, 2012 9:21 am
Profile WWW
User avatar

Joined: Jun 14, 2006
Posts: 500
Post Re: Help converting a simple applescript to Python
thanks Jay, I've give it a try.

Here is what I am using to learn Python. Pretty basic but good for Python newbies like myself;

http://www.sthurlow.com/python/lesson01/


Fri Mar 23, 2012 10:07 am
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6651
Location: Austin, Texas
Post Re: Help converting a simple applescript to Python
I personally like Dive Into Python;)

_________________
Jay (Indigo Support)
Image


Fri Mar 23, 2012 11:11 am
Profile WWW
User avatar

Joined: Jun 14, 2006
Posts: 500
Post Re: Help converting a simple applescript to Python
Jay,

This code works in the shell but as an embedded script I get error "embedded script: invalid syntax".What am I doing wrong?

Code: Select all
#set coffee timestamp

import datetime
applianceLinc = indigo.devices["coffee"]
if applianceLinc.onState:
    indigo.variable.updateValue(862715057, str(datetime.datetime.now()))


Mon Mar 26, 2012 6:37 am
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6651
Location: Austin, Texas
Post Re: Help converting a simple applescript to Python
No idea - I used the "Select All" above your code, copy/pasted it in and hit the "Compile" button and it compiled correctly. I changed the device name to one of mine and changed the variable ID to one of mine and ran it with no problem. You sure that's exactly what you have in the embedded script?

_________________
Jay (Indigo Support)
Image


Mon Mar 26, 2012 9:47 am
Profile WWW
User avatar

Joined: Jun 14, 2006
Posts: 500
Post Re: Help converting a simple applescript to Python
Jay,

I cut and pasted it direct from the Indigo EMBEDDED SCRIPT window :( but I get that error in red at the bottom of the window when I try to compile it.


Mon Mar 26, 2012 11:50 am
Profile
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6651
Location: Austin, Texas
Post Re: Help converting a simple applescript to Python
Delete the action (not the whole trigger/schedule, just the action) and recreate it from what's pasted in here. If it still gives you the error, then tell me what's highlighted in the window when you see the error.

_________________
Jay (Indigo Support)
Image


Mon Mar 26, 2012 4:39 pm
Profile WWW
User avatar

Joined: Jun 14, 2006
Posts: 500
Post Re: Help converting a simple applescript to Python
Jay,

I get a an "embedded script: invalid syntax" warning for the following line;

Code: Select all
applianceLinc = indigo.devices["coffee"]


Tue Mar 27, 2012 6:18 am
Profile
Site Admin
User avatar

Joined: Jan 27, 2003
Posts: 11689
Location: Texas
Post Re: Help converting a simple applescript to Python
If you change the script to just that single line does it compile? Remove any whitespace lines before or after the line as well.

And copy/paste what you see in the Event Log after you hit compile for me.

_________________
Image


Tue Mar 27, 2012 7:07 am
Profile WWW
User avatar

Joined: Jun 14, 2006
Posts: 500
Post Re: Help converting a simple applescript to Python
Matt,

I get a an "embedded script: invalid syntax" warning and the line is highlighted, for the following code;

Code: Select all
applianceLinc = indigo.devices["coffee"]


The code above on it's own compiles OK with no log warnings.

When compiling the complete script although it highlights the above line when it shows the error, in the log it shows the last line as causing the problem;

Script Error embedded script: invalid syntax
Script Error around line 4 - "indigo.variable.updateValue(862715057, str(time.strftime( "%a %b %d %H:%M:%S", time.localtime( time.time() ) )))"


This script runs OK in the shell.


Tue Mar 27, 2012 8:35 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 41 posts ]  Go to page: 1, 2, 3  Next

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.   Template designed by STSoftware.