Hi,
At a high level you will need to run a script from cron that queries the toon, lets say every 5 minutes and inserts the values in domoticz.
- Code: Select all
#!/usr/bin/php
<?php
/* Synchronize samrt plug usage information from tToon to Domoticz */
/* 20-10-2018 YJB /
/* 15-11-2018 YJB Added logging */
/* 21/12/2018 YJB Cleaned up code */
/* Instructions:
- Create Domoticz dummy devices (Electric Instant & Counter), name should match plugname in Toon
- Make a note of the idx numbers and enter those
- Place script in Domoticz folder /home/pi/domoticz/scripts/toon/ or desired folder.
- Make sure Curl is installed (sudo apt-get install php-curl).
- Make sure script is executable (chmod +x filename.php).
- Create a new of using an existing crontab file in /etc/cron.d
Example entry (every 5 minutes): */
# */5 * * * * root /home/pi/domoticz/scripts/toon/smartplugs_usage.php > /dev/null 2>&1
/* Set IP addresses and idx*/
$IPDomoticz = '127.0.0.1';
$IPToon = 'xxx.xxx.xxx.xxx';
/* idx = array(idx_1, idx_2, idx_3, etc) */
$idx = array(764, 765, 766, 767, 820);
$date_time = date ('Y-m-d H:i');
/* Read the current values from Toon */
$file_string_ElecCounter = file_get_contents("http://$IPToon/hdrv_zwave?action=getDevices.json");
/* If you set the second parameter of json_decode to true, you get an array*/
$parsed_json_ElecCounter = json_decode($file_string_ElecCounter, true);
foreach ($idx as $Index) {
/* read last recorded total value from Domoticz */
$json_string = file_get_contents("http://$IPDomoticz/json.htm?type=devices&rid=$Index");
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['result'][0];
$Counter_Domoticz = $parsed_json['Data']*1000;
$Name_Domoticz = $parsed_json['Name'];
foreach($parsed_json_ElecCounter as $device){
$Counter_Toon = "Empty";
$Name_Toon = "Empty";
(isset($device['DeviceName']) && $Name_Toon = $device['DeviceName']);
if ($Name_Toon == $Name_Domoticz){
(isset($device['CurrentElectricityQuantity']) && $Counter_Toon = intval($device['CurrentElectricityQuantity']));
(isset($device['CurrentElectricityQuantity']) && $Current_Flow_Toon = intval($device['CurrentElectricityFlow']));
$ElecIncrement = $Counter_Toon-$Counter_Domoticz;
/* Debug Only*/
/* echo "-----------\n";
echo "Name reported by Domoticz: $Name_Domoticz \n";
echo "Total Stored in Domoticz = $Counter_Domoticz Wh\n";
echo "Name reported by Toon: $Name_Toon \n";
echo "Total reported by Toon = $Counter_Toon Wh\n";
echo "Current flow reported by Toon = $Current_Flow_Toon Wh\n";
echo "Calculated Increment: $ElecIncrement Wh \n";
*/
/* write new value to Domoticz */
if ($Counter_Domoticz == 0){ #this is for the first run only, when the idx is still empty.
$WriteInitValue = curl_init("http://$IPDomoticz/json.htm?type=command¶m=udevice&idx=$Index&nvalue=0&svalue=$Current_Flow_Toon;$Counter_Toon");
echo "\n";
echo "Initialize Counter \n";
curl_exec($WriteInitValue);
}
else if ($Counter_Toon < $Counter_Domoticz){
error_log("[$date_time] $Name_Domoticz Toon value lower ($ElecIncrement), do nothing. \n", 3, '/var/log/smartplugs_usage-error.log');
/* Debug Only*/
/* echo "Toon value lower ($ElecIncrement), do nothing. \n";
$WriteValue = curl_init("http://$IPDomoticz/json.htm?type=command¶m=udevice&idx=$Index&nvalue=0&svalue=$Current_Flow_Toon;$Counter_Toon");
curl_exec($WriteValue);
*/
}
else if ($ElecIncrement >= 2000){
echo "Toon value is larger than 2000, do nothing \n";
error_log("[$date_time] $Name_Domoticz Toon value is larger than 2000 ($ElecIncrement), Skipped\n", 3, '/var/log/smartplugs_usage-error.log');
}
else if ($Counter_Toon >= $Counter_Domoticz){
$WriteValue = curl_init("http://$IPDomoticz/json.htm?type=command¶m=udevice&idx=$Index&nvalue=0&svalue=$Current_Flow_Toon;$Counter_Toon");
echo "\n";
/* Debug Only*/
/*echo "Write new value \n";*/
/*error_log("[$date_time] $Name_Domoticz Write new value\n", 3, '/var/log/smartplugs_usage-error.log');*/
curl_exec($WriteValue);
}
else {
echo "Domoticz counter ($Counter_Domoticz) incorrect, do nothing \n";
echo "Toon value ($Counter_Toon) \n";
error_log("[$date_time] Domoticz counter ($Counter_Domoticz) incorrect, do nothing \n", 3, '/var/log/smartplugs_usage-error.log');
error_log("[$date_time] Toon counter ($Counter_Toon) \n", 3, '/var/log/smartplugs_usage-error.log');
}
}
}
}
?>
I used some snippets from other people, but can't remember who (and I'm not pretending this is mine) and added some stuff. I'm not a php programmer, so it can probably be done more efficient.
The problem is that if you unplug the plug and insert it again it will loose all the values and starts counting from scratch, there are probably ways of dealing with this, but I never bothered to do that