PHP: Store switch states and values in variables
Purpose
Store switch states, device values etc in variables so they can be used in scripting or web design.
The complete code is available at https://github.com/Egregius/PHP-Custom-Floorplan-for-Domoticz
Dependencies - hardware / software / operating system
sudo apt-get install apache2 php5 php5-memcached sqlite3 php5-cli php5-dev make libsqlite3-0 libsqlite3-dev php5-sqlite php5-curl
This will install all nescessary packages to enable all functions of my scripts.
Domoticz Setup - switches, variables, version
Disable security for localhost 127.0.0.1
files/scripts
Create a subfolder 'secure' and protect it so it isn't accessible from the outside. In apache this can be done in the virtualhost file: basically we block everybody except localhost and the computer you use to develop.
<Directory /var/www/secure>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
order deny,allow
Deny from All
allow from 127.0.0.1
allow from 192.168.0.10
</Directory>
secure/functions.php
All values are stored in variables based on the name of the device in lowercase.
The name is prepended with 2 letters. First is the type of device, second the stored value.
Examples:
$Toutside = temperature value of device 'outside'
$TIoutside = idx
$TToutside = last timestamp
$Ddining = On/Off state of dimmer 'dining'
$Dleveldining = dim level
$DIdining = idx
$DTdining = last timestamp
$Rbathroom = Setpoint temperature of device 'bathroom'
$RIbathroom = idx
$RTbathroom = last timestamp
All non specified devices are starting with S (switch):
$Skitchen = state of kitchen light
$SIkitchen = idx
$STkitchen = last timestamp
Code:
<?php
$domoticzurl='http://127.0.0.1:8080/json.htm?';
$domoticz=json_decode(file_get_contents($domoticzurl.'type=devices&used=true&plan=2'),true);//I only get devices on roomplan 2. This way I only get nescassary devices and the speed is at it best.
if($domoticz){
foreach($domoticz['result'] as $dom) {
(isset($dom['Type'])?$Type=$dom['Type']:$Type='None'); //Some devices don't have all Type fields filled
(isset($dom['SwitchType'])?$SwitchType=$dom['SwitchType']:$SwitchType='None');
(isset($dom['SubType'])?$SubType=$dom['SubType']:$SubType='None');
$name=strtolower($dom['Name']); //Convert all switchnames to lowercase
if($Type=='Temp + Humidity'||$Type=='Temp'){${'T'.$name}=$dom['Temp'];${'TI'.$name}=$dom['idx'];${'TT'.$name}=strtotime($dom['LastUpdate']);}
else if($SwitchType=='Dimmer'){${'DI'.$name}=$dom['idx'];$dom['Status']=='Off'?${'D'.$name}='Off':${'D'.$name}='On';$dom['Status']=='Off'?${'Dlevel'.$name}=0:${'Dlevel'.$name}=$dom['Level'];${'DT'.$name}=strtotime($dom['LastUpdate']);}
else if($Type=='Usage'&&$dom['SubType']=='Electric') ${'P'.$name}=substr($dom['Data'],0,-5);
else if($Type=='Radiator 1'||$Type=='Thermostat') {${'RI'.$name}=$dom['idx'];${'R'.$name}=$dom['Data'];${'RT'.$name}=strtotime($dom['LastUpdate']);}
else {
if(substr($dom['Data'],0,2)=='On') ${'S'.$name}='On';
else if(substr($dom['Data'],0,3)=='Off') ${'S'.$name}='Off';
else if(substr($dom['Data'],0,4)=='Open') ${'S'.$name}='Open';
else ${'S'.$name}=$dom['Data'];${'SI'.$name}=$dom['idx'];${'ST'.$name}=strtotime($dom['LastUpdate']);${'SB'.$name}=$dom['BatteryLevel'];}
}
unset($domoticz,$dom);