help request - debug python script

Posted on
Wed Oct 02, 2019 10:21 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

help request - debug python script

I'm trying to manage a variable, hseOcc as follows: if true, make it false, else make it true.
My attempt:

Code: Select all
hseOcc = indigo.variables[612296111].getValue(bool)     #
if hseOcc = true: # I also tried "true", and 1
    indigo.variables.updateValue[612296111] = false #
   else
    indigo.variable.updateValue(612296111) = true #

gives me an invalid syntax error.
Eliminating all but the first line also produces a syntax error.

Using the Extending Indigo documents, I tried:

Code: Select all
hseOcc = indigo.variable[612296111]
if hseOcc == "true":
    indigo.variable.updateValue[612296111], "false" #
else:
    indigo.variable.updateValue(612296111), "true" #

The log error for this is"embedded script, line 1, at top level
TypeError: 'VariableCmds' object does not support indexing.

Suggestions?
BTW, I know I could do this with triggers, but I'm still determined to make something work with Python. This seems like an easy problem and learning opportunity for me.
Thanks,
Bob

Posted on
Wed Oct 02, 2019 10:50 pm
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: help request - debug python script

Bool values in python are True and False. Upper case .. not true false


Sent from my iPhone using Tapatalk

Posted on
Wed Oct 02, 2019 11:28 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: help request - debug python script

modified to:

Code: Select all
hseOcc = indigo.variable[612296111]
if hseOcc == "True":
    indigo.variable.updateValue[612296111], "False" #
else:
    indigo.variable.updateValue(612296111), "False" #

It is crashing of the first line, with the message, "embedded script: 'VariableCmds' object does not support indexing. Not sure what that means, but I tried the alpha name for the variable, and got the same error.

Posted on
Wed Oct 02, 2019 11:30 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: help request - debug python script

Correction: using the alpha name of the variable, the error is "embedded script: global name 'hseOccupied' is not defined"
Sorry.

Posted on
Thu Oct 03, 2019 12:34 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: help request - debug python script

Try this:
Code: Select all
hseOcc = indigo.variables[612296111].getValue(bool) #
if hseOcc:
    indigo.variable.updateValue(612296111, value="False")
else:
    indigo.variable.updateValue(612296111, value='True')
The if hseOcc: line can be written as if hseOcc is True:.

Using device name instead of device id, the code would look like this:
Code: Select all
hseOcc = indigo.variables["hseOccupied"].getValue(bool) #
if hseOcc:
    indigo.variable.updateValue("hseOccupied", value="False")
else:
    indigo.variable.updateValue("hseOccupied", value='True')
Hope this helps. :)

EDIT: Added device name example code above.

Posted on
Thu Oct 03, 2019 11:48 am
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: help request - debug python script

I changed the if line as suggested, but the script crashes on the first line:

hseOcc = indigo.variable[hseOccupied]

results in the error message, "embedded script: global name 'hseOccupied' is not defined". How do I fix this?

Posted on
Thu Oct 03, 2019 12:00 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: help request - debug python script

SMUSEBY wrote:
I changed the if line as suggested, but the script crashes on the first line:

hseOcc = indigo.variable[hseOccupied]

results in the error message, "embedded script: global name 'hseOccupied' is not defined". How do I fix this?


What happened to the quotes around the name hseOccupied in that line? It was in the code autolog posted. Also, you dropped the 's'. It should be:

Code: Select all
hseOcc = indigo.variables["hseOccupied"]


And, yet again, USE THE CODE TAG when posting code snippets.
Attachments
Screen Shot 2019-10-03 at 2.00.59 PM.png
Screen Shot 2019-10-03 at 2.00.59 PM.png (18.59 KiB) Viewed 2886 times

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

Posted on
Thu Oct 03, 2019 12:30 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: help request - debug python script

I'm very sorry - I'm asking for help, and clearly violating protocol. Doing my best, and trying to learn.
Is this what you want?

Code: Select all
hseOcc = indigo.variable[hseOccupied]
if hseOcc is True:
    indigo.variable.updateValue[612296111], "False" #
else:
    indigo.variable.updateValue(612296111), "False" #


To repeat my question, the code crashes on the first line - 'global name 'hseOccupied' is not defined. If I replace the variable name with the ID, the error is, "embedded script: 'VariableCmds' object does not support indexing".

Posted on
Thu Oct 03, 2019 12:34 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: help request - debug python script

SMUSEBY wrote:
I'm very sorry - I'm asking for help, and clearly violating protocol. Doing my best, and trying to learn.
Is this what you want?


Yes.

SMUSEBY wrote:
Code: Select all
hseOcc = indigo.variable[hseOccupied]
if hseOcc is True:
    indigo.variable.updateValue[612296111], "False" #
else:
    indigo.variable.updateValue(612296111), "False" #


To repeat my question, the code crashes on the first line - 'global name 'hseOccupied' is not defined. If I replace the variable name with the ID, the error is, "embedded script: 'VariableCmds' object does not support indexing".


See my previous answer. You have two problems. You dropped the 's' from the indigo.variables dictionary name, and you need the key for the dictionary to be either a variable ID (an integer) OR a string. You put in an undefined Python variable. Again, it needs to be:

Code: Select all
hseOcc = indigo.variables["hseOccupied"]


Once you fix that, then you need to fix the lines to set the Indigo variable. You're using brackets instead of parenthesis, and you have the replacement value outside the function call.

Code: Select all
if hseOcc is True:
    indigo.variable.updateValue(612296111, "False")
else:
    indigo.variable.updateValue(612296111, "False")

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

Posted on
Thu Oct 03, 2019 1:02 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: help request - debug python script

Solved issues in first line - thank you.
Now the problem is line 2 - the the conditional test is not working: the variable ="True", and it is not being changed to "False" in line 3.
Code: Select all
hseOcc = indigo.variables[612296111]
if hseOcc is True:
    indigo.variable.updateValue(612296111, "False")
else:
    indigo.variable.updateValue(612296111, "True")


if I modify the script to:
Code: Select all
hseOcc = indigo.variables[612296111]
if hseOcc is True:
    indigo.variable.updateValue(612296111, "False")
else:
    indigo.variable.updateValue(612296111, "xyz")


it assigns xyz to the hseOccupied variable. There must be something wrong with line 2.

Posted on
Thu Oct 03, 2019 1:11 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: help request - debug python script

SMUSEBY wrote:
Solved issues in first line - thank you.
Now the problem is line 2 - the the conditional test is not working: the variable ="True", and it is not being changed to "False" in line 3.
Code: Select all
hseOcc = indigo.variables[612296111]
if hseOcc is True:
    indigo.variable.updateValue(612296111, "False")
else:
    indigo.variable.updateValue(612296111, "True")


if I modify the script to:
Code: Select all
hseOcc = indigo.variables[612296111]
if hseOcc is True:
    indigo.variable.updateValue(612296111, "False")
else:
    indigo.variable.updateValue(612296111, "xyz")


it assigns xyz to the hseOccupied variable. There must be something wrong with line 2.


Your line 1 is getting a reference to the Indigo variable, not the value of the variable. If you want to use the value of the variable as a boolean (as Line 2 is written) then it should be:
Code: Select all
hseOcc = indigo.variables[612296111].getValue(bool)
if hseOcc:
    indigo.variable.updateValue(612296111, "False")
else:
    indigo.variable.updateValue(612296111, "True")


In this case, instead of getting the reference to the variable, the method call (.getValue()) returns the value interpreted to be a boolean. And since it's a boolean now, you don't need to compare it to another boolean, you can just use it for the conditional as is.

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

Posted on
Thu Oct 03, 2019 1:19 pm
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: help request - debug python script

Hi Bob,

SMUSEBY wrote:
... BTW, I know I could do this with triggers, but I'm still determined to make something work with Python. This seems like an easy problem and learning opportunity for me. ...


If you refer to my answer above, you will see I already showed you how to do it. :wink:

You need to make sure that you copy the code presented to you and then you would likely get to the answer much quicker. :)

You may find this documentation helpful for future reference: Variables :)

Thanks @FlyingDiver for your patient assistance. :)

Posted on
Thu Oct 03, 2019 2:11 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: help request - debug python script

It works - thank you for your help and patience.
I am now working on a conditional script that involves strings. It runs without errors, but it is not turning on the device. In the variables link you referenced, I don't see anything equivalent to the get.value(bool) for strings. I think I'm following the syntax, but...
Script Objective, in English: if the valve is 'closed' AND if the season is not 'summer' (both string values), I want to turnOn a device.
Code: Select all
season = indigo.variables[325456157]
Valve = indigo.variables[1371508015]
if Valve == "closed":
   if season.value != "summer":
      indigo.device.turnOn(1437097498)

Posted on
Thu Oct 03, 2019 2:19 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: help request - debug python script

SMUSEBY wrote:
It works - thank you for your help and patience.
I am now working on a conditional script that involves strings. It runs without errors, but it is not turning on the device. In the variables link you referenced, I don't see anything equivalent to the get.value(bool) for strings. I think I'm following the syntax, but...
Script Objective, in English: if the valve is 'closed' AND if the season is not 'summer' (both string values), I want to turnOn a device.
Code: Select all
season = indigo.variables[325456157]
Valve = indigo.variables[1371508015]
if Valve == "closed":
   if season.value != "summer":
      indigo.device.turnOn(1437097498)

Again, your lines 1 and 2 are getting a reference to the Indigo variable, not the value of the variable. Indigo variables are natively strings, so if you want the string value, you just do:

Code: Select all
Valve = indigo.variables[1371508015].value


Your season test is fine. You missed .value on the valve test.

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

Posted on
Thu Oct 03, 2019 2:50 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: help request - debug python script

I see - the value is either in the variable definition or the test.
Thanks.

Who is online

Users browsing this forum: No registered users and 3 guests