ISY-99i/ISY-26 INSTEON:Scope, Precedence and Execution Order

From Universal Devices, Inc. Wiki
Jump to: navigation, search

Scope

All programs and folders have global scope, i.e. they are visible to all other programs and folders.


Operator Precedence

A program's If clause may contain more than one condition. Every condition (including schedule conditions) begins with an operator, which is one of And or Or. The precedence of these operators is:

  • And
  • Or

Therefore an expression such as If A or B or C and D would be evaluated as If A or B or (C and D).

The order of evaluation may be modified by use of And ( .. ) and Or ( .. ) parenthesis. Any number of conditions may be placed between the parenthesis, and will be evaluated in precedence order. The entire parenthesized group then becomes a single And or Or condition, to be evaluated in precedence order with other same-level conditions (including other parenthesized groups). To change the above example to If (A or B or C) and D, use an And ( .. ) parenthesis around A, B, and C, with D outside the parenthesis.

In the following example code of the above example:

If
        Status  'Light 1' is On
     Or Status  'Light 2' is On
     Or Status  'Light 3' is On
    And Control 'Switch 1' is switched On

Then
        Set 'Light 4' On

Else
   - No Actions - (To add one, press 'Action')

Light 4 will be turned on whenever Light 1 is on, or Light 2 is on, or Switch 1 is switched on while Light 3 is on, which is likely not the intended result. To have Light 4 turn on whenever Switch 1 is switched on and either Light 1 or Light 2 or Light 3 is on, add an And ( .. ) parenthesis:

If
        (
             Status  'Light 1' is On
          Or Status  'Light 2' is On
          Or Status  'Light 3' is On
        )
    And Control 'Switch 1' is switched On

Then
        Set 'Light 4' On

Else
   - No Actions - (To add one, press 'Action')


The Or ( .. ) parenthesis are less frequently used. Consider the expression If A or ( .. ). If the expression within the Or ( .. ) parenthesis consists only of single operands connected by And and/or Or operators, then the Or ( .. ) parenthesis do not alter the order of evaluation, and are not required. In order for them to be required, they must be part of a compound expression as, for example, If A or (B and C or D) and E.

That example might have the requirement to turn Light 4 on if Switch 1 is turned on, or if either Switch 2 is on and Light 2 is on or Light 3 is on and Switch 3 is turned on:

If
        Control 'Switch 1' is switched On
     Or (
             Status  'Switch 2' is On
         And Status  'Light 2' is On
          Or Status  'Light 3' is On
        )
    And Control 'Switch 3' is switched On

Then
        Set 'Light 4' On

Else
   - No Actions - (To add one, press 'Action')


As a final example, the requirement is to have Light 3 turn on if Light 1 is on and Switch 1 is switched on, or Light 2 is on and Switch 2 is switched on: If (A and B) or (C and D). Since And has higher precedence than Or, the expression If A and B or C and D will bind as required, and therefore the following code needs no parenthesis:

If
        Status  'Light 1' is On
    And Control 'Switch 1' is switched On
     Or Status  'Light 2' is On
    And Control 'Switch 2' is switched On

Then
        Set 'Light 3' On

Else
   - No Actions - (To add one, press 'Action')

If this appears confusing, Or ( .. ) parenthesis may be used to clarify, without changing the order of evaluation:

If
        (
             Status  'Light 1' is On
         And Control 'Switch 1' is switched On
        )
     Or (
             Status  'Light 2' is On
         And Control 'Switch 2' is switched On
        )

Then
        Set 'Light 3' On

Else
   - No Actions - (To add one, press 'Action')


Note that no operator (And or Or) is displayed for the first condition in the If clause. This is because a single-condition clause has no conjunction, while in a multi-condition clause, the operation performed on the first condition is wholly dependent upon and determined by the operator of the second condition. The operator of the first condition, though ignored, is still maintained in the drop-down setting box in the lower portion of the window, in case the condition line should be moved down and no longer be the first condition.


Evaluation Order

Within the If clause of a program, expressions are evaluated from left-to-right, meaning the individual expressions are evaluated from top to bottom as viewed on the screen. Evaluation does not stop as soon as the outcome is known; rather all expressions within the clause are evaluated each time the If is invoked.


Statement Execution Order

Within the Then or Else clause of a program, statements are executed from top to bottom in the order in which they occur. When a statement calls another program, the called program begins executing, and the calling program immediately continues execution with the next statement in sequence--it does not wait for the called program to complete before continuing.

A series of statements within a Then clause (or within an Else clause), up to the next Wait or Repeat statement, are atomic. In other words, all such statements are executed before the conditions of the program are retested. The program's conditions are reevaluated each time a Wait or Repeat statement is encountered, and at the end of each iteration of a Repeat loop.

What this means is that if a program's Then clause changes a condition which causes the program's overall condition to become false (or if the program's Else clause changes a condition which causes the program's overall condition to become true), the current atomic statement group will complete, and at that point execution will transfer from the Then clause (or the Else clause) to the Else clause (or the Then clause).

Therefore, if a Then clause (or an Else clause) contains no Wait or Repeat statements, the entire clause is atomic, and will complete before the program's conditions are reevaluated.


Boot/Startup Sequence and Program Execution Order

When the ISY boots, the True/False state of all schedule programs is determined, and any schedules which require catching up, are set to run following initialization. If Catch up schedules at Restart is enabled, schedules are set to run from the beginning of the day; otherwise schedules are set to run only for the specified Missed Schedule Grace Period. Note that prior to firmware 2.6.4, the Missed Schedule Grace Period applied only to Time is schedules, not From/To or From/For schedules; as of firmware 2.6.4 it applies to all schedules.

Next, ISY queries all devices and determines their status. During this process, the True/False state is determined for programs and folders which have conditions, and any programs which require running are set to run following initialization. Folders with no conditions are set to True so as not to prevent programs within them from running, since without conditions a folder's state will never be changed. Programs which have no conditions are initialized to False, just as a variable would be, since programs, which are often used as flags, may have their state changed by other programs or directly from the Program Summary tab, even though they have no conditions.

Finally, programs which are set to Run at Startup are set to run following initialization. Note that prior to firmware 2.6.6, a program which was set to Run at Startup would actually execute at startup only if its Enabled box was checked; as of firmware 2.6.6 it will run at startup when set to do so, regardless of the state of the Enabled box.

At this point initialization is complete, and event processing begins. All programs which were set during initialization to run, whether schedules, triggers, or Run at Startup, now begin running, and programs initiated by new events will also begin running. In all cases, the order in which the programs run is determined by ISY's internal algorithms, and is not user predictable; specifically, schedules do not necessarily run in chronological order.


Program Enabled Checkbox

Each program has an Enabled checkbox. When this box is checked, and provided any folder conditions in the program's hierarchy are True, the program will run normally whenever triggered by an event, when called from another program, when invoked directly from the Program Summary tab, and at startup if it is set to Run at Startup.

When the Enabled box is not checked, the program will not run in response to an event, but will still run normally when called from another program and when invoked directly from the Program Summary tab, provided any folder conditions in the program's hierarchy are True.

In firmware versions prior to 2.6.6, a program set to Run at Startup would not actually execute at startup when its Enabled box was not checked. In firmware 2.6.6 and above, a program set to Run at Startup will do so regardless of the state of its Enabled checkbox, provided any folder conditions in the program's hierarchy are True.


Folder Conditions

Folders may be created with or without conditions, and may be nested. Folders with no conditions are an effective organizational tool, and programs within them run as described above.

When a folder is given conditions, then programs within that folder or any of its sub-folders run only when the folder's conditions are met (the folder's status is True). Whether invoked by a trigger or schedule, by another program, by Run at Startup, or directly from the Program Summary tab, a program will run only when the conditions are True for each folder within the program's hierarchy. Therefore folder conditions may be considered to have the highest level of precedence.

When a folder's conditions become True, programs within that folder or its sub-folders do not automatically run, unless the event which caused the folder to become True is also a condition of one or more programs within that folder, in which case those programs will run either the Then or Else path, depending on whether the program's conditions are True or False. Except for that special case, programs within conditional folders do not run until an event which is a condition of the program occurs after the folder becomes True. When the event occurs, the program's conditions are re-evaluated, and since the folder is now True, the program runs either the Then or Else path, depending on whether the program's conditions are True or False. Note that this does not require that the program's conditions change from False to True or vice versa, only that an event occurs which causes the program's conditions to be re-evaluated. As of firmware version 2.6.6, the True/False state of a folder may be tested within the If conditions of a program or another folder. This allows a program's If conditions to be automatically re-evaluated when a folder becomes True, by placing the test of the folder condition within the program's If conditions.




ISY-26 INSTEON / ISY-99i Series INSTEON : How-To Guide