Summing several counters
Moderators: leecollings, remb0
-
- Posts: 37
- Joined: Friday 25 December 2015 0:19
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Contact:
Summing several counters
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?
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Summing several counters
Create a time script where you update a virtual device with the sum of the others.
Code could be lua, php, bash, whatever you like.
If you like to go with PHP I can help you or even provide a example.
Code could be lua, php, bash, whatever you like.
If you like to go with PHP I can help you or even provide a example.
-
- Posts: 37
- Joined: Friday 25 December 2015 0:19
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Contact:
Re: Summing several counters
I'm programing noob, help will be welcome.
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Summing several counters
This should work:
Place the devices that you want a total of in a roomplan.
Adjust the roomplan (and ip:port) in de $domoticz= line
Adjust the idx in the Udevice call.
Execute the script every minute (or every x seconds) from cron.
If you like to be adventurous and start with my Pass2php script you wouldn't need cron for this and could have a total in realtime.
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);
}
Adjust the roomplan (and ip:port) in de $domoticz= line
Adjust the idx in the Udevice call.
Execute the script every minute (or every x seconds) from cron.
If you like to be adventurous and start with my Pass2php script you wouldn't need cron for this and could have a total in realtime.
-
- Posts: 37
- Joined: Friday 25 December 2015 0:19
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Contact:
Re: Summing several counters
Thank You but something not working.
Thats what I change

Everything seems to work, but my virtual device with the sum, all the time = 0
My roomplan
When I run manualy php script
Maybe do I need make some special dummy switch?
Thanks again
Thats what I change

Everything seems to work, but my virtual device with the sum, all the time = 0
My roomplan
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"
}
Thanks again
-
- Posts: 483
- Joined: Tuesday 12 August 2014 5:37
- Target OS: Raspberry Pi / ODroid
- Domoticz version: V3_8394
- Location: Rumbeke,Belgium
- Contact:
Re: Summing several counters
Seems you have only 'CounterToday' and no 'Usage'
is this correct?
is this correct?
-
- Posts: 37
- Joined: Friday 25 December 2015 0:19
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Contact:
Re: Summing several counters
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;
}
Last edited by tuspam on Thursday 27 April 2017 22:08, edited 1 time in total.
-
- Posts: 483
- Joined: Tuesday 12 August 2014 5:37
- Target OS: Raspberry Pi / ODroid
- Domoticz version: V3_8394
- Location: Rumbeke,Belgium
- Contact:
Re: Summing several counters
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);
}
-
- Posts: 483
- Joined: Tuesday 12 August 2014 5:37
- Target OS: Raspberry Pi / ODroid
- Domoticz version: V3_8394
- Location: Rumbeke,Belgium
- Contact:
Re: Summing several counters
so you will only sending the sum of the 3 Kwh devices to your 'new virtual device' and not Kwh and Watt
maybe I'm also wrong
But i learned alot from Egregius his Pass2php script
maybe I'm also wrong

But i learned alot from Egregius his Pass2php script
-
- Posts: 483
- Joined: Tuesday 12 August 2014 5:37
- Target OS: Raspberry Pi / ODroid
- Domoticz version: V3_8394
- Location: Rumbeke,Belgium
- Contact:
Re: Summing several counters
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));}
}
-
- Posts: 483
- Joined: Tuesday 12 August 2014 5:37
- Target OS: Raspberry Pi / ODroid
- Domoticz version: V3_8394
- Location: Rumbeke,Belgium
- Contact:
Re: Summing several counters
the function lg will log the values to domoticz log file, so can debug it
-
- Posts: 37
- Joined: Friday 25 December 2015 0:19
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Contact:
Re: Summing several counters
In my case i got 2 roomplans, first with kWh and second with Watt usage.
only kWh in my case is now:
Only Watt usage:
First script work i think correctly (i must check this more carefully) but second with Watt only i have error
This is my roomplan only Watt (idx3)
only kWh in my case is now:
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"
}
-
- Posts: 483
- Joined: Tuesday 12 August 2014 5:37
- Target OS: Raspberry Pi / ODroid
- Domoticz version: V3_8394
- Location: Rumbeke,Belgium
- Contact:
Re: Summing several counters
replace:
with:
and try again 
Code: Select all
$usage=number_format(str_replace(' Watt','',$dom['Usage']),0);
with:
Code: Select all
$usage=number_format(str_replace(' Watt','',$dom['Data']),0);

-
- Posts: 37
- Joined: Friday 25 December 2015 0:19
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Contact:
Re: Summing several counters
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);

Last edited by tuspam on Thursday 27 April 2017 23:13, edited 1 time in total.
-
- Posts: 483
- Joined: Tuesday 12 August 2014 5:37
- Target OS: Raspberry Pi / ODroid
- Domoticz version: V3_8394
- Location: Rumbeke,Belgium
- Contact:
Re: Summing several counters
you're welcome 

-
- Posts: 37
- Joined: Friday 25 December 2015 0:19
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Contact:
Re: Summing several counters
kWh count not sum data correctly
What mean 3 on the end this line ?
lg function not work for me:
after add lg('$today: '.$today);
Code: Select all
$today=number_format(str_replace(' kWh','',$dom['CounterToday']),3);
lg function not work for me:
after add lg('$today: '.$today);
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
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Summing several counters
lg is a function of pass2php to log a message in the Domoticz log:
The 3 at the end is a parameter of the number_format function defining the number of decimals. 1.23456789 becomes 1.235 with that.
I think that when you add both watt and kWh devices in one roomplan it must be possible to also make a sum of the kWh.
You could then post both numbers to one dummy device.
Code: Select all
function lg($msg){file_get_contents('http://192.168.2.2:8080/json.htm?type=command¶m=addlogmessage&message='.urlencode('=> '.$msg));}
I think that when you add both watt and kWh devices in one roomplan it must be possible to also make a sum of the kWh.
You could then post both numbers to one dummy device.
-
- Posts: 37
- Joined: Friday 25 December 2015 0:19
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Contact:
Re: Summing several counters
Ok, before i go forward i need run pass2PHP
Edit: tempolary moved to pass2PHP viewtopic.php?f=64&t=12343&p=131489#p131489
Edit: tempolary moved to pass2PHP viewtopic.php?f=64&t=12343&p=131489#p131489
Last edited by tuspam on Friday 28 April 2017 14:07, edited 3 times in total.
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Summing several counters
It's not required to use pass2php for this. But once you start with php you can't stop 
For pass2php questions: viewtopic.php?f=64&t=12343
For this, you could just add the lg function to your script.

For pass2php questions: viewtopic.php?f=64&t=12343
For this, you could just add the lg function to your script.
Re: Summing several counters
Nice script. Yesterday I was very happy with it. Now it seems the script cannot add up if the individual values exceed 1000. Any thoughts on this?
Verstuurd vanaf mijn TA-1021 met Tapatalk
Verstuurd vanaf mijn TA-1021 met Tapatalk
Who is online
Users browsing this forum: No registered users and 1 guest