« Back to Product

Documentation

Reuse scripts

Warning

This is an example that can also be mapped with events. This article is more about System variables and reusability of script sections.

This example is about a corridor light control that is the same on all floors. If possible, this should not be programmed three times.

One possibility would be to create 3 scripts and then copy and paste the content of one script into the individual scripts. This is certainly possible for a quick fix. However, this also leads to a certain redundancy and if something needs to be improved at one point in the script, this does not have to be done once but three times.

As an example for code reusing, a script with the name "light control (xComfort, Corridor, Motion detector, Brightness control)" can be created. The name should be descriptive and include what systems, devices and status it uses.

The following code could be the content:

//If no movement - see "System Variables" documentation for meaning of $_IPS['VALUE']
    if(!$_IPS['VALUE']) {
        MXC_SwitchMode($lampID, false);
    }
    //If motion was detected
    else {
        //Only after sunset
        if(!GetValueBoolean($istTag)) {
            //If it is between 10:30 p.m. and 06:00 a.m 
|             if((time() > strtotime("22:30")) || (time() < strtotime("06:00"))) { |
               MXC_DimSet($lampID, 15);
            }
            else {
                MXC_DimSet($lampID, 50);
            }
        }
    }

This script must be called by a triggering event - here usually by one/several motion detectors. The light is then set to a different dimming level depending on the time of day. As soon as the motion detector no longer detects any movement and sends the FALSE impulse, the device switches off.

The logic is thus outsourced and only scripts have to be created that set the missing variables and are called by an event. Below is an example:

//Triggering of the event by motion detector variable!

    //Unique Device-ID
    $lampID = 54321 /*[ground floor\corridor\ceiling lamp]*/;
    //Day/night variable from the location control 
    $isDay = 56789 /*[IsDay]*/;

    includeScript(12345 /*[scenarios\light control]*/);

    //Function to insert script content
    function includeScript($scriptID) {
        $s = IPS_GetScript($scriptID);
        include($s['ScriptFile']);
    }

In this way, "function templates" can be created and then fed with the necessary IDs. In these scripts then are the IDs of the devices/variables for which IP-Symcon creates complete and meaningful names (provided the object tree structure was created properly).

If required, the script can also be adapted for other systems and the correct function can be called dynamically using the transferred InstanceID or the system-specific changes can be made.

Any questions?