Python, Sprinklers and Control Page project

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
Forum rules
This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
scotsman
Posts: 66
Joined: Sat Aug 14, 2010 7:24 am
Location: Austin, Texas

Python, Sprinklers and Control Page project

Post by scotsman »

OK so my first foray into Python. I am sure there are lots of stupid errors here but I can't find many example Indigo python scripts around yet.

My sprinkler control page is attached.

What I want to do:
1) The larger sprinkler button in the top left represents the on state of the sprinklers. Orange for on, blue for off.
- Tapping in the off state begins the sprinkler schedule.
- Tapping in the on state stops all sprinklers.

2) I can run individual sprinklers by tapping their corresponding icon. Their colour changes. A second tap switches that sprinkler off again.

3) Provide a control page to adjust individual sprinkler durations.

It's more complex than I thought...

Here's what I have written out so far.

For the individual sprinklers:
1) I have an action group for each sprinkler. They each run a different python script that runs that sprinkler for a time that is stored in a series of variables.

Code: Select all

indigo.sprinkler.run(1875748152, schedule=[indigo.variable(138167704),0,0, 0, 0, 0, 0, 0])
This doesn't work. Can anyone help me with using a variable this way in python grammar?

2) On the control page I have two image files named avon_sprinkler.png and avon_sprinkler+On.png. These are set to reflect device state of that particular zone. This works fine.

3) Tapping a sprinkler icon executes a python script:

Code: Select all

# If that sprinkler isn't running, activate the corresponding action group that will run it.  If it is running, stop it.
if indigo.devices[1875748152].activeZone!=4:
	indigo.actionGroup.execute(624487976)
else:
	indigo.sprinkler.stop(1875748152)
This works fine.

Now for the master sprinkler control:
1) I have a trigger set-up that runs the following python script should an Active Zone Name have any change in any sprinkler:

Code: Select all

#If all sprinklers are off, update the variable "sprinklers_On" to false, else set to true.
if indigo.devices[1875748152].activeZone==-1:
	indigo.variable.updateValue(736137373, "false")
else:
	indigo.variable.updateValue(736137373, "true")
This works fine.

2) On the control page I have two image files named avon_sprinkler_large.png and avon_sprinkler_large+true.png. These are set to reflect the variable value of sprinklers_On. This works fine.

3) Tapping this button executes a script:

Code: Select all

#If all sprinklers are off, then commence the schedule, else switch everything off.
if indigo.variable[736137373]=="false":
        indigo.sprinkler.run(1875748152, schedule=[indigo.variable(138167704),indigo.variable(xxxxx),etc.])
else:
	indigo.sprinkler.stop(1875748152)
Again not sure how I use variables in this way.

Phew. Any hints to get my python in order? And is there an easier way to do this?

Thanks!
Attachments
photo.PNG
photo.PNG (63.77 KiB) Viewed 1109 times
scotsman
Posts: 66
Joined: Sat Aug 14, 2010 7:24 am
Location: Austin, Texas

Re: Python, Sprinklers and Control Page project

Post by scotsman »

Also another basic question. How to I test the contents of a variable? This doesn't seem to work (it always tests false):

Code: Select all

if indigo.variables[484189783] == "true":
	indigo.variable.updateValue(47276982, "Normal")
else:
	indigo.variable.updateValue(47276982, "Low")
User avatar
jay (support)
Site Admin
Posts: 19232
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Python, Sprinklers and Control Page project

Post by jay (support) »

This code:

Code: Select all

someVar = indigo.variables[484189783]
returns a variable object. To access the value:

Code: Select all

someVar = indigo.variables[484189783]
if someVar.value == "true":
    #do whatever
Variable values are always strings, so if you want to use the value as something else (like an integer in your former example):

Code: Select all

someVar = indigo.variables[484189783]
try:
    someInt = int(someVar.value)
    indigo.sprinkler.run(1875748152, schedule=[someInt, 0, 0, 0, 0, 0, 0, 0])
except:
    # the variable value probably wasn't a valid integer value
    indigo.server.log(u"The value of variable %s (%s) isn't a valid integer" % (someVar.name,someVar.value))
The code above is untested but should be pretty close.
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
scotsman
Posts: 66
Joined: Sat Aug 14, 2010 7:24 am
Location: Austin, Texas

Re: Python, Sprinklers and Control Page project

Post by scotsman »

That's wonderful Jay - thank you so much for your help. Working a treat so far!
Locked

Return to “Extending Indigo with Plugins and Python”