If script failing

Posted on
Fri Nov 15, 2019 6:31 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

If script failing

I am trying to create a toggle script that would test for the device state, permitting the operation of a device from a single (Lutron) keypad button.
I have added a simple action with if/then logic: - turn the light on if off, and vice versa.
Running the script, with the lamp off, results in a turnOff action - should be turnOn. - i.e., the if test is not working.
Code: Select all
lamp = indigo.devices[1580115691] # where the number is the ID of "office desk lamp"
if hasattr(lamp, 'onState'):
   isOn = lamp.onState
   indigo.device.turnOff(1580115691) #nOff lamp
else:
   indigo.device.turnOn(1580115691) #nOff lamp


# lamp = indigo.devices[1234567890] # where the number is the ID of "office desk lamp"
# if hasattr(lamp, 'onState'):
#    isOn = lamp.onState

Posted on
Fri Nov 15, 2019 7:14 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: If script failing

This should work for you.

Code: Select all
dev = indigo.devices[1580115691]

if dev.onState:
    indigo.device.turnOff(1580115691)
else:
    indigo.device.turnOn(1580115691)

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

[My Plugins] - [My Forums]

Posted on
Fri Nov 15, 2019 7:27 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: If script failing

That's a lot neater. Perhaps others would benefit if the website were updated.

Posted on
Fri Nov 15, 2019 8:04 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: If script failing

The one on the website is for something slightly different. The hasattr() bit is using Python introspection which is useful when you're not sure whether an object has a specific property. The script I suggested assumes that it has the onState property because it's a lamp device. According to the Python docs:

hasattr(object, name)
The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not.


That's all that bit does.

Of course, there are many ways to do things and if you want short, how about this? :D
Code: Select all
indigo.device.toggle(1580115691)

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

[My Plugins] - [My Forums]

Posted on
Sat Nov 16, 2019 11:33 am
jay (support) offline
Site Admin
User avatar
Posts: 18225
Joined: Mar 19, 2008
Location: Austin, Texas

Re: If script failing

Your modification of the script doesn't work because you're never actually testing to check the state, only to check if the device has an onState (that's what the original snippit was showing - so it's not actually wrong). This would be the most complete version that would handle both questions (does the device have an onState and if so is it on):

Code: Select all
# Get the device instance
lamp = indigo.devices[1580115691]
# Check to see if it has an on state
if hasattr(lamp, 'onState'):
    # It does have an on state, so check to see if it's on
    if lamp.onState:
       # It's on, so turn it off
       indigo.device.turnOff(1580115691)
    else:
       # It's off, so turn it on
       indigo.device.turnOn(1580115691)


If, however, you always want to just toggle the device and you know that it can be toggled (which is what your description suggests), then Dave's answer is the most straight forward.

Actually, you don't really need a script if that's all the script does - just use the UI toggle action.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Nov 16, 2019 12:54 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: If script failing

I now understand the website snippet - testing for the existence of an onState. However, can't I do that by taking a look at the device in Indigo? For a Python rookie, the snippet was not useful: I spent hours trying to make a toggle with it before posting here. The solution, create dev and 'if not dev.onState:' creates the toggle I wanted.
I understand that I can do most of what I'm doing without Python, but with multiple keypads doing the same thing, and two installations, it's easier to maintain the system by editing a common script. I have created a template script that checks for on state and does things, including managing devices, the keypad LEDs, variables, and timers. The template forces a disciplined approach to my use of Indigo. The result is a clean environment; previously, over the years, in response to issues, I had created triggers and action groups to do things, ad hoc, that often conflicted with triggers and action groups previously created, not to mention the Insteon keypads that regularly failed. It was a mess. Installing Lutron keypads and implementing Python is creating an environment that actually works and is easy to maintain.
I could not have done this without the patient, courteous, and very prompt assistance from you, Flying Diver, and others.
Thanks to all of you.

Posted on
Sat Nov 16, 2019 1:24 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: If script failing

I should have mentioned that using device IDs instead of names, avoids another risk of ad hoc confusion.

Posted on
Sun Nov 17, 2019 8:11 pm
jay (support) offline
Site Admin
User avatar
Posts: 18225
Joined: Mar 19, 2008
Location: Austin, Texas

Re: If script failing

SMUSEBY wrote:
For a Python rookie, the snippet was not useful


This, I'm sure, applies to many of the other snippets in the docs. It does not, however, make the snippit itself useless to everyone but rather only for your specific scenario. Snippets are there to show people how to do specific things - they may not be particularly logical/necessary in the form given, but creating simple scenarios is one of the best ways for people to learn.

I'm sorry that it threw you off - I suspect that doing a Python tutorial might give you a better foundation on which to build and would probably have made it clear that this particular example wasn't in fact what you were looking for.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Nov 17, 2019 10:35 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: If script failing

I'm sure you're right about the tutorial. I tried, and simply did not have the motivation to do what was necessary.
My work-around, as you well know, is to guess and check until I give up, post here, and get on with it. I have a sense that the snippets you have posted to the website are for the well-informed pythonistas, and probably very useful for those capable of creating plug-ins. For me, I need to do simple stuff - if/then, time, variable manipulation, and the quick and dirty is all I need. If I have an example that works, I can run with it; if it requires a sophisticated understanding of the language, I'm lost.
I've also noted that when I search on line for solutions, before posting here, that much of what is available on line doesn't work - perhaps because the indigo environment has it's own variation on Python? Before posting here, I work for hours on this stuff because the challenge is fun; the resources I use typically do not work, and I finally, tail between my legs, post questions here. Today, trying to use and manipulate a variable is typical: the snippet on the website didn't work, and neither did the code on the tutorial I use, not to mention all the variations I tried. It's hard, but when it's done, it is wonderful.
It's probably an offensive concept, but I'm collecting snippets that are now working, and I'm happy to offer the collection to you for posting.

Posted on
Mon Nov 18, 2019 9:56 am
jay (support) offline
Site Admin
User avatar
Posts: 18225
Joined: Mar 19, 2008
Location: Austin, Texas

Re: If script failing

SMUSEBY wrote:
I'm sure you're right about the tutorial. I tried, and simply did not have the motivation to do what was necessary.


Ok...

SMUSEBY wrote:
perhaps because the indigo environment has it's own variation on Python?


Nope, Indigo uses the standard Python 2.7 that's installed with macOS.

Now, Indigo presents the indigo module (a module is a Python library), which is what allows you to interact with Indigo. The indigo module isn't built-in to Python. Extending Python with specific modules is the standard way of integrating Python into other systems (similar to OSAX's for AppleScript). Our documentation focuses on using the indigo module and doesn't focus on teaching you Python. We even link to some Python tutorials at the top of the Scripting Tutorial wiki page.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Nov 18, 2019 4:13 pm
SMUSEBY offline
Posts: 511
Joined: Sep 16, 2009
Location: California

Re: If script failing

When attempting to write a script, the snippets in the scripting tutorials or what's available when searching for a particular need, what I have found usually does not work - for instance, manipulating a variable. And because the Indigo module is not in the scripting tutorials, and because I have been unable to make the snippets on the Indigo website work, I have had to ask for help.
The good news is I am almost done with my redo of my two Indigo environments.

Posted on
Mon Nov 18, 2019 5:01 pm
jay (support) offline
Site Admin
User avatar
Posts: 18225
Joined: Mar 19, 2008
Location: Austin, Texas

Re: If script failing

SMUSEBY wrote:
When attempting to write a script, the snippets in the scripting tutorials ... usually does not work - for instance, manipulating a variable.


Please point out the snippets for manipulating a variable that don't work and we will correct them - we want the documentation to be correct.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

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

Re: If script failing

I was using the following, but reading the heading, I see that it is not for setting the value, but rather for changing the python type.
myInteger = 1
indigo.variable.updateValue(1234567, value=unicode(myInteger))

- if only I had taken the tutorial...

What would help is a snippet to actually change the indigo variable -
indigo.variable.updateValue(scene_var, value=new_scene_number)

or more direct, if possible,
indigo.variable.updateValue(scene_var,value=xyz) - or "="xyz"" or "==xyz" or "=12345(int)"
So - snippets for changing the value of a variable to a 1) integer, 2) floating, 3) boolean, 4) string would be really helpful.
And since you sort of asked, snippets for conditional logic with =, >, <>, as well as OR, Else, elif - all the things you can do in your sleep, but are a struggle for novices.

Posted on
Mon Nov 18, 2019 7:11 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: If script failing

The Indigo Scripting Tutorial has examples of how to store values into variables, and how to pull their values into your script.

To save your data into a variable:
Code: Select all
myAsciiString = "ASCII String"
indigo.variable.updateValue(1234567, value=myAsciiString)

myUnicodeString = u"éçø"
indigo.variable.updateValue(1234567, value=myUnicodeString)

myInteger = 1
indigo.variable.updateValue(1234567, value=unicode(myInteger))

myFloat = 1.0
indigo.variable.updateValue(1234567, value=unicode(myFloat))

myList = ["one", 2, "three", 4]
indigo.variable.updateValue(1234567, value=unicode(myList))

myDictionary = {"first":1, "second":"two", "third":3, "fourth":"four"}
indigo.variable.updateValue(1234567, value=unicode(myDictionary))
You'll notice that every example stores the value as a unicode string. The important thing to remember is that all Indigo variables are stored as strings. There is no way to store them as any other type. So pulling data from variables requires conversion (if you aren't needing a string).

To retrieve a variable value into a format that you want, you can use the following:
Code: Select all
x = indigo.variables[12345678].getValue(int)
y = indigo.variables[12345678].getValue(float)
z = indigo.variables[12345678].getValue(bool)
To do a comparison, you could do this:
Code: Select all
if indigo.variables[12345678].getValue(int) > 10:
    # do something

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

[My Plugins] - [My Forums]

Posted on
Mon Nov 18, 2019 7:33 pm
jay (support) offline
Site Admin
User avatar
Posts: 18225
Joined: Mar 19, 2008
Location: Austin, Texas

Re: If script failing

SMUSEBY wrote:
So - snippets for changing the value of a variable to a 1) integer, 2) floating, 3) boolean, 4) string would be really helpful.


That's exactly what the snippet you posted does... :?:

SMUSEBY wrote:
And since you sort of asked, snippets for conditional logic with =, >, <>, as well as OR, Else, elif - all the things you can do in your sleep, but are a struggle for novices.


Conditional logic isn't Indigo specific - it's something that you would learn by doing a python tutorial. I can't recommend enough that you do a Python tutorial - almost everything you've asked about would have been solved by just learning the Python basics taught in any of the intro Python tutorials out there on the net.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Who is online

Users browsing this forum: No registered users and 10 guests