Summing several counters
Posted: Tuesday 25 April 2017 12:15
Hi, I have 3 energy meters (pulse counters) for three separate phases, how to make a virtual device that will show me the consumption of kWh for all three?
Code: Select all
<?php
$sumusage=0;
$sumtoday=0;
$domoticz=json_decode(file_get_contents('http://127.0.0.1:8080/json.htm?type=devices&plan=3'),true);
if($domoticz){
foreach($domoticz['result'] as $dom){
$usage=number_format(str_replace(' Watt','',$dom['Usage']),0);
$sumusage=$sumusage+$usage;
$today=number_format(str_replace(' kWh','',$dom['CounterToday']),3);
$sumtoday=$sumtoday+$today;
}
}
Udevice(516,0,$sumusage.';'.$sumtoday);
function Udevice($idx,$nvalue,$svalue){
echo file_get_contents('http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
}
Code: Select all
{
"ActTime" : 1493322772,
"ServerTime" : "2017-04-27 21:52:52",
"Sunrise" : "05:34",
"Sunset" : "20:18",
"result" : [
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 10.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"Counter" : "44.550 kWh",
"CounterToday" : "0.134 kWh",
"CustomImage" : 0,
"Data" : "44.550 kWh",
"Description" : "",
"Favorite" : 1,
"HardwareID" : 4,
"HardwareName" : "Virtualne przelaczniki",
"HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
"HardwareTypeVal" : 15,
"HaveTimeout" : false,
"ID" : "82344",
"LastUpdate" : "2017-04-27 21:52:43",
"Name" : "L1 GN kuchnia lazienka",
"Notifications" : "false",
"PlanID" : "2",
"PlanIDs" : [ 2 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "Counter Incremental",
"SwitchTypeVal" : 0,
"Timers" : "false",
"Type" : "General",
"TypeImg" : "counter",
"Unit" : 1,
"Used" : 1,
"ValueQuantity" : "",
"ValueUnits" : "",
"XOffset" : "0",
"YOffset" : "0",
"idx" : "344"
},
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 10.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"Counter" : "110.683 kWh",
"CounterToday" : "3.720 kWh",
"CustomImage" : 0,
"Data" : "110.683 kWh",
"Description" : "",
"Favorite" : 1,
"HardwareID" : 4,
"HardwareName" : "Virtualne przelaczniki",
"HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
"HardwareTypeVal" : 15,
"HaveTimeout" : false,
"ID" : "82364",
"LastUpdate" : "2017-04-27 21:52:49",
"Name" : "L2 Gniazdka old",
"Notifications" : "false",
"PlanID" : "2",
"PlanIDs" : [ 2 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "Counter Incremental",
"SwitchTypeVal" : 0,
"Timers" : "false",
"Type" : "General",
"TypeImg" : "counter",
"Unit" : 1,
"Used" : 1,
"ValueQuantity" : "",
"ValueUnits" : "",
"XOffset" : "0",
"YOffset" : "0",
"idx" : "364"
},
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 10.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"Counter" : "6.322 kWh",
"CounterToday" : "1.960 kWh",
"CustomImage" : 0,
"Data" : "6.322 kWh",
"Description" : "",
"Favorite" : 1,
"HardwareID" : 4,
"HardwareName" : "Virtualne przelaczniki",
"HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
"HardwareTypeVal" : 15,
"HaveTimeout" : false,
"ID" : "82365",
"LastUpdate" : "2017-04-27 21:51:55",
"Name" : "L3 Swiatlo",
"Notifications" : "false",
"PlanID" : "2",
"PlanIDs" : [ 2 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "Counter Incremental",
"SwitchTypeVal" : 0,
"Timers" : "false",
"Type" : "General",
"TypeImg" : "counter",
"Unit" : 1,
"Used" : 1,
"ValueQuantity" : "",
"ValueUnits" : "",
"XOffset" : "0",
"YOffset" : "0",
"idx" : "365"
}
],
"status" : "OK",
"title" : "Devices"
}
Code: Select all
root@raspberrypi:~# php /home/pi/domoticz/scripts/php/energia.php
{
"status" : "OK",
"title" : "Update Device"
}
Yes, that's why I did #ropske wrote:Seems you have only 'CounterToday' and no 'Usage'
is this correct?
Code: Select all
if($domoticz){
foreach($domoticz['result'] as $dom){
# $usage=number_format(str_replace(' Watt','',$dom['Usage']),0);
# $sumusage=$sumusage+$usage;
$today=number_format(str_replace(' kWh','',$dom['CounterToday']),3);
$sumtoday=$sumtoday+$today;
}
Code: Select all
<?php
$sumtoday=0;
$domoticz=json_decode(file_get_contents('http://127.0.0.1:8080/json.htm?type=devices&plan=3'),true);
if($domoticz){
foreach($domoticz['result'] as $dom){
$today=number_format(str_replace(' kWh','',$dom['CounterToday']),3);
$sumtoday=$sumtoday+$today;
}
}
Udevice(516,0,$sumtoday);
function Udevice($idx,$nvalue,$svalue){
echo file_get_contents('http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
}
Code: Select all
<?php
$sumtoday=0;
$domoticz=json_decode(file_get_contents('http://127.0.0.1:8080/json.htm?type=devices&plan=3'),true);
if($domoticz){
foreach($domoticz['result'] as $dom){
$today=number_format(str_replace(' kWh','',$dom['CounterToday']),3);
lg('$today: '.$today);
$sumtoday=$sumtoday+$today;
}
}
lg('$sumtoday: '.$sumtoday);
Udevice(516,0,$sumtoday);
function Udevice($idx,$nvalue,$svalue){
echo file_get_contents('http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
function lg($msg){file_get_contents('http://127.0.0.1:8080/json.htm?type=command¶m=addlogmessage&message='.urlencode('=> '.$msg));}
}
Code: Select all
<?php
$sumtoday=0;
$domoticz=json_decode(file_get_contents('http://192.168.1.247:8080/json.htm?type=devices&plan=2'),true);
if($domoticz){
foreach($domoticz['result'] as $dom){
$today=number_format(str_replace(' kWh','',$dom['CounterToday']),3);
$sumtoday=$sumtoday+$today;
}
}
Udevice(372,0,$sumtoday);
function Udevice($idx,$nvalue,$svalue){
echo file_get_contents('http://192.168.1.247:8080/json.htm?type=command¶m=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
}
Code: Select all
<?php
$sumusage=0;
$domoticz=json_decode(file_get_contents('http://192.168.1.247:8080/json.htm?type=devices&plan=3'),true);
if($domoticz){
foreach($domoticz['result'] as $dom){
$usage=number_format(str_replace(' Watt','',$dom['Usage']),0);
$sumusage=$sumusage+$usage;
}
}
Udevice(371,0,$sumusage);
function Udevice($idx,$nvalue,$svalue){
echo file_get_contents('http://192.168.1.247:8080/json.htm?type=command¶m=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
}
Code: Select all
pi@raspberrypi:~ $ php /home/pi/domoticz/scripts/php/energia2.php
PHP Notice: Undefined index: Usage in /home/pi/domoticz/scripts/php/energia2.php on line 6
PHP Warning: number_format() expects parameter 1 to be double, string given in /home/pi/domoticz/scripts/php/energia2.php on line 6
PHP Notice: Undefined index: Usage in /home/pi/domoticz/scripts/php/energia2.php on line 6
PHP Warning: number_format() expects parameter 1 to be double, string given in /home/pi/domoticz/scripts/php/energia2.php on line 6
PHP Notice: Undefined index: Usage in /home/pi/domoticz/scripts/php/energia2.php on line 6
PHP Warning: number_format() expects parameter 1 to be double, string given in /home/pi/domoticz/scripts/php/energia2.php on line 6
{
"status" : "OK",
"title" : "Update Device"
}
pi@raspberrypi:~ $
Code: Select all
{
{
"ActTime" : 1493326066,
"ServerTime" : "2017-04-27 22:47:46",
"Sunrise" : "05:34",
"Sunset" : "20:18",
"result" : [
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"CustomImage" : 0,
"Data" : "588.0 Watt",
"Description" : "",
"Favorite" : 1,
"HardwareID" : 4,
"HardwareName" : "Virtualne przelaczniki",
"HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
"HardwareTypeVal" : 15,
"HaveTimeout" : false,
"ID" : "82374",
"LastUpdate" : "2017-04-27 22:43:23",
"Name" : "L2 watt old GN",
"Notifications" : "false",
"PlanID" : "3",
"PlanIDs" : [ 3 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "Electric",
"Timers" : "false",
"Type" : "Usage",
"TypeImg" : "current",
"Unit" : 1,
"Used" : 1,
"XOffset" : "0",
"YOffset" : "0",
"idx" : "374"
},
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"CustomImage" : 0,
"Data" : "0.0 Watt",
"Description" : "",
"Favorite" : 1,
"HardwareID" : 4,
"HardwareName" : "Virtualne przelaczniki",
"HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
"HardwareTypeVal" : 15,
"HaveTimeout" : false,
"ID" : "82388",
"LastUpdate" : "2017-04-27 22:43:02",
"Name" : "L1 Watt GN kuchnia lazienka",
"Notifications" : "false",
"PlanID" : "3",
"PlanIDs" : [ 3 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "Electric",
"Timers" : "false",
"Type" : "Usage",
"TypeImg" : "current",
"Unit" : 1,
"Used" : 1,
"XOffset" : "0",
"YOffset" : "0",
"idx" : "388"
},
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"CustomImage" : 0,
"Data" : "0.0 Watt",
"Description" : "",
"Favorite" : 1,
"HardwareID" : 4,
"HardwareName" : "Virtualne przelaczniki",
"HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
"HardwareTypeVal" : 15,
"HaveTimeout" : false,
"ID" : "82389",
"LastUpdate" : "2017-04-27 22:43:12",
"Name" : "L3 watt Swiatlo",
"Notifications" : "false",
"PlanID" : "3",
"PlanIDs" : [ 3 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "Electric",
"Timers" : "false",
"Type" : "Usage",
"TypeImg" : "current",
"Unit" : 1,
"Used" : 1,
"XOffset" : "0",
"YOffset" : "0",
"idx" : "389"
}
],
"status" : "OK",
"title" : "Devices"
}
Code: Select all
$usage=number_format(str_replace(' Watt','',$dom['Usage']),0);
Code: Select all
$usage=number_format(str_replace(' Watt','',$dom['Data']),0);
Of Course, thank you.ropske wrote:replace:Code: Select all
$usage=number_format(str_replace(' Watt','',$dom['Usage']),0);
with:and try againCode: Select all
$usage=number_format(str_replace(' Watt','',$dom['Data']),0);
Code: Select all
$today=number_format(str_replace(' kWh','',$dom['CounterToday']),3);
Code: Select all
pi@raspberrypi:~ $ php /home/pi/domoticz/scripts/php/energia.php
PHP Fatal error: Call to undefined function lg() in /home/pi/domoticz/scripts/php/energia.php on line 7
Code: Select all
function lg($msg){file_get_contents('http://192.168.2.2:8080/json.htm?type=command¶m=addlogmessage&message='.urlencode('=> '.$msg));}