Summing several counters

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

tuspam
Posts: 37
Joined: Friday 25 December 2015 0:19
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Summing several counters

Post by tuspam »

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?
User avatar
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

Post by Egregius »

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.
tuspam
Posts: 37
Joined: Friday 25 December 2015 0:19
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Summing several counters

Post by tuspam »

I'm programing noob, help will be welcome.
User avatar
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

Post by Egregius »

This should work:

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&param=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
}
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.
tuspam
Posts: 37
Joined: Friday 25 December 2015 0:19
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Summing several counters

Post by tuspam »

Thank You but something not working.

Thats what I change
Image

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"
}
When I run manualy php script

Code: Select all

root@raspberrypi:~# php  /home/pi/domoticz/scripts/php/energia.php
{
   "status" : "OK",
   "title" : "Update Device"
}
Maybe do I need make some special dummy switch?

Thanks again
ropske
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

Post by ropske »

Seems you have only 'CounterToday' and no 'Usage'
is this correct?
tuspam
Posts: 37
Joined: Friday 25 December 2015 0:19
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Summing several counters

Post by tuspam »

ropske wrote:Seems you have only 'CounterToday' and no 'Usage'
is this correct?
Yes, that's why I did #

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.
ropske
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

Post by ropske »

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&param=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
}
try this
ropske
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

Post by ropske »

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
ropske
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

Post by ropske »

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&param=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
function lg($msg){file_get_contents('http://127.0.0.1:8080/json.htm?type=command&param=addlogmessage&message='.urlencode('=> '.$msg));}

}
ropske
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

Post by ropske »

the function lg will log the values to domoticz log file, so can debug it
tuspam
Posts: 37
Joined: Friday 25 December 2015 0:19
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Summing several counters

Post by tuspam »

In my case i got 2 roomplans, first with kWh and second with Watt usage.

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&param=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
}
Only Watt usage:

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&param=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue);
}
First script work i think correctly (i must check this more carefully) but second with Watt only i have error

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:~ $
This is my roomplan only Watt (idx3)

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"
}

ropske
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

Post by ropske »

replace:

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);
and try again ;)
tuspam
Posts: 37
Joined: Friday 25 December 2015 0:19
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Summing several counters

Post by tuspam »

ropske wrote:replace:

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);
and try again ;)
Of Course, thank you.

Image
Last edited by tuspam on Thursday 27 April 2017 23:13, edited 1 time in total.
ropske
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

Post by ropske »

you're welcome ;)
tuspam
Posts: 37
Joined: Friday 25 December 2015 0:19
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Summing several counters

Post by tuspam »

kWh count not sum data correctly

Code: Select all

  $today=number_format(str_replace(' kWh','',$dom['CounterToday']),3);
  
What mean 3 on the end this line ?

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
User avatar
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

Post by Egregius »

lg is a function of pass2php to log a message in the Domoticz log:

Code: Select all

function lg($msg){file_get_contents('http://192.168.2.2:8080/json.htm?type=command&param=addlogmessage&message='.urlencode('=> '.$msg));}
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.
tuspam
Posts: 37
Joined: Friday 25 December 2015 0:19
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Summing several counters

Post by tuspam »

Ok, before i go forward i need run pass2PHP

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.
User avatar
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

Post by Egregius »

It's not required to use pass2php for this. But once you start with php you can't stop :P
For pass2php questions: viewtopic.php?f=64&t=12343

For this, you could just add the lg function to your script.
nico4sen
Posts: 6
Joined: Saturday 15 February 2020 13:41
Target OS: -
Domoticz version:
Contact:

Re: Summing several counters

Post by nico4sen »

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

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest