PHP: Functions to control Domoticz
Purpose
Easily use functions to handle repetive tasks. Keeps the code clean.
Dependencies - hardware / software / operating system
files/scripts
The following functions can be added to secure/functions.php (see PHP: Store switch states and values in variables)
Schakel
Switches idx On or Off
function Schakel($idx,$cmd) {
global $domoticzurl;
$reply=json_decode(file_get_contents($domoticzurl.'type=command¶m=switchlight&idx='.$idx.'&switchcmd='.$cmd),true);
if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
return $reply;
}
Scene
Activates scene
function Scene($idx) {
global $domoticzurl;
$reply=json_decode(file_get_contents($domoticzurl.'type=command¶m=switchscene&idx='.$idx.'&switchcmd=On'),true);
if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
return $reply;
}
Dim
Sets a dimlevel to a dimmer
function Dim($idx,$level) {
global $domoticzurl;
if($level>0&&$level<100) $level=$level+1;
$reply=json_decode(file_get_contents($domoticzurl.'type=command¶m=switchlight&idx='.$idx.'=&switchcmd=Set%20Level&level='.$level),true);
if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
return $reply;
}
Udevice
Updates the nvalue and svalue of a device. Setpoints are set this way.
function Udevice($idx, $nvalue, $svalue) {
global $domoticzurl;
$reply=json_decode(file_get_contents($domoticzurl.'type=command¶m=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue),true);
if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
return $reply;
}
Textdevice
Sets a text to a textdevice
function Textdevice($idx,$text) {
global $domoticzurl;
$reply=json_decode(file_get_contents($domoticzurl.'type=command¶m=udevice&idx='.$idx.'&nvalue=0&svalue='.$text),true);
if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
return $reply;
}
percentdevice
Sets a percentage to a percentage device
function percentdevice($idx,$value) {
global $domoticzurl;$reply=json_decode(file_get_contents($domoticzurl.'type=command¶m=udevice&idx='.$idx.'&nvalue=0&svalue='.$value),true);
if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
return $reply;
}
domlog
Writes a text to the domoticz logfile.
function domlog($msg) {
global $domoticzurl;
file_get_contents($domoticzurl.'type=command¶m=addlogmessage&message='.urlencode($msg));
}