Python conversion assist

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

Python conversion assist

I'd be very grateful for some python code assist for the issues below:

1) - I used the code below for AS date calculations. Is this necessary for date calculations with Python?
set dt to do shell script "/bin/date \"+%A, %b %d at %H:%M\""
set theHour to (do shell script "date +%H") --as integer
set theMinute to (do shell script "date +%M") --as integer

2) Boolean logic:
if VarA or VarB then
and
if VarA and VarB then

3) Below are lines to calculate time for populating a ad hoc time/date action (e.g., >60 min adds an hour, >24 hrs adds a day),. Maybe this isn't necessary for Python?
--calculate time to dim lights:
set dimMin to theMinute + onTym
set dimHour to theHour
if dimMin ≥ 60 then
set dimMin to dimMin - 60
set dimHour to dimHour + 1
if dimHour > 23 then
set dimHour to dimHour - 24
end if
end if

set dimTime to dimHour & ":" & dimMin & ":" & offSec as string
log dimTime using type "fYrdLts dimTime"

--calculate time to turn lights off:
set offMin to theMinute + onTym + dimTym
set offHour to theHour
if offMin ≥ 60 then
set offMin to offMin - 60
set offHour to offHour + 1
if offHour > 23 then
set offHour to offHour - 24
end if
end if
set offTime to offHour & ":" & offMin & ":" & offSec as string
log "offTime" & offTime using type "fYrdLts offTime"

--enable and populate schedule items


***But I would like to know how to create an ad hoc time/date action, replacing the AS code below:
set the time trigger type of time date action "fYrdLtsDIM" to absolute
set the date trigger type of time date action "fYrdLtsDIM" to everyDay
set the absolute trigger time of time date action "fYrdLtsDIM" to date dimTime

4) More Boolean logic:
if ("16:00:00" > offTime or offTime ≥ "22:00:00") then


Thank you in advance for any assistance.
Bob

Posted on
Mon Sep 16, 2019 6:38 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python conversion assist

SMUSEBY wrote:
I'd be very grateful for some python code assist for the issues below:

1) - I used the code below for AS date calculations. Is this necessary for date calculations with Python?
set dt to do shell script "/bin/date \"+%A, %b %d at %H:%M\""
set theHour to (do shell script "date +%H") --as integer
set theMinute to (do shell script "date +%M") --as integer


Probably not. You certainly don't need to run a shell script. I recommend you read this page, and concentrate on the time.strftime() function.

https://docs.python.org/2/library/time.html

Code: Select all
import time
now = time.time()
theHour = now.strftime("%H")
theMinute = now.strftime("%M")


SMUSEBY wrote:
2) Boolean logic:
if VarA or VarB then
and
if VarA and VarB then



Code: Select all
if varA or varB:
    # do something

if varA and varB:
    # do something

Take a look at the time functions I posted above, then post your #3 again after you've tried to convert it.

You might also look at https://docs.python.org/2/library/datetime.html

AS requires you to work with strings, break down the time components, manipulate them, them reconstruct the string. That's not how you do it in Python. In Python you get the time as either a float (using the time functions) or a datetime object (using datetime), manipulate that, and only construct a string at the end if you need it.

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

Posted on
Wed Sep 18, 2019 3:44 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python conversion assist

I am trying (with no success) to use the Indigo Open Scripting Shell Plugin to check the value of an Indigo variable and turn on a lamp. I am confused by how to get the variable into the Python world, how to convert it into a useful state (unicode???). I'm getting a variety of errors: unexpected indent, invalid syntax, name not defined, etc. I have tried to copy from the scripting tutorial, but it isn't helpful at my level.
Questions:
1) is it necessary to bring an indigo variable into Python (in AS, I did this with the 'set' command, e.g., set varA to the (value of variable "ambientLight") as integer)?
2) do I need to tell Python the nature of a particular variable, e.g. string, integer?
3) do I create the Python script in Text Editor and paste it as embedded code in Indigo, or, alternatively,
4) is there a Python script editor that I should be using? Searching, there are several. Is one recommended for Indigo purposes?
Basic stuff, but with a little help, perhaps I can move forward.

Could someone please show me the code to do the following: test an Indigo variable, "ambientLight" for an integer value;
1) if it needs to be imported into Python, set Python "VarA" to value of Indigo Variable "ambientLight"; and
2) if Python variable "VarA" (or indigo variable "ambientLight") is > 100, turn on a load, "lampA".

Please include any Python opening and closing lines, e.g., Tell... and endTell.
Thank you

Posted on
Wed Sep 18, 2019 4:11 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python conversion assist

SMUSEBY wrote:
Questions:
1) is it necessary to bring an indigo variable into Python (in AS, I did this with the 'set' command, e.g., set varA to the (value of variable "ambientLight") as integer)?


Yes

SMUSEBY wrote:
2) do I need to tell Python the nature of a particular variable, e.g. string, integer?


Generally not, as long as you retrieve it as the correct type. By default, Indigo variables are strings, so you need to specify that you want it as an integer.

SMUSEBY wrote:
3) do I create the Python script in Text Editor and paste it as embedded code in Indigo, or, alternatively,


Use a text editor, not but don't use TextEdit. It tries to do formatted text, which messed up the Python interpreter.

SMUSEBY wrote:
4) is there a Python script editor that I should be using? Searching, there are several. Is one recommended for Indigo purposes?
Basic stuff, but with a little help, perhaps I can move forward.


I recommend BBEdit. You can use the free version.

SMUSEBY wrote:
Could someone please show me the code to do the following: test an Indigo variable, "ambientLight" for an integer value;
1) if it needs to be imported into Python, set Python "VarA" to value of Indigo Variable "ambientLight"; and
2) if Python variable "VarA" (or indigo variable "ambientLight") is > 100, turn on a load, "lampA".




You do realize that for your example, scripting is not needed? That's all easy to do with Indigo UI directly.

Code: Select all
import indigo   # only needed for external scripts

varA = indigo.variables[717359973].getValue(int)     # use DeviceID of "ambientLight"
if varA > 100:
    indigo.device.turnOn(1234567890)                # use DeviceID of 'lampA'


SMUSEBY wrote:
Please include any Python opening and closing lines, e.g., Tell... and endTell.


Python doesn't use that stuff.

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

Posted on
Wed Sep 18, 2019 4:40 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python conversion assist

Thank you - that's very helpful, and timely.
I realize scripting isn't necessary for my example, but it is part of a more complex script. Believe me - if I don't need to use Python, I'm happy not to.
Regarding question #3, the text editor I should use for saved Python scripts is also BBEdit?
By the way, I'm tossing my Indigo keypads (and some switches) after most of the keypads failed, in favor of Lutron Radio Ra2. I believe you are responsible for the Lutron plugin - in which case, thanks for that too.
Bob

Posted on
Wed Sep 18, 2019 4:47 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python conversion assist

I like BBEdit for most all of my programming work. If you create a file with a .py extension, it knows it's Python and does some nice color coding and name completion.

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

Posted on
Wed Sep 18, 2019 5:09 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python conversion assist

Close, but...

Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)]
Connected to Indigo Server v7.3.1, api v2.1 (localhost:1176)
>>> varA = indigo.variables[1132180415].getValue(int) # use DeviceID of "ambientLight"
>>> if varA > 100:
... indigo.device.turnOn(1580115691) # use DeviceID of 'lampA'
...
...

I ran this in the scripting plugin. I can turn on the device with the second line, but the conditional is not working. The value of the variable is 1352.0, so guessing that the issue is that it isn't an integer? - if so, the alternative to 'int'?

Posted on
Wed Sep 18, 2019 5:18 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python conversion assist

It's best if you post code snippets like these by using the Code button in the editor window. Like this:

Code: Select all
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)]
Connected to Indigo Server v7.3.1, api v2.1 (localhost:1176)
>>> varA = indigo.variables[1132180415].getValue(int) # use DeviceID of "ambientLight"
>>> if varA > 100:
... indigo.device.turnOn(1580115691) # use DeviceID of 'lampA'
...
...

It helps us to spot issues with spacing and whatnot.

Code: Select all
varA = indigo.variables[717359973].getValue(int)     # use DeviceID of "ambientLight"
if varA > 100:
    indigo.device.turnOn(1234567890)                # use DeviceID of 'lampA'

See how the last line is indented (by 4 spaces)? That indentation is important.

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

[My Plugins] - [My Forums]

Posted on
Wed Sep 18, 2019 5:21 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python conversion assist

I found 'myFloat' associated with unicode, but 'int' is much simpler. Is there an equivalent?
Where can I find this stuff without bugging the forum?

Posted on
Wed Sep 18, 2019 5:33 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python conversion assist

Code button: that sounds like a very helpful hint. - except, where is the code button? I just installed BBEdit, and there is a "C" on the top, but some choices that don't look like what you are suggesting.
I copy/pasted the result that wasn't very helpful from the OpenScriptingShell plugin - and I'm guessing there is no coding button available there.

Posted on
Wed Sep 18, 2019 6:02 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python conversion assist

SMUSEBY wrote:
I found 'myFloat' associated with unicode, but 'int' is much simpler. Is there an equivalent?
Where can I find this stuff without bugging the forum?


Do an on-line Python tutorial. Like https://www.learnpython.org or maybe Code Academy. And then read the scripting guide in the Wiki, which explains how to deal with Indigo specific things.

If you want the variable value as a float, use .getValue(float) instead. But that's not your issue.

Dave was referring to the forum's post editor has a "Code" button. If you don't use that, we can't tell if you have your indentation correct. And Python is ALL about the indentation.

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

Posted on
Wed Sep 18, 2019 6:16 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python conversion assist

Runs!
Thanks.
Can you provide the URL for the forum's post editor?
Now - let's see how I can put this snippet to use.
Thanks again.

Posted on
Wed Sep 18, 2019 6:29 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: Python conversion assist

Beside 'int' and 'float', what do I use for 'boolean' and 'strings'?
The code you have suggested is much more concise than the examples on the indigo forum.
As for scripting tutorials, I tried two of them and found that I couldn't seem to get past the second lesson. Too many other things to do.
I learned apple scripting this way - by example - and hope that Python will come along quickly.

Posted on
Wed Sep 18, 2019 6:31 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python conversion assist

What do you mean URL? It's this button:
Attachments
Screen Shot 2019-09-18 at 8.29.53 PM.png
Screen Shot 2019-09-18 at 8.29.53 PM.png (266.05 KiB) Viewed 3190 times

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

Posted on
Wed Sep 18, 2019 6:33 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python conversion assist

SMUSEBY wrote:
Beside 'int' and 'float', what do I use for 'boolean' and 'strings'?.


It's all here: https://wiki.indigodomo.com/doku.php?id ... able_class

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

Who is online

Users browsing this forum: No registered users and 4 guests