Cycle automations/scenes with list/sequence instead of nested ifs


#1

1) Give a description of the problem
Not so much a problem, I’m just looking for a more graceful/future proof approach.

I have a piston that cycles through a list of virtual switchs with the push of a remote button.
On push - If Virtual Switch 1 is on, turn on Virtual switch 2
On push - If Virtual Switch 2 is on, turn on Virtual switch 3
and so on…

Each Virtual switch runs a different Automation in Smartthings (triggering a specific scene), which includes turning off the other Virtual Switches. e.g. The Automation run by VS2, turns off VS1, VS3, VS4 etc.

I know I could use the piston to just run the Scenes I want to run, but I’m doing this way to help with integration in sharptools and Alexa. Switches seem to solve my problem.

So my question is basically, how do I do this more gracefully than just a series of nested If statements? I’m new to webcore, and have an extremely basic knowledge of coding.

But I’m looking for something along the lines of:
For each switch in the list; if previous switch is on, trigger the next switch.
With some kind of +1 incrementation so can easily add more Switches to the list, without keep creating more nested Ifs.

2) What is the expected behaviour?
It works, so just hoping to recreate what I’ve got.

3) What is happening/not happening?

**4) Post a Green Snapshot of the piston![image|45x37]

5) Attach logs after turning logging level to Full

Thanks in advance for any help.


#2

(I’ve just noticed a problem with this, in that it doesn’t actually turn on switch 1 at any point… It should do. That’s my bad, and I need to fix it. But my question remains the same… How could I do this whole piston without all of the nested ifs?)

Thanks


#3

There are many ways to do what you are asking. I would suggest using a Switch block. See HERE and HERE for information.


#4

You can also Drag & Drop code to easily copy blocks (or lines) at a later time.


#5

Thank you both for your help.

I’m not sure if this is what you meant @Pantheon , but did you mean something like this…

Thanks


#6

I have not had my coffee yet, and I have not tested this. So not sure this will work. It also may be overkill and IF/ELSE may work better…

define
device Devices_On;

IF Dimmer 1's button #1 gets pushed
Then
  If any of Switch 2, Switch 3, Switch 4 is on
    Save matching devices (Devices_On)

Switch (Devices_On)
  case Switch 2;
    with Switch 3
      turn on

  case Switch 3;
    with Switch 4
      turn on

  case Switch 4;
    with Switch 5
      turn on

  default :
    with Switch 2
      turn on
END switch

#7

@Hendo25, To build on @Pantheon’s suggestion, here is an option that may be more extendable:

define
device deviceList=Switch 1,Switch 2, Switch 3, Switch 4;
device Devices_On;
device nextDevice;
integer numberOfDevices=count(deviceList);
integer indexVal;

IF Dimmer 1's button #1 gets pushed
Then
  If any of deviceList is on
    Save matching devices (Devices_On)
  then
       set variable indexVal=indexOf(deviceList,Devices_On)+1
       if indexVal is greater than or equal to numberOfDevices
       then
           set indexVal=0
       endif
       set nextDevice=arrayItem(indexVal,deviceList)
       with Device_On
            turn off
       end with
       with nextDevice
            turn on
       end with
  end if

With this you should be able to add/subtract devices to the list and everything will work.


#8

Thank you both so much for your help.

I’m completely new to this, and have this challenging and interesting. I couldn’t get @Pantheon s solution to work. (I’m assuming the solution does work, but my idiot attempts at coding it, didn’t).

However - I did get @guxdude code to work.

Thanks for your help.


#9

I noticed one error in my code. It will work for now but you really want the first indented ‘if’ to be:

if any of deviceList is on

This will be sure you are checking all the devices as you add/subrtract from the list. I corrected in my post above. If you could check my post above as the solution for future searchers, that would be great.

Glad it is working for you.


#10

Glad you got it working. Coding is always an adventure.


#11

I threw together some code for my example. If you would like, load this piston and give it a try.

Screenshot_2020-06-18%20webCoRE%20Dashboard


#12

I think the missing element from your example is the proper turn off. Since at each switch you know which one was on, you just need to turn that off and then turn the next one on. That way with each button press it will cycle forward. This should work great. Adding more switches/scenes is only slightly more cumbersome as you have to add another case statement.


#13

I thought he was already turning off the switches within ST.


#14

You’re right. I misunderstood that part. :blush: