PHP: Functions to control Domoticz

From Domoticz
Revision as of 09:13, 31 December 2015 by Egregius (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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&param=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&param=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&param=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&param=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&param=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&param=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&param=addlogmessage&message='.urlencode($msg));
}