Programming with a While statement

A ‘While’ statement runs continuously in a loop until the evaluation is shown to be False (Boolean value). ‘While’ statements can be repeating ‘if’ statements also.

Caution: When programming with WHILE statements, the script must incorporate delays, or the script will run so fast that it will consume all available CPU resources and decrease system performance.

This example shows how to program a system to keep the air conditioning off while a window is open.

To program a While statement, follow this example:

  1. Make sure the thermostat and window contact sensor are configured in Composer.
  1. Click Programming.
  1. In the project tree, select Kitchen Window Contact Sensor to trigger an event.
  2. Select the When the Kitchen Window opens event. The event appears at the top of the Script pane.
  1. In the Device Actions pane, select Kitchen Window Contact Sensor and click on Loops.
  2. Select the the Kitchen Window is open loop.
  3. Click the loop icon next to 'While the <room>->Kitchen Window is open' and drag it into the Script pane.
  4. In Programming Controls, enter 5 seconds next to Delay and drag the Delay green arrow below the while loop in the Script pane to nest the delay below the while statement.
  5. In Device Actions, select the thermostat and click on Commands.
  6. Select Off in the Set HVAC Mode to selection.
  7. Click on the green arrow next to 'Set the system mode on <room>->Control4 Thermostat to Off' and drag the green arrow below the while statement to nest it below the delay.