how to add decimals to power values ​​in the metern dashboard

Talks about metering
Post Reply
maxsemp
Posts: 6
Joined: Thu Oct 17, 2019 1:06 pm

how to add decimals to power values ​​in the metern dashboard

Post by maxsemp » Thu Oct 17, 2019 1:18 pm

Hi Jeanmarc.
I'm a new user of the forum. I would like to know, if possible, how you can add decimals in the power values ​​in the metern dashboard. Thanks.

jeanmarc
Posts: 2134
Joined: Thu Aug 29, 2013 7:16 am

Re: how to add decimals to power values ​​in the metern dashboard

Post by jeanmarc » Thu Oct 17, 2019 7:57 pm

hi,
Sure, it depend of what your com app return eg :

Code: Select all

[root@i3 ~]# houseenergy -power
elect(322.6*W)

maxsemp
Posts: 6
Joined: Thu Oct 17, 2019 1:06 pm

Re: how to add decimals to power values ​​in the metern dashboard

Post by maxsemp » Fri Oct 18, 2019 12:59 pm

I use poolerconsumi, which gives me this result:
root@raspberry:/var/www/comapps/poolerconsumi 1 power
1(243*W)
without decimals.

Code: Select all

#!/usr/bin/php
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
// This script will output a meterN compatible format for the main command
// You'll need to setup the path to meterN ($pathtomn). 
// ln -s /var/www/comapps/poolerconsumi.php /usr/local/bin/poolerconsumi
// Usage:  poolerconsumi [MeterID] [energy | power]

// No edit is needed below
if (!isset($argv[1],$argv[2])) {
$argv[1]=null;
}

if ($argv[1]!= null && ($argv[2]== 'power' || $argv[2]== 'energy')) {
    
	$pathtomn = '/var/www/metern';
	$prevcount = 0; // Inserire qui il totale del contatore precedente
	$metnum = $argv[1]; // Meter ID Consumi

	$cmd = "more /dev/shm/metern$metnum.txt | egrep \"^$metnum\(\" | grep \"*Wh)\""; // Request Energy values
	$cmd1 = "more /dev/shm/metern$metnum.txt | egrep \"^$metnum\(\" | grep \"*W)\""; // Request Power values
    #echo "$cmd\n";
    #echo "$cmd1\n";

	// End of setup

	define('checkaccess', TRUE);
	include("$pathtomn/config/config_main.php");
	include("$pathtomn/config/config_met$metnum.php");
    
    if ($argv[2]== 'energy') {
        // acquisisce il valore precedente dal csv
        $dir    = '/var/www/metern/data/csv';
        $output = array();
        $output = glob($dir . '/*.csv');
        sort($output);
        $cnt = count($output);
            
        if (file_exists($output[$cnt - 1])) {
            $file       = file($output[$cnt - 1]); // today
            $contalines = count($file);

            if ($contalines > 1) {
                $prevarray = preg_split("/,/", $file[$contalines - 1]);
                
            } elseif ($contalines == 1 && file_exists($output[$cnt - 2])) { // yesterday, only header
                $file       = file($output[$cnt - 2]);
                $contalines = count($file);
                $prevarray = preg_split("/,/", $file[$contalines - 1]);
            }
            $cons_val_first = trim($prevarray[$metnum]);
        } else {
            $cons_val_first = null;
        }      

        #sleep(1); // oh why ?
        // Now retrieve the current value
        $datareturn = shell_exec($cmd);
        $datareturn = trim($datareturn);
        $datareturn = preg_replace("/^${'ID'.$metnum}\(/i", '', $datareturn); // VALUE*UNIT)
        $lastval    = preg_replace("/\*[a-z0-9]+\)$/i", '', $datareturn); // VALUE
        #echo "$lastval\n";

        settype($lastval, 'float');
        settype($prevcount, 'float');
        settype($cons_val_first, 'float');

        $lastval += $prevcount; // aggiunge il correttore del totale

        if ($lastval < $cons_val_first) { // controlla se il contatore segna meno del valore precedente
            $lastval = $cons_val_first;
        }
        $lastval = round($lastval, ${'PRECI' . $metnum});
        $str     = utf8_decode("${'ID'.$metnum}($lastval*${'UNIT'.$metnum})\n");
        file_put_contents("/dev/shm/consumi$metnum.txt", $str);
        echo "$str";
    
    }elseif ($argv[2]== 'power') {
        #sleep(1); // oh why ?
        // Now retrieve the current value
        $datareturn = shell_exec($cmd1);
        $datareturn = trim($datareturn);
        $datareturn = preg_replace("/^${'ID'.$metnum}\(/i", '', $datareturn); // VALUE*UNIT)
        $powerval   = preg_replace("/\*[a-z0-9]+\)$/i", '', $datareturn); // VALUE
        #echo "$powerval\n";

        settype($powerval, 'float');

        $powerval = round($powerval, 0);
        $str     = utf8_decode("${'ID'.$metnum}($powerval*${'LIVEUNIT'.$metnum})\n");
        echo "$str";
    }
    
} else {
    die("Usage: poolerconsumi {MeterID} {energy | power}\n");
}
?>
What should I change in this code to have at least one decimal? Thanks.

jeanmarc
Posts: 2134
Joined: Thu Aug 29, 2013 7:16 am

Re: how to add decimals to power values ​​in the metern dashboard

Post by jeanmarc » Fri Oct 18, 2019 1:11 pm

What contain /dev/shm/meternxx.txt ? I think the value is rounded there, check the script that fill /dev/shm

maxsemp
Posts: 6
Joined: Thu Oct 17, 2019 1:06 pm

Re: how to add decimals to power values ​​in the metern dashboard

Post by maxsemp » Sat Oct 19, 2019 11:51 am

The metern1.txt file contains the following lines:
1(0.00*W)
1(2567166*Wh)
1_1(231.40*V)
1_2(0.00*A)
1_3(50.00*Hz)
1_4(1.00*F).
As you can see, the power value has two decimal places after the comma. So, in theory, they should also appear in the dashboard. Or not?
Bye and thanks for the answers. :D

jeanmarc
Posts: 2134
Joined: Thu Aug 29, 2013 7:16 am

Re: how to add decimals to power values ​​in the metern dashboard

Post by jeanmarc » Sat Oct 19, 2019 2:59 pm

/me stupid ;)
Change $powerval = round($powerval, 0); to $powerval = round($powerval, 2);

maxsemp
Posts: 6
Joined: Thu Oct 17, 2019 1:06 pm

Re: how to add decimals to power values ​​in the metern dashboard

Post by maxsemp » Sat Oct 19, 2019 5:28 pm

Hi Jeanmarc.
I made the change you suggested and it works! Thanks for your help! :lol: :lol: :lol:
Next time.

maxsemp
Posts: 6
Joined: Thu Oct 17, 2019 1:06 pm

Re: how to add decimals to power values ​​in the metern dashboard

Post by maxsemp » Tue Oct 22, 2019 1:29 pm

Hi Jeanmarc.
I would like to know, from where are taken the daily values ​​of the energy consumed in the dashboard. Thanks.

jeanmarc
Posts: 2134
Joined: Thu Aug 29, 2013 7:16 am

Re: how to add decimals to power values ​​in the metern dashboard

Post by jeanmarc » Tue Oct 22, 2019 4:14 pm

That script always took Wh values from csv and make the difference. :eh:
In fact, it seem useless to me as it make a virtual meter from a real one :wtf: Why not simply return values from /dev/shm/metern$metnum.txt ?

Also for a vitual meter : the data lag from 5" (reads past csv) and will miss values if the logger isn't started (missing lines in csv), that's why houseenergy & pooltot.php are better examples :thumbup:

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests