(python scripting) find and control all devices in a folder?

Posted on
Mon Dec 11, 2023 12:47 am
dliccardo offline
Posts: 53
Joined: Jun 09, 2014

(python scripting) find and control all devices in a folder?

[MODERATOR NOTE]: moved to the appropriate forum.
---------------------
Hi All!

Working on a python script. I have my z-wave lights organized by folders. I would like to be able to select all devices in a folder and say, turn them off.

What is the best way to find all devices in a folder?

I guess I can get the folder ID this way? indigo.devices.folders.getId("Some Folder Name")

Now do I have to loop through every device in my system to check if it's in the folder? That doesn't seem like it will be very performant. Is there no way to ask a folder what devices it has in it?

Thanks,
Darren

Posted on
Mon Dec 11, 2023 6:40 am
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: (python scripting) find and control all devices in a fol

You didn't say what version of Indigo you're working with (which version of Python), so here's a Python 3 example:

Code: Select all
for dev in indigo.devices.iter():
    if dev.folderId == 251697344:
        indigo.device.turnOff(dev.id)
There's no way to ask a folder which objects are in it, but iterating through all the objects doesn't take long (the above script ran in 0.2 seconds on an Intel MBP for about 150 devices). There is no filter for folder ID [ indigo.devices.iter(filter="") ], but that would just be moving the iteration from your script to the server.

You could run a script to "watch" your Indigo setup and maintain a dictionary of folders and their contents (to save it to a variable, you'd need to convert it to JSON). But honestly, performance shouldn't be an issue with the above.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Dec 11, 2023 9:19 am
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: (python scripting) find and control all devices in a fol

For completeness, here's one way to create a JSON payload and save it to an Indigo variable.

Code: Select all
import json

object_dict = {
    'devices': indigo.devices,
    'variables': indigo.variables,
    'triggers': indigo.triggers,
    'schedules': indigo.schedules,
    'actionGroups': indigo.actionGroups,
    'controlPages': indigo.controlPages
    }
folder_dict = {key: [] for key in object_dict}   # makes a dict like {'devices': [], variables: [], ...}

for key, value in object_dict.items():           # i.e., key = 'devices', value = indigo.devices
    for item in value.iter():                    # i.e., for dev in indigo.devices.iter()
        folder_dict[key].append(item.id)         # i.e., folder_dict['devices'].append(dev.id)
       
my_json = json.dumps(folder_dict)                # convert the dict to json
indigo.variable.updateValue(123456789, my_json)  # variable values are always strings, so must be stored as json

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Thu Dec 14, 2023 11:00 pm
dliccardo offline
Posts: 53
Joined: Jun 09, 2014

Re: (python scripting) find and control all devices in a fol

Thanks for your thoughts and help @DaveL17. I ended up going with the simple iteration you suggested. My z-wave network is much slower than the iteration logic anyway ;). I was able to clean up and future proof a few action groups I have with this help. Cheers!

Posted on
Fri Dec 15, 2023 2:22 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: (python scripting) find and control all devices in a fol

Glad you were able to land on something useful.

Cheers.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 19 guests