Page 1 of 1

Clearing delayed actions for multiple devices in one command

PostPosted: Mon Dec 25, 2023 8:24 pm
by durosity
I've been trying to rationalise and improve a lot of my sprawling python scripts, and I'm not sure if this is possible.

At present I have some action groups that run that have multiple lines for clearing device specific delayed actions, like so:

indigo.device.removeDelayedActions(monitor)
indigo.device.removeDelayedActions(lights)
indigo.device.removeDelayedActions(fan)
etc

I've been trying to find a way to get it into one single line. Most of what I've tried has failed on a syntax error. Closest I can find is:

indigo.device.removeDelayedActions(monitor),(lights),(fan)

but this doesn't seem to remove the delayed actions at all. Is this possible and if so what's the best way to do it?

Re: Clearing delayed actions for multiple devices in one com

PostPosted: Tue Dec 26, 2023 7:22 am
by DaveL17
I'll answer by saying that you can't, and then show you how you can. :wink:

The command indigo.device.removeDelayedActions only accepts one argument at a time, and then clears all delayed actions from the target device. The command doesn't accept collections of devices (although I believe this feature may be on the list). You can iterate over a collection of devices, which takes a couple of lines--and it's the way I'd recommend doing it because it's more Pythonic.

Code: Select all
device_list = (123456789, 987654321, 234567890)
for item in device_list:
    indigo.device.removeDelayedActions(item)
It's only three lines, and it's easy to see what's going on.

That said, you can use a little Python "trickery" using a List Comprehension, which compacts these operations down to a single line:

Code: Select all
_ = [indigo.device.removeDelayedActions(item) for item in (123456789, 987654321, 234567890)]
Still relatively easy to understand, but the above code is way more obvious. The underscore is a common way to indicate a "toss away" value which is really only necessary to make Python syntax inspectors happy (not required).

Re: Clearing delayed actions for multiple devices in one com

PostPosted: Tue Dec 26, 2023 9:22 am
by durosity
Ooooh perfect! That makes sense. I shall have a play with this later on. Much appreciated!

Re: Clearing delayed actions for multiple devices in one com

PostPosted: Wed Dec 27, 2023 10:52 am
by ryanbuckner
Dave, this forum totally need an up vote feature.

Re: Clearing delayed actions for multiple devices in one com

PostPosted: Wed Dec 27, 2023 11:03 am
by DaveL17
ryanbuckner wrote:
Dave, this forum totally need an up vote feature.

Appreciated, but not necessary. I've received a metric ton of help on these fantastic forums and I'm happy to help out when I can.

Re: Clearing delayed actions for multiple devices in one com

PostPosted: Sat Dec 30, 2023 7:25 pm
by durosity
Oh and this worked perfectly. And I’ve adapted it for use with a few other lines that were similarly setup! Many thanks!


Sent from my iPhone using Tapatalk Pro

Re: Clearing delayed actions for multiple devices in one com

PostPosted: Sat Dec 30, 2023 7:48 pm
by DaveL17
Glad to help.