Updating variable value

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

Updating variable value

I'm trying to update a variable "scenes" with a number. Can't seem to get it right.
Second question: I need to stop the execution once a scene has been executed. Is the "import sys" and "sys.exit()" going to work?
Code: Select all
# get status of living room ceiling fixture lights
#import sys
scene = indigo.variables[586684114].getValue(int) #

if scene == 0:
   indigo.actionGroup.execute(40370896) #scene #1
   # update variable to current scene
#   scene = 1
   indigo.variable.updateValue(586684114, value = 1)
#   sys.exit()
if scene == 1:
   indigo.actionGroup.execute(1620661527) #scene #2
   # update variable to current scene
   scene = 2
#   indigo.variable.updateValue(586684114, value==2)
#   sys.exit()

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

Re: Updating variable value

Again, variable values are strings in Indigo, so just as you have to convert it to an int when you get it, you have to make it a str when updating it.

Code: Select all
scene = indigo.variables[586684114].getValue(int)
if scene == 0:
    indigo.actionGroup.execute(40370896) #scene #1
    indigo.variable.updateValue(586684114, value="1")
elif scene == 1:
    indigo.actionGroup.execute(1620661527) #scene #2
    indigo.variable.updateValue(586684114, value="2")


In order to make the script more maintainable, you probably only want to hardcode the variable ID in one place so if you need to change it in the future you only have to change it once. There are a couple of options, but I would do this:

Code: Select all
# Get the variable object
scene_var = indigo.variables[586684114]
# Get it's current value as an integer
old_scene_number = scene_var.getValue(int)
# Set the new scene number to the current number - we'll use this to update later
new_scene_number = str(old_scene_number)

if old_scene_number == 0:
    indigo.actionGroup.execute(40370896) #scene #1
    new_scene_number = "1"
elif old_scene_number == 1:
    indigo.actionGroup.execute(1620661527) #scene #2
    new_scene_number = "1"

# Update the variable using the new_scene_number (which may not change if the scene number isn't 0 or 1)
indigo.variable.updateValue(scene_var, value=new_scene_number)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

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

Re: Updating variable value

I have 4 scenes that this script needs to address. My thought is that I start with all off (scene 0), and implement scene 1 (dim); if I want more light, it will increment the scene variable to 2, and get brighter, followed by 3 and 4, and then back to all off.
Because my design is to increment the value of the scene variable with each press of the keypad button, I thought I needed to put the exit() in the script to stop the script from looping from 0 to 1 to...to 4 and back to 0.
You proposed two versions.
1) doesn't the first one require the exit() before the elif line?
2) this one I find totally confusing: where is the value for the variable 'scene' assigned? And I'm not sure it permits the incrementing of the variable with button pushing.
I didn't fully describe what I was trying to do.
Any new thoughts?
Bob

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

Re: Updating variable value

SMUSEBY wrote:
1) doesn't the first one require the exit() before the elif line?


No. It's very rare that you need to explicitly stop a script from running, if your script is constructed properly. The elif line is only evaluated if the scene is 1: so if the scene is 0, the first section is executed, if the scene is 1, the second section will be executed, if the scene is something else, nothing happens and the script exits naturally (this is generally how a correctly written script will work).

SMUSEBY wrote:
2) this one I find totally confusing: where is the value for the variable 'scene' assigned? And I'm not sure it permits the incrementing of the variable with button pushing.


The second script does exactly what the first script does. The only difference is that rather than have two separate variable update calls, we set a Python variable (new_scene_number) to be what we want the scene number in the variable to be when the script is finished, then only call the update at the end of the script.

Completing a Python tutorial would definitely have helped your understanding on both of these.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Nov 18, 2019 10:24 am
matt (support) offline
Site Admin
User avatar
Posts: 21416
Joined: Jan 27, 2003
Location: Texas

Re: Updating variable value

Hi Bob – note the script doesn't have any looping at all. It just has a conditional. So when it is executes it just runs once through and stops. If you need the script called multiple times then that would be additional logic – either from an Indigo schedule or trigger, or by adding a loop to the script.

Image

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

Re: Updating variable value

I think we're close. No looping with the updating at the bottom - clever, and thank you.
However, the conditional is not working - i.e., with the scene_var = 0, the two indented steps are not executed. The event log displays only 'running a script', and no errors. Syntax error for the variable?
Code: Select all
# Get the variable object
scene_var = indigo.variables[586684114]

# Get it's current value as an integer
old_scene_number = scene_var.getValue(int)
# Set the new scene number to the current number - we'll use this to update later
new_scene_number = str(old_scene_number)

#   scene = 1
if scene_var == 0:
   indigo.actionGroup.execute(40370896) #scene #1
   new_scene_number = "1"

#   scene = 2
elif scene_var == 1:
    indigo.actionGroup.execute(1620661527) #scene #2
    new_scene_number = "2"

# scene = 3
elif scene_var == 2:
    indigo.actionGroup.execute(1785140140) #scene #3
    new_scene_number = "3"

# scene = 4
elif scene_var == 3:
    indigo.actionGroup.execute(845981601) #scene #4
    new_scene_number = "4"

# scene = 0
elif scene_var == 4:
    indigo.actionGroup.execute(927532899) #scene #0
    new_scene_number = "0"

# Update the variable using the new_scene_number (which may not change if the scene number isn't 0 or 1)
indigo.variable.updateValue(scene_var, value=new_scene_number)

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

Re: Updating variable value

You should be using old_scene_number in your if statements - scene_var is a reference to an Indigo variable object, not the actual variable value.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

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

Re: Updating variable value

Of course. Thanks.
And very cool.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests