Jump to content

Wrapping my head around how the ISY's ELSE works ...


bill02888

Recommended Posts

Posted

I've only got a few simplistic programs controlling the 3 Insteon devices that I currently have. I'm considering expanding my system so I'm trying to get a better understanding of how the If works and, in particular, I'm trying to wrap my head around when (and how) the ELSE condition executes. My background: I wrote my first programs (on programmable calculators) back in the late 70s. Been a programmer/analyst since the mid 80s.

 

If I have "If Time is Sunrise THEN Run Program X ELSE Run Program Y", I know that the THEN portion is executed AT (or at least very close to) Sunrise. I'm just not clear on when the ELSE runs. Clearly it doesn't run every millisecond that it's NOT sunrise. Is every If statement's condition evaluated whenever any part of the condition changes, and the corresponding THEN or ELSE is run ONLY when the entire condition transitions from true to false or from false to true?

 

I have none of my programs set to Run At Startup. What sort of evaluation and processing is done after a power failure for non-Run At Startup programs? I may use the ISY to control various aquarium equipment and want to make sure that everything resumes correctly after a power failure. Are there any particularly pesky situations I should be aware of in which I might consider creating Run At Startup programs to re-test and re-set certain program states?

 

Thanks,

Bill

Posted
Is every If statement's condition evaluated whenever any part of the condition changes, and the corresponding THEN or ELSE is run ONLY when the entire condition transitions from true to false or from false to true?

 

In general, this is my understanding. Programs are executed only at the time a condition is evaluated, which is when one or more of the statements transitions. I don't believe that a the if condition has to change state, but only be evaluated, even if the state remains unchanged.

 

Based on the number of posts about this in the past, this is apparently confusing to many. You may benefit from tracking down some of the earlier topics.

Posted

Hello Bill,

 

oberkc got it right! In short, every time there's a change in the state of any device to which the condition depends on, then program is re-evaluated and if true, the Then part will run.

 

With kind regards,

Michel

Posted

If I can put in my 2 cents worth - the part that gets confusing for most (myself included) is related to the else and the constant reevaluation

 

You see... if you have a condition like

 

If switch is turned on then

do something

wait 5 minutes

do something else

else

 

then when the switch turns on the do something will happen but - and here's the big but - if the switch turns off in the meantime then the whole condition will be reevaluated and the else will happen. The wait will never finish - whatever was happening will stop and the else will happen. So if you're counting on the something else happening after the wait you must be sure that the condition doesn't change in the meantime.

 

This can be very confusing - for instance

 

If button turns on

wait 5 minutes

turn aquarium off

else

 

is your program - when the button turns on the if will execute but if the button turns off before the 5 minutes are up the aquarium will NOT turn off. The else will happen and it's blank

 

The simple explanation is this.

Every time the evaluated condition changes and the condition must be reevaluated the program, if running, will be terminated. It will then be reevaluated and the appropriate If or Then will execute.

 

 

So...

 

The way around this is to code so that this doesn't 'getcha'. If you always want the wait and whatever else to happen when a button is pressed then you put those items in a different program. Like this

 

prog1

If button turns on

run prog 2

else

blank

 

prog2

wait 5 minutes

turn aquarium off

 

This will keep make the wait always happen when the button turns on and it won't stop when the button turns off. BUT - it will start the wait all over again if the button turns on again.

 

See how it can get confusing?

 

You have to keep this in mind when designing your logic and it WILL come back to bite you in the behind if you're not careful.

 

mark

Posted

Let me first say that I have several times expressed the need to seperate the trigger conditions for a program from the IF statement conditions, i.e. what starts the program running versus what conditions are evaluated to resolve the IF statement. Much of the confusion could be eliminated by making this change in the programming structure.

 

Second, with all due respect, Mark James's example is not quite correct. This example confuses the difference between "Control" conditions and "Status" conditions. If your condition is "IF switch is turned ON" (a "Control" condition), then the program will only be executed if the switch is turned ON. The program will not run if the switch is turned OFF. For example:

 

If
       Control 'Master Suite / Keypad - G' is switched On

Then
       Set 'Master Suite / Left Sconce' On

Else
       Set 'Master Suite / Left Sconce' Off

This program will turn the "Left Sconce" ON when the G button is turned ON. However, it will never turn the "Left Sconce" OFF (the ELSE branch), nor will the program status ever be FALSE. The IF condition in this program will always be true, because the program is only run if the G button is turned ON. Compare that with:

 

If
       Control 'Master Suite / Keypad - G' is switched On
   And Control 'Master Suite / Keypad - G' is not switched Off

Then
       Set 'Master Suite / Left Sconce' On

Else
       Set 'Master Suite / Left Sconce' Off

In this case, the program works more along the lines of what would be expected. This program is run anytime a G button command is sent, be it ON, OFF, DIM, whatever. This is because all commands are needed to evaluate the "IF switch is not switched OFF" condition. Therefore, turning G ON will turn the "Left Sconce" ON and turning G OFF will turn the "Left Sconce" OFF.

 

Contrast all of this with "Status" conditions:

 

If
       Status  'Master Suite / Keypad - G' is On

Then
       Set 'Master Suite / Left Sconce' On

Else
       Set 'Master Suite / Left Sconce' Off

This program will run anytime the status of G changes. So, if G is OFF and G is switched ON, then the program will run and the "Left Sconce" will turn ON. If G is subsequently turned OFF, the program will run again and the 'Left Sconce" will turn OFF. Note, however, that if G is ON and is switched ON again (such as a keypad button in non-toggle ON mode), the program will not run since the status hasn't changed, where the two programs provided above will run anytime G is switched ON, regardless of the previous status.

 

Hopefully this helps you understand WHEN programs run, and thus what the likely outcome of the IF statement will be (THEN branch or ELSE branch). Mark James's comment regarding a WAIT in a program is correct. If the program is in a WAIT and is executed again, then the WAIT is termintated, and any subsequent statements are not executed, in favor of the newly executing instance of that program. The same holds true for a REPEAT.

Posted

My apologies...

 

I was paying no attention to the details of the program itself but rather to the concept involved. My example should have clearly used a STATUS check not a CONTROL check as there really isn't a reevaluation to a CONTROL check. I don't think I actually specified either - but I can see how what I wrote could be read that way.

 

My point was that the fundamental concept you have to understand is that each and every time the condition in the IF changes and gets reevaluated then the program will terminate if it's running and the program re-executes the appropriate IF/THEN branch.

 

In my admittedly bad example - the case of the CONTROL check, kingwr is right - the program will run when a control ON is sent but will never do anything when an OFF is sent. There is no reevaluation of the IF when the OFF is sent as there's been no change in the condition. Receiving an OFF is not an ELSE to receiving an ON unless, as kingwr pointed out, your condition has the control NOT OFF as a control sending OFF will cause a reevaluation of a NOT OFF control condition. This is a simple way to avoid writing two programs - one for control ON and another for control OFF. Of course, if you base the program on STATUS, the status does in fact change as the KPL switches ON or OFF and the whole thing becomes easier to read.

 

This frequently leaves new ISY programmers scratching their heads wondering why their program didn't do what they thought it would.

 

Personally, I'd prefer to see programs have an option where you could select a checkbox and not have the program reevaluate until it has completed its execution. This would clarify some aspects.

 

On the other hand, the way it is works great for things like motion sensors where each and every time the sensor detects motion and sends an ON it can retrigger a program that does something WHILE the sensor is on.

 

Anyways - I hope the part about the program terminating and starting over each time the program gets reevaluated helps - that's the part that gets most people. It's easy to write a program that does something like this

 

If status kpl-a is on

turn kpl-a back off

turn on a bunch of lights

turn on a bunch of other lights

repeat

flashing a light for a while

turn off some lights

do something else

else

do nothing

 

The problem with it is the first line will cause the KPL to turn back off and cause the program to terminate and run the ELSE so nothing past that will happen (actually there will be a brief lag so some of it might).

  • 3 weeks later...
Posted

Okay - so Im missing something. Im trying to evaluate is a particular light is on, if it is do nothing, if its not, turn it on. My setup is an IOLinc on the garage door (on is closed), and a dimmer on the kitchen light. Ive tried and/or (this has to be where Im screwing up) in the "IF", and it always turns the light ON no matter what.

 

My program looks like;

 

IF

From Sunset - 30 minutes

To Sunrise (next day)

 

Or Control 'Garage-Sensor' is switched Off

And Control 'Kitchen' is not switched Off

 

Then

Set 'Kitchen' On

 

Else

- No Actions

 

 

 

Essentially this ALWAYS turns on the light (even when dimmed) to full on whenever the IOLinc sensor changes status. Any suggestions greatly appreciated

 

Matt

Posted

You want to use the status condition for your second condition - I imagine what you want is if the sensor is off and the switch is off then turn the light on

 

You should have it read

OR

Control 'garage sensor is switched off'

and status 'kitchen' is not on

 

that will turn the light on if the sensor times out and AND kitchen is off.

 

if that's not what you want - it may be that you want

 

or status 'garage sensor' is off

and status kitchen is off

 

this will turn it on if both the sensor and the kitchen switch are off

 

Note that this will not pay any attention to the time of day. If you want to know if it's dark you could also query the motion sensor dusk/dawn sensor to check.

 

control ifs are only evaluated when they are sent by physically pressing the button and only if the right 'side' of the button is pressed - ie each time you press the on it will execute regardless of whether it is already on

 

status ifs are evaluated everytime it switches state. So if it turns on or off either the then or the else will happen. But if it's off and you send another off nothing will happen at all - likewise if you send an on and it's already on - nothing will happen at all.

'

 

an 'if control is switched on do this ELSE do that' will NEVER execute the 'else' part. To execute the 'else' part you'd need an 'if control is switched off' statement and put the actions in the THEN portion

 

 

 

mark

Posted

I see a number of problems here.

 

First, the "Control 'Garage-Sensor' is switched Off" condition is ORed with the Sunset to Sunrise condition. So, the Kitchen light will turn ON at Sunset - 30, regardless of whether the garage is opened or not. Is this intended? Also, anytime the garage opens during Sunset-30 to Sunrise, the light will turn on full because the conditions are ORed.

 

Second the condition "Control 'Kitchen' is not switched Off" is a control condition, not a status condition. This essentially means that no OFF command was received from the Kitchen switch concurrent with whatever other condition kicked off the program. What you want here is (I think) "Status 'Kitchen' is Off," in other words not On or dimmed.

 

If your intention is to turn the kitchen light ON whenever the garage door is opened between Sunset-30 to Sunrise as long as the Kitchen light is not already On or dimmed, then your program should look like:

 

IF

Control 'Garage-Sensor' is switched Off

And From Sunset - 30 minutes

To Sunrise (next day)

And Status 'Kitchen' is Off

 

Then

Set 'Kitchen' On

 

Else

- No Actions

Posted

...If your intention is to turn the kitchen light ON whenever the garage door is opened between Sunset-30 to Sunrise as long as the Kitchen light is not already On or dimmed, then your program should look like:

 

IF

Control 'Garage-Sensor' is switched Off

And From Sunset - 30 minutes

To Sunrise (next day)

And Status 'Kitchen' is Off

 

Then

Set 'Kitchen' On

 

Else

- No Actions

 

Well, that seems to work perfectly!

 

Thanks for the help. I was getting hung up on "CONTROL is not off", and obviously the and/or wasnt even close.

Guest
This topic is now closed to further replies.

×
×
  • Create New...