What can we improve?

Documentation

(Edit)

How can I...?

Many of these scripts use special IP-Symcon functions.
The Command Reference/Module Reference entail further information on how exactly these functions work.

... switch on a device and switch it off again after 60 seconds
... get a list of module names including GUID
... configure an instance from PHP
... find out the remaining number of seconds of a ScriptTimer
... directly include a script by ID
... Output UpdateTime in a separate string variable (1 script - n variables)
... create a timer & variable from PHP
... download a file from the Internet
... load a folder recursively into the MediaPlayer playlist
... export a variable profile

... switch on a device and switch it off again after 60 seconds

if($_IPS['SENDER'] == "TimerEvent")
{
    //From function
    ...
 
    //Turn off the timer
    IPS_SetScriptTimer($_IPS['SELF'], 0);
} else {
    //To function
    ...
 
    //Turn on the timer
    IPS_SetScriptTimer($_IPS['SELF'], 60);
}

... get a list of module names and GUID

foreach(IPS_GetModuleList() as $mid)
{
    $m = IPS_GetModule($mid);
    echo $mid."=".$m['ModuleName']."\n";
}

... configure an instance from PHP

//Change property
WWWReader_SetPage($id,"http://www.google.com");
 
//Apply changes
IPS_ApplyChanges($id);
 
//Get new URL
WWWReader_UpdatePage($id);

... find out the remaining number of seconds of a ScriptTimer

echo GetTimeRemaining($_IPS['SELF']); //Find out by yourself
 
function GetTimeRemaining($id)
{
    $eid=@IPS_GetEventIDByName("ScriptTimer", $id);
    if($eid === false) {
        return -1;
    } else {
        $e=IPS_GetEvent($eid);
        if($e['NextRun'] == 0)
        {
            return -1;
        } else {
            return $e['NextRun'] - microtime(true);
        }
    }
}

... directly include a script by ID

//Include script with ID 14871
include(IPS_GetScriptFile(14871));

... Output UpdateTime in a separate string variable (1 script - n variables)

//Evaluate event
if($_IPS['SENDER'] != "Variable")
    return;
 
SetValue(CreateVariableIDByName($_IPS['VARIABLE'], 'Updated', 3), date("d.m.y H:i:s"));
 
function CreateVariableIDByName($id, $name, $type)
{
    $vid = @IPS_GetVariableIDByName($name, $id);
    if($vid===false) {
        $vid = IPS_CreateVariable($type);
        IPS_SetParent($vid, $id);
        IPS_SetName($vid, $name);
        IPS_SetInfo($vid, "This Variable was created by Script #".$_IPS['SELF']);
    }
    return $vid;
}

... create a timer & variable from PHP

When the script runs, it sets a timer that starts every six hours and then puts a variable with the time of day as a value between 0-3 into the variable.

//NOTE:
//~~~~~~~~
//This script sets itself up automatically when run
//
//- A variable is set depending on the time of day (0-3)
// 0 = 0-6
// 1 = 6-12
// 2 = 12-18
// 3 = 19-24
 
//-----------------------------------------------------------------------------
//From this point nothing needs to be changed
//-----------------------------------------------------------------------------
if($_IPS['SENDER'] == "Execute")
{
    $eventid = @IPS_GetEventIDByName("Timer", $_IPS['SELF']);
    if($eventid === false)
    {
        $eventid = IPS_CreateEvent(1); //Cyclic
        IPS_SetEventActive($eventid, true);
        IPS_SetName($eventid, "Timer");
        IPS_SetEventScript($eventid, $_IPS['SELF']);
        IPS_SetEventCyclic($eventid, 0, 0, 0, 0, 3, 6);
    }
    $variableid = @IPS_GetVariableIDByName("Daytime", $_IPS['SELF']);
    if($variableid === false)
    {
        $variableid = IPS_CreateVariable(1);
        IPS_SetName($variableid, "Daytime");
        IPS_SetParent($variableid, $_IPS['SELF']);
    }
}
SetValue(IPS_GetVariableIDByName("Daytime", $_IPS['SELF']), floor(date("H") / 6));

... download a file from the Internet

$remoteImage = "https://www.google.com/images/srpr/logo3w.png";
$localImage = IPS_GetKernelDir()."\\media\\image.jpg";
 
//Download
$content = @file_get_contents($remoteImage);
if((strpos($http_response_header[0], "200") === false))
{
    return;
}
 
//Save to computer
file_put_contents( $localImage, $content );

... load a folder recursively into the MediaPlayer playlist

function WAC_PlayDir($id, $dir)
{
    function ReadRecursive($dir, $subdir = "") {
        $result = Array();
        $files = scandir($dir."/".$subdir);
        foreach($files as $file)
        {
            if(($file != ".") && ($file != "..")) {
                if(is_dir($dir."/".$subdir."/".$file)) {
                    $res = ReadRecursive($dir, $subdir."/".$file);
                    $result = array_merge($res, $result);
                } else {
                    $filedir = $subdir."/".$file;
                    $filedir = substr($filedir, 1, strlen($filedir));
                    $result[] = $filedir;
                }
            }
        }
        return $result;
    }
 
    $allowed = Array("mp3", "wma");
    $files = ReadRecursive($dir);
 
    //Use PHP's random number generator
    //shuffle($files);
 
    WAC_ClearPlaylist($id);
    foreach($files as $file)
    {
        $ext = pathinfo($dir."/".$file, PATHINFO_EXTENSION);
        if(in_array(strtolower($ext), $allowed))
        {
            WAC_AddFile($id, $dir."/".$file);
        }
    }
    WAC_Play($id);
}

... export a variable profile

getVariableProfileCreationCode("~Temperature.FHT");
getVariableProfileCreationCode("~Temperature.FHT", "TemperatureTest");
 
// first function parameter: Profile name, second parameter (optional): new profile name
function getVariableProfileCreationCode ($profileName, $newProfileName = "")
{
    $profile = IPS_GetVariableProfile($profileName);
    if ($profile !== false)
    {
        $profileName = (strlen($newProfileName) > 0) ? $newProfileName : $profileName;
        echo 'IPS_CreateVariableProfile("'.$profileName.'", '.$profile['ProfileType'].');'."\n";
        echo 'IPS_SetVariableProfileText("'.$profileName.'", "'.$profile['Prefix'].'", "'.$profile['Suffix'].'");'."\n";
        echo 'IPS_SetVariableProfileValues("'.$profileName.'", '.$profile['MinValue'].', '.$profile['MaxValue'].', '.$profile['StepSize'].');'."\n";
        echo 'IPS_SetVariableProfileDigits("'.$profileName.'", '.$profile['Digits'].');'."\n";
        echo 'IPS_SetVariableProfileIcon("'.$profileName.'", "'.$profile['Icon'].'");'."\n";
        foreach ($profile['Associations'] as $association)
        {
            echo 'IPS_SetVariableProfileAssociation("'.$profileName.'", '.$association['Value'].', "'.$association['Name'].'", "'.$association['Icon'].'", '.$association['Color'].');'."\n";
        }
        echo "\n";
    }
}
Deutsche Webseite verfügbar
Go to cart
Any questions?