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!
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
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
- jay (support)
- Site Admin
- Posts: 19232
- Joined: Wed Mar 19, 2008 11:52 am
- Location: Austin, Texas
- Contact:
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
I've added exception handling throughout to help illustrate how that works as 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)
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.
Re: Setting Brightness of a Device to a Variable Value
Worked very well, thank you.