Trying to convert...

Posted on
Wed Jul 15, 2020 2:28 am
Umtauscher offline
User avatar
Posts: 542
Joined: Oct 03, 2014
Location: Cologne, Germany

Trying to convert...

Hi Guys,

I am sorry but my inability to come to terms with Indigo-Python leaves me with an unconverted Applescript again.

If anyone is able to convert this, I would very much appreciate any help.

Code: Select all
# Indigo no longer supports execution of embedded AppleScripts. For help
# translating AppleScripts to python see: http://indigodomo.com/applescript
#
# The original AppleScript was:
#
# -- Set the value of a variable.
# -- If the variable doesn't exist, create it.
#
# tell application "IndigoServer"
#    if not (variable "AlarmScharf" exists) then
#       make new variable with properties {name:"AlarmScharf", value:false}
#    end if
#    do shell script "afplay /Users/wilhelm/Music/STARWAVE/c827.WAV"
#    if on state of device "Alarmsensoren ausgelöst" is true then
#       say "Alarmsensoren sind aktiv, der Alarm kann nicht scharfgeschaltet werden"
#    else
#       set value of variable "AlarmScharf" to true
#       say "Alarm ist scharf"
#    end if
# end tell
#
#


Thanks in advance
Wilhelm

Posted on
Wed Jul 15, 2020 7:11 am
neilk offline
Posts: 342
Joined: Jul 13, 2015
Location: Reading, UK

Re: Trying to convert...

Something like this (untested) and likely to have errors but to get you started.....

Code: Select all
import os
try:
    var = indigo.variables["AlarmScharf"]
except:
   indigo.variable.create("AlarmScharf", "false")
os.system("afplay /Users/wilhelm/Music/STARWAVE/c827.WAV")
if indigo.devices[u"Alarmsensoren ausgelöst"].onState == True:
   os.system('say "Alarmsensoren sind aktiv, der Alarm kann nicht scharfgeschaltet werden"')
else:
   indigo.variable.updateValue(AlarmScharf, "true")
   os.system('say "Alarm ist scharf"')



Better if you use the device/variable ID's, not sure of the logic over the variable creation but if once created it stays then you can use the variable ID's instead
Last edited by neilk on Thu Jul 16, 2020 9:27 am, edited 1 time in total.

Posted on
Wed Jul 15, 2020 8:55 am
Umtauscher offline
User avatar
Posts: 542
Joined: Oct 03, 2014
Location: Cologne, Germany

Re: Trying to convert...

Thanks a lot Neil, and it looks so simple.... ;-)

I'd rather stay with variable names like in any other programming language.

Thanks again
Wilhelm

Posted on
Wed Jul 15, 2020 9:36 am
jay (support) offline
Site Admin
User avatar
Posts: 16307
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Trying to convert...

Umtauscher wrote:
I'd rather stay with variable names like in any other programming language.


That would be a non sequitur. We're not talking about variables in the sense of a programming language, but rather Variables as an Indigo object type which you reference by ID or name. Referencing an object by an immutable attribute like it's id is much safer than referencing an object by a mutable attribute like a name.

This applies to all other object oriented programming languages as well.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jul 16, 2020 10:37 am
neilk offline
Posts: 342
Joined: Jul 13, 2015
Location: Reading, UK

Re: Trying to convert...

Umtauscher wrote:
Thanks a lot Neil, and it looks so simple.... ;-)


Thanks again
Wilhelm


Not a problem, I should probably have added some comments, and I corrected an error I spotted editing the original post ( adding the 'u' due to the accented characters in your device name to declare it as unicode). In my defence I am English and we expect everyone else to speak our language, even if our America cousins get a little confused with the spelling :wink: Let me know if you need any help as any errors should be easy to flush out.

Code: Select all
# the standard python 'os' module gives access to operating system capability, in this case I will use 'os.system' to execute the commands from your AppleScript
# this is really just fire and forget and assumes they run successfully, look into subprocess if you want greater control and feedback from the execution
# the import makes the 'os' module available to this script (probably an overly simplistic explanation, but it will do)
import os
# the python try/except block allows you to test some code for errors, and handle them if an error occurs, the indentation following the try and identifies the code either to be tried, or on error
try:
   # in this case I am trying to set the variable 'var' to the value of the indigo variable "AlarmScharf", if the variable does not exist it will cause an error
    var = indigo.variables["AlarmScharf"]
except:
   # which is then caught in the except block on error, and the line below creates the indigo variable and sets the value to false, indigo variables  are strings
   indigo.variable.create("AlarmScharf", "false")
# os.system simply executes the command
os.system("afplay /Users/wilhelm/Music/STARWAVE/c827.WAV")
# the if statement is pretty clear (and uses the same principle of indentation that was used in the try/except block)
# in the if statement you don't actually need the "== True", you can omit it as the device state is a boolean but it reads a little more clearly when new to python
# the 'u' is to identify the literal that follows is unicode, as your device name has accented characters
if indigo.devices[u"Alarmsensoren ausgelöst"].onState == True:
   os.system('say "Alarmsensoren sind aktiv, der Alarm kann nicht scharfgeschaltet werden"')
else:
   indigo.variable.updateValue(AlarmScharf, "true")
   os.system('say "Alarm ist scharf"')


I am still a Python novice and get some of the basics wrong (like forgetting unicode), but I did follow Jay's advice and did one of the online tutorials (the free one from Microsoft https://www.youtube.com/watch?v=jFCNu1-Xdsw) and conversations I previously didn't follow now make sense, and I even managed to knock out a plugin or two with the help of the Indigo hive (especially Jay, Joe and Dave)

Neil

Posted on
Fri Jul 17, 2020 3:17 am
Umtauscher offline
User avatar
Posts: 542
Joined: Oct 03, 2014
Location: Cologne, Germany

Re: Trying to convert...

Thanks!
Just another short question:

How would I set "virtual On/Off device state" via python?

I cannot seem to find any special documentation for virtual devices.

Cheers
Wilhelm

Posted on
Fri Jul 17, 2020 2:25 pm
jay (support) offline
Site Admin
User avatar
Posts: 16307
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Trying to convert...

If you're talking about the Virtual On/Off Device type in the Virtual Devices Interface, it depends on how you have it configured. If it's using a variable value for status, then you just set the Indigo variable value like you do any other Indigo variable. If you don't have it configured that way, you can execute an action group that has a Set Virtual On/Off Device State action in it.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Jul 17, 2020 2:56 pm
Umtauscher offline
User avatar
Posts: 542
Joined: Oct 03, 2014
Location: Cologne, Germany

Re: Trying to convert...

Hi Jay,

the backgroud of my question is the following:

I am using an action group to test some paramters and conditionally change a variable to switch the virtual device.
So starting this action group would change the state of the virtual device on certain conditions.
This works, when only using the device in Indigo.
But:
When I use the Homekit bridge, this is not working well, because the state in homekit is always changed - whether the virtual device changes or not. I found homekit only realizes the the switch doesn‘t occur, when I flip the state of the virtual device on and off again. Since the script decides the actual action, I have to do that inside the script. But I didn‘t find anything to set the virtual device state using python.

The virtual device has 2 action groups and a state variable.

Posted on
Fri Jul 17, 2020 4:54 pm
jay (support) offline
Site Admin
User avatar
Posts: 16307
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Trying to convert...

Yeah, so it's easy, just update the variable value and the device state will follow.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Jul 18, 2020 1:13 am
Umtauscher offline
User avatar
Posts: 542
Joined: Oct 03, 2014
Location: Cologne, Germany

Re: Trying to convert...

So you are basically saying, changing the virtual device state from a pyton script is not possible.

I cannot set the variable because there are triggers on that which I don't want to fire just to flip the device state for a split second...
Anyways, thanks, I probably have to redesign that constuction.
Cheers
Wilhelm

Posted on
Sun Jul 19, 2020 6:54 pm
jay (support) offline
Site Admin
User avatar
Posts: 16307
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Trying to convert...

Umtauscher wrote:
So you are basically saying, changing the virtual device state from a pyton script is not possible.


No, that's not what I said:

jay (support) wrote:
Yeah, so it's easy, just update the variable value and the device state will follow.


I said it's the easy way, not the only way. You can execute an action group which has a Set Virtual On/Off Device State action to turn it on and then another one that turns it off after a 1 second delay. Note that it could then get out of sync with the variable value (if the variable is already set to "on" then it won't be in sync after the action).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests