Button to control 2x Bedroom Lights in a alternating pattern


#1

I am new at programming anything, I know there is some flaws in the logic of my code. If anyone can let me know what I have been doing wrong and optimize the code that would be nice.

I know the commands are not working so there is no point in posting logs yet.

1) Give a description of the problem
I have a XiaoMi wireless switch/button and want to use it alternate the bedroom lighting to Left->Right->Both->Off pattern

2) What is the expected behavior?

  • LEFT ON, RIGHT OFF
  • RIGHT ON, LEFT OFF
  • LEFT ON, RIGHT ON
  • LEFT OFF, RIGHT OFF

3) What is happening/not happening?
It only alternates between RIGHT and LEFT

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

5) Attach any logs (From ST IDE and by turning logging level to Full)
(PASTE YOUR LOGS HERE BETWEEN THE MARKS THEN HIGHLIGHT ALL OF THE LOGS SND CLICK ON THE </> ICON TO FORMAT THEM)


#2

Ok, there’s two ways I could help with this… I could post an example piston that will do what you want. Or we can go through what you posted and discuss what will and won’t work. Which way do you learn best?


#3

Thank you for replying, if you can post an example piston, then I can have a look and ask more questions once I do more trial and error.

Although I am not a programmer, but if my logic is correct it would be like this:

Define events:
Event 1 - All Off
Event 2 - Left On, Right Off
Event 3 - Left Off, Right On
Event 4 - All On

Action:
Assign the button to progress each event sequentially (1-2-3-4-1-2-3-4-1 so forth) when button is pushed.

I simply don’t know enough webcore to set this whole thing up.


#4

No problem, I’ll post up an example you can dissect and ask questions!


#5

There are loads of different ways to do this but this is one of the most straightforward:


#6

Thanks…

Your example piston is exactly what I thought - define the event {LightSequence} and follow with simple if/else statements.

Now I understand what I have been doing wrong and I will try to implement “held” command on the button.


#7

I’ve imported your example and added the “held” command.

It looks like to be working, what do you think?


#8

Looks good! The only thing I would do to simplify is get rid of the IF statement in line 89. If “any of switches x or y are on” evaluates false, then the only other condition available is switches x and y are off. Just put the “with switches X and Y, turn on” action alone in the else section of the previous IF (line 81).