Setting Brightness of a Device to a Variable Value

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

Posted on
Mon May 06, 2013 7:21 pm
vtmikel offline
Posts: 643
Joined: Aug 31, 2012
Location: Boston, MA

Setting Brightness of a Device to a Variable Value

I'm sorry to ask such a basic question, but I'm struggling writing a basic phython script that will set the brightness of a Dimmer device to the value of a variable. Can someone show me some example code?

Thank you!

Posted on
Tue May 07, 2013 10:18 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Setting Brightness of a Device to a Variable Value

The Scripting Tutorial has examples of the Indigo specific parts you need - for users new to Python and the Indigo IOM that's a good place to start. Here's a script that should work pretty well

Code: Select all
# Define the IDs for the variable and device
variableId = VARIDHERE
deviceId = DEVICEIDHERE
# Get the variable object
try:
   myVar = indigo.variables[variableId]
except:
   indigo.server.log(u"Variable id %i doesn't exist." % variableId)
# Next, we need to turn it into an integer value and make sure
# it's in the appropriate range (0-99). We'll use a try block
# and log an error if the value can't be turned into an
# integer or if it's out of range
try:
   # Convert the value to an int - it will throw if it can't
   newBrightness = int(myVar.value)
   # Throw if it's not in range
   if newBrightness not in range(101):
      raise Exception
except:
   indigo.server.log(u"Variable value is either not an integer or not from 0-100.")
   return
# We now know newBrightness is an integer from 0-100. Set the value and
# catch any exceptions which most likely means the device isn't a dimmer.
try:
   indigo.dimmer.setBrightness(deviceId, value=newBrightness)
except:
   indigo.server.log(u"Device id %i is not a dimmer." %  deviceId)


I've added exception handling throughout to help illustrate how that works as well.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue May 07, 2013 9:37 pm
vtmikel offline
Posts: 643
Joined: Aug 31, 2012
Location: Boston, MA

Re: Setting Brightness of a Device to a Variable Value

Thanks for this, sorry to ask such a newbe question to python, but I seriously couldn't figure it out with the existing documentation. Mostly python skills was keeping me back, not Indigo. I haven't completed this yet, got sidetracked, but I'll hopefully figure it out with your help.

Posted on
Fri May 10, 2013 3:27 pm
vtmikel offline
Posts: 643
Joined: Aug 31, 2012
Location: Boston, MA

Re: Setting Brightness of a Device to a Variable Value

Worked very well, thank you.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest