[ANSWERED]Device Group and python

Posted on
Fri Jun 12, 2015 11:55 pm
rbrouwer offline
Posts: 3
Joined: Jan 01, 2015

[ANSWERED]Device Group and python

I have a virtual device consisting of 5 fibaro dimmers. I have a small python script that cycles a variable between 0, 25 and 100 each time it is run. I was wanting to then take the value of this variable, and update the brightness to each of the dimmers in the group to that value.

devId = 739620904 (Id of the dimmer group)
varId = 799897200

myVar = indigo.variables[varId]
newBrightness = int(myVar.value)

indigo.dimmer.setBrightness(devId, value = newBrightness)

Whilst the variable cycles nicely each time I press the assigned button, can't get the lights to work. I was wondering if this is the right approach.

Posted on
Sat Jun 13, 2015 8:38 am
Chameleon offline
Posts: 611
Joined: Oct 04, 2014

Re: Device Group and python

rbrouwer wrote:
I have a virtual device consisting of 5 fibaro dimmers. I have a small python script that cycles a variable between 0, 25 and 100 each time it is run. I was wanting to then take the value of this variable, and update the brightness to each of the dimmers in the group to that value.

devId = 739620904 (Id of the dimmer group)
varId = 799897200

myVar = indigo.variables[varId]
newBrightness = int(myVar.value)

indigo.dimmer.setBrightness(devId, value = newBrightness)

Whilst the variable cycles nicely each time I press the assigned button, can't get the lights to work. I was wondering if this is the right approach.


I'd take a different approach. Although Matt & Jay seem to be looking to increase the abilities of Virtual Groups I don't think they can do what you think - I'm hoping I'm wrong here BTW :D

I think I'd write some processing code to set each of the lights in turn based on the variable setting. In my case I've called the variable DimmerGroupBrightness

Code: Select all
# There are a couple of things that make me suggest this approach
#
#   1. I suspect that indigo doesn't see the virtual group as a dimmer so you can't set brightness unless Matt or Jay disagree
#
#   2. You generally have to get the devID number as I've shown rather than just the number itself as it's a reference to an object
#      - I normally right click on the device in Indigo and select Copy Python Reference and then paste
#      - it into my programmes
#
# This must be run from inside indigo
import indigo

# Change the variable 0, 25, 50, 100 etc...
# You've already got the code to do this and it works so I won't suggest anything
# Now act on the change

# Trigger - if the variable DimmmerGroupBrightness changes:

# Set up dimmer group in a list to make it easier to process later.  You can add as many items here as you need

# Note that I've copied the Python Reference from indigo directly (Right click on the device and select Copy Python Reference)

dimmerGroup = []
dimmerGroup.append(indigo.devices[1925423536]) # "Living Room Light"
dimmerGroup.append(indigo.devices[46056323]) # "Small Living Room Light (Dual)"
dimmerGroup.append(indigo.devices[1037977303]) # "Office/Chillout Room Light"
#...

# Now get the variable value from indigo using the same technique I did with the device by right clicking on the variable name in the variable window
dimmerSetting = indigo.variables[460137250] # "DimmerGroupBrightness"
dimmerValue = int(dimmerSetting.value) # You're right that indigo saves this as a string so you have to convert it

# Now change the brightness settings for each dimmer in turn
for dimmerNumber in range(len(dimmerGroup)):
    indigo.dimmer.setBrightness(dimmerGroup[dimmerNumber], value = dimmerValue)


I tried this as embedded Python code in a trigger that was looking for whenever the variable DimmerGroupBrightness changed and it worked fine for me :D Each time I changed the DimmerGroupBrightness variable it changed the lights as well :)

Good luck and let me if you need any more help

Regards

Mike

Posted on
Sat Jun 13, 2015 4:57 pm
rbrouwer offline
Posts: 3
Joined: Jan 01, 2015

Re: Device Group and python

Thanks for a great solution !

I had as a work around, made an action group and repeated this script 5 times, changing the devId each time.

varId = 799897200
devId = 140533913

myVar = indigo.variables[varId]

indigo.dimmer.setBrightness(devId, value = int(myVar.value))

Your way is so much more elegant - make a list of devices and iterate through each - 1 script - very nice. Also thanks for the advice about getting the device ID in python format.

With respect to Virtual Devices, I noticed in the wiki that the state of the group referred to the brightness if the device was a dimmer (0-100). I was wondering if there was a scriptable way of changing or updating the device state (I tried using updateStateOnServer()) to the value of a variable, but kept getting errors. I am also very inexperienced in scripting so might have been doing something wrong.

Posted on
Sat Jun 13, 2015 5:22 pm
Chameleon offline
Posts: 611
Joined: Oct 04, 2014

Re: Device Group and python

No problem... Yep done the same as you with multiple triggers or action groups. Finally decided that learning some scripting might help and I was right. Hard at the start but much easier to manage Indigo now. 3 months ago I was just starting out in Python and scripting and I got some good help. So I'm happy to help someone else. Got enough support so I could write the NEST Home plugin :)

I'll look at what you've mentioned but I don't think virtual devices work like that.

Mike

Posted on
Mon Jun 15, 2015 9:31 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Device Group and python

First, you can use the device group's device list in a script to adjust brightness. Here's a somewhat reworked version of Mike's script that used a device list's devices:

Code: Select all
# Start by getting the device group's device list and the value from the variable - if either of those
# things fail then log and exit the script
try:
   # get the device group - right click on the device group in the home window and select Copy Python Reference
   group = indigo.devices[1364669139] # "Some Device Group"

   # get the list of device ID's in the group
   deviceList = group.globalProps["com.perceptiveautomation.indigoplugin.devicecollection"]["deviceList"]

   # Now get the variable value from indigo using the same technique I did with the device by right clicking on the variable name in the variable window
   dimmerSetting = indigo.variables[153335959] # "DimmerGroupBrightness"
   dimmerValue = int(dimmerSetting.value) # You're right that indigo saves this as a string so you have to convert it

   # cycle through the device list setting the brightness of each one, but only
   # try to do it on ones that are actually dimmer devices
   for deviceId in deviceList:
      # While cycling through each device id, get the device instance and set the brightness - if either
      # fail then log the error and skip to the next one
      try:
         device = indigo.devices[int(deviceId)]
         if "brightnessLevel" in device.states:
            indigo.dimmer.setBrightness(device, value = dimmerValue)
      except:
         # something went wrong getting or setting this device, log and skip to the next one
         indigo.server.log("Couldn't get the device or set brightness for device '%s'" % device.name, type="My Script", isError=True)

except:
   indigo.server.log("Couldn't get the device group or variable", type="My Script", isError=True)


The advantage here is that you don't have to maintain two different lists (one in the script and the other in the device group). Change the device group and the script continues to work on just the devices in the group.

Second, you can update the stored state of the devices (brightness for dimmable devices and on/off state for on/off devices) in a device group by using the Device Actions->Virtual Device Controls->Update Device Group Saved States action. It will update each one to it's current value - so you want to make sure that they're all set how you want them set when you execute the action.

[MODERATOR NOTE] moved to the virtual devices section of the forums.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jun 15, 2015 10:18 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: [ANSWERED]Device Group and python

I think you have a typo, Jay. Shouldn't this line:

Code: Select all
deviceList = dg.globalProps["com.perceptiveautomation.indigoplugin.devicecollection"]["deviceList"]


be:

Code: Select all
 deviceList = group.globalProps["com.perceptiveautomation.indigoplugin.devicecollection"]["deviceList"]


Or vice versa (change group to dg).

joe

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

Posted on
Mon Jun 15, 2015 11:19 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: [ANSWERED]Device Group and python

Yep - that's what I get for trying to clarify names without testing again. Fixed in post.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Jun 16, 2015 4:05 am
rbrouwer offline
Posts: 3
Joined: Jan 01, 2015

Re: [ANSWERED]Device Group and python

Gee I"m learning a lot about python scripting from just one thread. Thanks heaps for the great ideas.

jay - forgive the question - is the reason you code in try/except clauses so if an error occurs the script can continue on rather than exit/halt where the error occurs ?

With respect to UpdateDeviceGroupsSavedStates action, can this be set to the value of a variable or is just to the current state of each device in the the device group ?

Thanks

Rich

Posted on
Tue Jun 16, 2015 9:25 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: [ANSWERED]Device Group and python

rbrouwer wrote:
jay - forgive the question - is the reason you code in try/except clauses so if an error occurs the script can continue on rather than exit/halt where the error occurs ?


Exactly. For this scenario, it seems to me that what you want is a best attempt at getting everything set rather than failing on any error.

rbrouwer wrote:
With respect to UpdateDeviceGroupsSavedStates action, can this be set to the value of a variable or is just to the current state of each device in the the device group ?


The latter - all devices must be set how you want them set when you update the saved states. It's not really meant as a general purpose mechanism for arbitrarily setting the "scene" nature of the device group, but rather as a convenient way to set the scene up and adjust it periodically later. Given that your requirement is to have 3 sets of "scene" settings, and given the cycle nature of how you want them to work, the script is the best approach. Basically, you're using the Device Group as just the single place where you manage the devices used by the group.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Jun 16, 2015 9:25 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: [ANSWERED]Device Group and python

rbrouwer wrote:
jay - forgive the question - is the reason you code in try/except clauses so if an error occurs the script can continue on rather than exit/halt where the error occurs ?


Exactly. For this scenario, it seems to me that what you want is a best attempt at getting everything set rather than failing on any error.

rbrouwer wrote:
With respect to UpdateDeviceGroupsSavedStates action, can this be set to the value of a variable or is just to the current state of each device in the the device group ?


The latter - all devices must be set how you want them set when you update the saved states. It's not really meant as a general purpose mechanism for arbitrarily setting the "scene" nature of the device group, but rather as a convenient way to set the scene up and adjust it periodically later. Given that your requirement is to have 3 sets of "scene" settings, and given the cycle nature of how you want them to work, the script is the best approach. Basically, you're using the Device Group as just the single place where you manage the devices used by the group.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Jun 17, 2015 4:33 pm
Chameleon offline
Posts: 611
Joined: Oct 04, 2014

Re: Device Group and python

jay (support) wrote:
First, you can use the device group's device list in a script to adjust brightness. Here's a somewhat reworked version of Mike's script that used a device list's devices:

Code: Select all
# Start by getting the device group's device list and the value from the variable - if either of those
# things fail then log and exit the script
try:
   # get the device group - right click on the device group in the home window and select Copy Python Reference
   group = indigo.devices[1364669139] # "Some Device Group"

   # get the list of device ID's in the group
   deviceList = group.globalProps["com.perceptiveautomation.indigoplugin.devicecollection"]["deviceList"]

   # Now get the variable value from indigo using the same technique I did with the device by right clicking on the variable name in the variable window
   dimmerSetting = indigo.variables[153335959] # "DimmerGroupBrightness"
   dimmerValue = int(dimmerSetting.value) # You're right that indigo saves this as a string so you have to convert it

   # cycle through the device list setting the brightness of each one, but only
   # try to do it on ones that are actually dimmer devices
   for deviceId in deviceList:
      # While cycling through each device id, get the device instance and set the brightness - if either
      # fail then log the error and skip to the next one
      try:
         device = indigo.devices[int(deviceId)]
         if "brightnessLevel" in device.states:
            indigo.dimmer.setBrightness(device, value = dimmerValue)
      except:
         # something went wrong getting or setting this device, log and skip to the next one
         indigo.server.log("Couldn't get the device or set brightness for device '%s'" % device.name, type="My Script", isError=True)

except:
   indigo.server.log("Couldn't get the device group or variable", type="My Script", isError=True)


The advantage here is that you don't have to maintain two different lists (one in the script and the other in the device group). Change the device group and the script continues to work on just the devices in the group.

Second, you can update the stored state of the devices (brightness for dimmable devices and on/off state for on/off devices) in a device group by using the Device Actions->Virtual Device Controls->Update Device Group Saved States action. It will update each one to it's current value - so you want to make sure that they're all set how you want them set when you execute the action.

[MODERATOR NOTE] moved to the virtual devices section of the forums.


Genius idea... I can use that as well. Thanks Jay :)

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest