Extracting status and measuring values with PHP
Requesting a status or measuring values of a Domoticz device
Sometimes it would be nice to extract data from Domoticz devices and use them within scripts to switch other devices depending on extracted values.
This wiki shows how to extract values from an URL request of Domoticz devices and how to store them in a $variable.
Generally an URL request is used like: [1] http://192.168.1.18:8080/json.htm?type=devices&rid=55
More information: [2] http://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s
Don't forget to use your IP-adress, port and device id (...rid=xx).
In this example the request is done on a thermometer/hygrometer and the browser shows:
{ "5MinuteHistoryDays" : 2, "DashboardType" : 0, "MobileType" : 1, "WindScale" : 3.600000143051147, "WindSign" : "km/h", "result" : [ { "AddjMulti" : 1.0, "AddjMulti2" : 1.0, "AddjValue" : 0.0, "AddjValue2" : 0.0, "BatteryLevel" : 100, "CustomImage" : 0, "Data" : "9.5 C, 70 %", "DewPoint" : "4.30", "Favorite" : 1, "HardwareID" : 1, "HardwareName" : "RFXCOM", "HaveTimeout" : false, "Humidity" : 70, "HumidityStatus" : "Wet", "ID" : "A30E", "LastUpdate" : "2014-02-16 14:18:20", "Name" : "T/H Norden", "Notifications" : "true", "SignalLevel" : 5, "SubType" : "Cresta, TFA TS34C", "Temp" : 9.50, "Timers" : "false", "Type" : "Temp + Humidity", "TypeImg" : "temperature", "Unit" : 4, "Used" : 1, "idx" : "55" } ], "status" : "OK", "title" : "Devices" }
In this example you will find the temperature behind the data "Temp" : 9.50 and the humidity behind "Humidity" : 70.
Now let us start to write a very short PHP script which will reqest the status and measuring values and show them in a better view.
PHP code first part of example 1
#!/usr/bin/php
<?php
$json_string = file_get_contents("http://192.168.1.18:8080/json.htm?type=devices&rid=55");
$parsed_json = json_decode($json_string, true);
$test_link = "/home/pi/domoticz/scripts/test_1.txt";
$test_data = fopen ($test_link, "w+");
fwrite ($test_data, print_R($parsed_json, TRUE));
fclose ($test_data);
?>
Save the PHP script in /home/pi/domotizc/scripts as extract.php and make it executable. Now from CLI (Putty) start the script by ./extract.php .
The txt file output
A file "test_1.txt" should be generated. It should look like this:
Array ( [5MinuteHistoryDays] => 2 [DashboardType] => 0 [MobileType] => 1 [WindScale] => 3.6000001430511 [WindSign] => km/h [result] => Array ( [0] => Array ( [AddjMulti] => 1 [AddjMulti2] => 1 [AddjValue] => 0 [AddjValue2] => 0 [BatteryLevel] => 100 [CustomImage] => 0 [Data] => 9.1 C, 71 % [DewPoint] => 4.12 [Favorite] => 1 [HardwareID] => 1 [HardwareName] => RFXCOM [HaveTimeout] => [Humidity] => 71 [HumidityStatus] => Wet [ID] => A30E [LastUpdate] => 2014-02-16 14:56:43 [Name] => T/H Norden [Notifications] => true [SignalLevel] => 6 [SubType] => Cresta, TFA TS34C [Temp] => 9.1 [Timers] => false [Type] => Temp + Humidity [TypeImg] => temperature [Unit] => 4 [Used] => 1 [idx] => 55 ) ) [status] => OK [title] => Devices
Now the most important thing for the next step is to understand the different "levels" or "keys" is this array.
This area is the "highest level":
[5MinuteHistoryDays] => 2 [DashboardType] => 0 [MobileType] => 1 [WindScale] => 3.6000001430511 [WindSign] => km/h [result] => Array [status] => OK [title] => Devices
This part is the next "lower level":
[0] => Array
Now we come to the "lowest level" in this array:
( [AddjMulti] => 1 [AddjMulti2] => 1 [AddjValue] => 0 [AddjValue2] => 0 [BatteryLevel] => 100 [CustomImage] => 0 [Data] => 9.1 C, 71 % [DewPoint] => 4.12 [Favorite] => 1 [HardwareID] => 1 [HardwareName] => RFXCOM [HaveTimeout] => [Humidity] => 71 [HumidityStatus] => Wet [ID] => A30E [LastUpdate] => 2014-02-16 14:56:43 [Name] => T/H Norden [Notifications] => true [SignalLevel] => 6 [SubType] => Cresta, TFA TS34C [Temp] => 9.1 [Timers] => false [Type] => Temp + Humidity [TypeImg] => temperature [Unit] => 4 [Used] => 1 [idx] => 55 )
In our example we would like to extract the temperature and the humidity. The "keys" are [Temp] and [Humidity]. This means, we want to get the value behind those "keys".
Now we complete our example code:
PHP code second part of example 1
#!/usr/bin/php <?php $json_string = file_get_contents("http://192.168.1.18:8080/json.htm?type=devices&rid=55"); $parsed_json = json_decode($json_string, true); $test_link = "/home/pi/domoticz/scripts/test_1.txt"; $test_data = fopen ($test_link, "w+"); fwrite ($test_data, print_R($parsed_json, TRUE)); fclose ($test_data); $parsed_json = $parsed_json['result'][0]; $temp = $parsed_json['Temp']; echo "Temperature ".$temp."C"."\n"; $parsed_json = json_decode($json_string, true); $parsed_json = $parsed_json['result'][0]; $hum = $parsed_json['Humidity']; echo "Humidity ".$hum."%"."\n"; ?>
The extracted values
After saving the enlarged code we start the script again from CLI (Putty) and should see something like following:
Temperature 9.2C
Humidity 72%
This means we have successfully extracted the values we would like to see. The temperature value is stored in the variable $temp and the humidity is stored in the variable $hum.
A more detailed look on the most important commands and the array structure
Now the important code line for this:
$parsed_json = $parsed_json['result'][0];
The key ['result'] is taken form the "highest level" and the key [0] from the "lower level" in this example. Now the code row which delivers us the requested value:
$temp = $parsed_json['Temp'];
As you see here the "key" ['Temp'] delivers the temperature value 9.2C .
Depending on the request it could be that an array has more than three "levels" or "keys" like this example but the system is always the same.
Second example -> a switch device
Now a second example using a Domoticz "SWITCH" device:
Array ( [5MinuteHistoryDays] => 2 [DashboardType] => 0 [MobileType] => 1 [WindScale] => 3.6000001430511 [WindSign] => km/h [result] => Array ( [0] => Array ( [AddjMulti] => 1 [AddjMulti2] => 1 [AddjValue] => 7200 [AddjValue2] => 0 [BatteryLevel] => 255 [CustomImage] => 9 [Data] => On [Favorite] => 1 [HardwareID] => 1 [HardwareName] => RFXCOM [HaveDimmer] => [HaveGroupCmd] => 1 [ID] => 67 [Image] => Generic [IsSubDevice] => [LastUpdate] => 2014-02-16 13:58:35 [Level] => 0 [LevelInt] => 0 [MaxDimLevel] => 0 [Name] => Lader [Notifications] => false [SignalLevel] => 7 [Status] => On [StrParam1] => [StrParam2] => [SubType] => ARC [SwitchType] => On/Off [SwitchTypeVal] => 0 [Timers] => true [Type] => Lighting 1 [TypeImg] => lightbulb [Unit] => 12 [Used] => 1 [UsedByCamera] => [idx] => 7 ) ) [status] => OK [title] => Devices
As we see the status [Status] of the device is "On". The following code does extract the status:
#!/usr/bin/php
<?php
$json_string = file_get_contents("http://192.168.1.18:8080/json.htm?type=devices&rid=7");
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['result'][0];
$stat = $parsed_json['Status'];
echo "Status ".$stat."\n";
?>
In Putty you will receive:
Status On
The status is stored in $stat .
This page will be continued with examples on weather underground requests delivering also arrays with a lot of data.