espeasy sonoff pulse counter Metern

megamarco83
Posts: 72
Joined: Tue Apr 14, 2015 9:25 am

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Mon Dec 17, 2018 3:46 pm

jeanmarc wrote:
Mon Dec 10, 2018 3:13 pm
ok ok, hard to debug like that. Your config is ok, try this

You'll see at line 121 how to get values from your json.
You cannot set a starting value yet, i have to find some times to tweak the script.
Ciao Jeanmarc
do you have time to debug the code? :)
i see that pulse counter inside espeasy if rebooted begin to zero the total counter. is it possible to store inside the php the total and update it every time that we read the espeasy total? in this way we prevent the lost data for a rebbot of esp, what do you think?
thanks

the structure of json is:

Code: Select all

pi@raspberrypi:~ $ curl http://192.168.0.204/json?tasknr=3
{
"TaskValues": [
{"ValueNumber":1,
"Name":"Count",
"NrDecimals":0,
"Value":0
},
{"ValueNumber":2,
"Name":"Total",
"NrDecimals":0,
"Value":0
},
{"ValueNumber":3,
"Name":"Time",
"NrDecimals":0,
"Value":0
}],
"TTL":30000,
"DataAcquisition": [
{"Controller":1,
"IDX":0,
"Enabled":"false"
},
{"Controller":2,
"IDX":0,
"Enabled":"false"
},
{"Controller":3,
"IDX":0,
"Enabled":"false"
}],
"TaskInterval":30,
"Type":"Generic - Pulse counter",
"TaskName":"gas",
"TaskEnabled":"true",
"TaskNumber":3

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Tue Dec 18, 2018 10:08 am

Hi,
That what it should do, see the line "surge or esp counter restart"

Code: Select all

#!/usr/bin/php
<?php
// A simple script to request a espeasy counter that doesn't always increase (after a surge) and make virtual meter.
// You should set your meter ID in meterN and define a non null pass over value (like 100000)
// chmod +x then ln -s /srv/http/comapps/req_espeasy.php /usr/bin/req_espeasy
// Use req_espeasy -total as main command -live as live command

// Config
$pathtomn = '/srv/http/metern'; // without / at the end
$metnum   = 1; // Vitual meter number
$url      = 'http://192.168.1.140/json?tasknr=2'; // espeasy url
$initial	= 0; // initial virtual meter value

// No edit should be needed bellow
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
if (isset($argv[2])) {
    die("Abording: Too many arguments\n");
}
if (!isset($argv[1])) {
    die("\nUsage: req_espeasy { total | live | Humidity | prev }\n
    -total : Virtual total counter
    -live : Counter live value
    -Humidity : Show humidity value
    -prev : Show previous file
    \n");
}
// Save previous values in a file, make sure the http user can write there
$prevfile = '/dev/shm/req_espeasy.json';
if (file_exists($prevfile) && !is_writable($prevfile)) {
    die("$prevfile not writable");
}

define('checkaccess', TRUE);
include("$pathtomn/config/config_main.php");
include("$pathtomn/config/memory.php");
include("$pathtomn/config/config_met$metnum.php");

function retrievecsv($meternum, $csvarray, $passo) // Retrieve last know value in csv
{
    $datareturn = null;
    $contalines = count($csvarray);
    $j          = 0;
    while (!isset($datareturn)) {
        $j++;
        $array      = preg_split('/,/', $csvarray[$contalines - $j]);
        $datareturn = (int) trim($array[$meternum]);
        if ($datareturn == '') {
            $datareturn = null;
        }
        if ($j == $contalines) {
            $datareturn = 0;
        }
    }
    if ($datareturn > $passo) {
        $datareturn -= $passo;
    }
    return $datareturn;
}

if ($argv[1] == '-live' || $argv[1] == '-total' || $argv[1] == '-Humidity') { // Get last values
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000); // error
    $espjson = array();
    if (($espjson = curl_exec($ch)) === false) {
        die(curl_error($ch) . "\n");
    }
    curl_close($ch);
    
    // Testing
    /*
    $espjson = '
    {  
    "TaskValues":[  
    {  
    "ValueNumber":1,
    "Name":"Count",
    "NrDecimals":2,
    "Value":10000.00
    },
    {  
    "ValueNumber":2,
    "Name":"Total",
    "NrDecimals":2,
    "Value":1000.00
    },
    {  
    "ValueNumber":3,
    "Name":"Time",
    "NrDecimals":2,
    "Value":0.00
    }
    ],
    "TTL":1000,
    "DataAcquisition":[  
    {  
    "Controller":1,
    "IDX":0,
    "Enabled":"false"
    },
    {  
    "Controller":2,
    "IDX":0,
    "Enabled":"false"
    },
    {  
    "Controller":3,
    "IDX":0,
    "Enabled":"false"
    }
    ],
    "TaskInterval":1,
    "Type":"Generic - Pulse counter",
    "TaskName":"gas",
    "TaskEnabled":"true",
    "TaskNumber":3
    }';
*/    
    $espjson   = json_decode($espjson, true);
    // Get values from json
    //$val_live  = $espjson['Count'];
    $val_count = $espjson['TaskValues'][1]['Value'];
    $val_count /=1000; // 1 impulse = 0.01m3 gas
    $val_humid = $espjson['TaskValues'][1]['Value'];
    //print_r($espjson);
}

if ($argv[1] == '-live') {
    if (!isset($val_live)) {
        die("Abording: Cannot get live Count value\n");
    }
    $val    = (float) $val_live;
    $outstr = utf8_decode("${'LID' . $metnum}($val*${'LIVEUNIT' . $metnum})\n");
    echo "$outstr";
} elseif ($argv[1] == '-total') {
    if (!isset($val_count)) {
        die("Abording: Cannot get last Total value\n");
    }
    // Retrieve previous virtual meter value
    if (file_exists($prevfile)) {
        $data     = file_get_contents($prevfile);
        $previous = json_decode($data, true);
    } else {
        $tdfile  = date('Ymd');
        $tdfile  = $pathtomn . '/data/csv/' . $tdfile . '.csv';
        $ytdfile = date('Ymd', strtotime("-1 day"));
        $ytdfile = $pathtomn . '/data/csv/' . $ytdfile . '.csv';
        
        if (file_exists($tdfile)) {
            $lines = file($tdfile);
        } else if (file_exists($ytdfile)) {
            $lines = file($ytdfile);
        }
        if (file_exists($tdfile) || file_exists($ytdfile)) { // today or yesterday
            $contalines           = count($lines);
            $previous['virt_tot'] = retrievecsv($metnum, $lines, ${'PASSO' . $metnum});
        } else { // restarting from scratch !
            $previous['virt_tot'] = $initial;
        }
        $previous['esp_count'] = 0;
    }
    $last = (float) $val_count;
    $prev = (float) $previous['esp_count'];
    if ($last > $prev) {
        $diff = $last - $prev; // Increment vitural meter
        $previous['virt_tot'] += $diff;
        if ($previous['virt_tot'] >= ${'PASSO' . $metnum}) { // passed over
            $previous['virt_tot'] -= ${'PASSO' . $metnum};
        }
        $previous['esp_count'] = $val_count;
    } elseif ($last < $prev) { // surge or esp counter restart
        $previous['esp_count'] = 0;
    }
    
    // Saving previous values
    $data = json_encode($previous);
    file_put_contents($prevfile, $data);
    $val    = $previous['virt_tot'];
    // Output
    $outstr = utf8_decode("${'ID' . $metnum}($val*${'UNIT' . $metnum})\n");
    echo "$outstr";
} elseif ($argv[1] == '-Humidity') {
    $val    = $val_humid;
    $outstr = utf8_decode("Humidity($val*hum)\n");
    echo "$outstr";
} elseif ($argv[1] == '-prev') {
    if (file_exists($prevfile)) {
        echo "\n$prevfile :\n\n";
        $data     = file_get_contents($prevfile);
        $previous = json_decode($data, true);
        print_r($previous);
    }
}
?>

megamarco83
Posts: 72
Joined: Tue Apr 14, 2015 9:25 am

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Tue Dec 18, 2018 1:57 pm

hi, it seams now working, i can test better only on saturday.
but only one question:
on the php file i set:
$initial = 122019; // initial virtual meter value
because my gas real meter is showing: 1220,19 m3
so how about the two decimal data?
is forseen on the script to have?
how i have to write the data with decimals in php ?
thanks

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Tue Dec 18, 2018 2:18 pm

Yup that's the initial value in 0.001m3. The decimal is a always a dot, so it should be 1220.19

megamarco83
Posts: 72
Joined: Tue Apr 14, 2015 9:25 am

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Tue Dec 18, 2018 6:49 pm

jeanmarc wrote:
Tue Dec 18, 2018 2:18 pm
Yup that's the initial value in 0.001m3. The decimal is a always a dot, so it should be 1220.19
ok thanks, and where is the conversion?
my real gas meter have 1 pulse = 0,01m3
is considering this conversion your php?


and i set in php
$initial = 1220.19; // initial virtual meter value

if i see the json in /run/shm i see:
{"virt_tot":1210.015,"esp_count":0.015}

i should not see the value 1220.19 that i put in php that is my starting value?

if i run the test in metern about the -total i see:
20(1210.015*m3) is a valid entry !

i see in espeasy web page that my pulse counter count n°15 pulse in total
so why is reported in the metern json "esp_count":0.015} ??
the conversion according to my gas meter should be 15 (pulse) * 0.01(my conversion per pulse) = 0.15 m3 gas

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Tue Dec 18, 2018 8:02 pm

change line 125

Code: Select all

 $val_count /=100; // 1 impulse = 0.01m3 gas
Also, for the initial value, it is when you starting from scratch. You should remove /dev/shm/req_espeasy.json and change the last daily value in csv if you wish to change.

megamarco83
Posts: 72
Joined: Tue Apr 14, 2015 9:25 am

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Wed Dec 19, 2018 10:37 am

jeanmarc wrote:
Tue Dec 18, 2018 8:02 pm
change line 125

Code: Select all

 $val_count /=100; // 1 impulse = 0.01m3 gas
Also, for the initial value, it is when you starting from scratch. You should remove /dev/shm/req_espeasy.json and change the last daily value in csv if you wish to change.
hi, i tested the php
this is my configuration:
Image
when i test the "command" with total is ok
but if i test the "live command" i see this error:
Command : req_espeasy -total
Abording: Cannot get live Count value is not valid, the correct format is 20(1234.5*) .
why if i put req_espeasy -live i see in the error the command -total??

in metern i see the meter live always 0

this is my json:
{"virt_tot":1212.24,"esp_count":1.24}

this is my php:

Code: Select all

#!/usr/bin/php
<?php
// A simple script to request a espeasy counter that doesn't always increase (after a surge) and make virtual meter.
// You should set your meter ID in meterN and define a non null pass over value (like 100000)
// chmod +x then ln -s /srv/http/comapps/req_espeasy.php /usr/bin/req_espeasy
// Use req_espeasy -total as main command -live as live command

// Config
$pathtomn = '/var/www/metern'; // without / at the end
$metnum   = 20; // Vitual meter number
$url      = 'http://192.168.0.204/json?tasknr=3'; // espeasy url
$initial	= 1211.22; // initial virtual meter value CHANGE LINE 125  -> $val_count /=100; // 1 impulse = 0.01m3 gas

// No edit should be needed bellow
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
if (isset($argv[2])) {
    die("Abording: Too many arguments\n");
}
if (!isset($argv[1])) {
    die("\nUsage: req_espeasy { total | live | Humidity | prev }\n
    -total : Virtual total counter
    -live : Counter live value
    -Humidity : Show humidity value
    -prev : Show previous file
    \n");
}
// Save previous values in a file, make sure the http user can write there
$prevfile = '/dev/shm/req_espeasy.json';
if (file_exists($prevfile) && !is_writable($prevfile)) {
    die("$prevfile not writable");
}

define('checkaccess', TRUE);
include("$pathtomn/config/config_main.php");
include("$pathtomn/config/memory.php");
include("$pathtomn/config/config_met$metnum.php");

function retrievecsv($meternum, $csvarray, $passo) // Retrieve last know value in csv
{
    $datareturn = null;
    $contalines = count($csvarray);
    $j          = 0;
    while (!isset($datareturn)) {
        $j++;
        $array      = preg_split('/,/', $csvarray[$contalines - $j]);
        $datareturn = (int) trim($array[$meternum]);
        if ($datareturn == '') {
            $datareturn = null;
        }
        if ($j == $contalines) {
            $datareturn = 0;
        }
    }
    if ($datareturn > $passo) {
        $datareturn -= $passo;
    }
    return $datareturn;
}

if ($argv[1] == '-live' || $argv[1] == '-total' || $argv[1] == '-Humidity') { // Get last values
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000); // error
    $espjson = array();
    if (($espjson = curl_exec($ch)) === false) {
        die(curl_error($ch) . "\n");
    }
    curl_close($ch);
    
    // Testing
    /*
    $espjson = '
    {  
    "TaskValues":[  
    {  
    "ValueNumber":1,
    "Name":"Count",
    "NrDecimals":2,
    "Value":10000.00
    },
    {  
    "ValueNumber":2,
    "Name":"Total",
    "NrDecimals":2,
    "Value":1000.00
    },
    {  
    "ValueNumber":3,
    "Name":"Time",
    "NrDecimals":2,
    "Value":0.00
    }
    ],
    "TTL":1000,
    "DataAcquisition":[  
    {  
    "Controller":1,
    "IDX":0,
    "Enabled":"false"
    },
    {  
    "Controller":2,
    "IDX":0,
    "Enabled":"false"
    },
    {  
    "Controller":3,
    "IDX":0,
    "Enabled":"false"
    }
    ],
    "TaskInterval":1,
    "Type":"Generic - Pulse counter",
    "TaskName":"gas",
    "TaskEnabled":"true",
    "TaskNumber":3
    }';
*/    
    $espjson   = json_decode($espjson, true);
    // Get values from json
    //$val_live  = $espjson['Count'];
    $val_count = $espjson['TaskValues'][1]['Value'];
    $val_count /=100; // 1 impulse = 0.01m3 gas
    $val_humid = $espjson['TaskValues'][1]['Value'];
    //print_r($espjson);
}

if ($argv[1] == '-live') {
    if (!isset($val_live)) {
        die("Abording: Cannot get live Count value\n");
    }
    $val    = (float) $val_live;
    $outstr = utf8_decode("${'LID' . $metnum}($val*${'LIVEUNIT' . $metnum})\n");
    echo "$outstr";
} elseif ($argv[1] == '-total') {
    if (!isset($val_count)) {
        die("Abording: Cannot get last Total value\n");
    }
    // Retrieve previous virtual meter value
    if (file_exists($prevfile)) {
        $data     = file_get_contents($prevfile);
        $previous = json_decode($data, true);
    } else {
        $tdfile  = date('Ymd');
        $tdfile  = $pathtomn . '/data/csv/' . $tdfile . '.csv';
        $ytdfile = date('Ymd', strtotime("-1 day"));
        $ytdfile = $pathtomn . '/data/csv/' . $ytdfile . '.csv';
        
        if (file_exists($tdfile)) {
            $lines = file($tdfile);
        } else if (file_exists($ytdfile)) {
            $lines = file($ytdfile);
        }
        if (file_exists($tdfile) || file_exists($ytdfile)) { // today or yesterday
            $contalines           = count($lines);
            $previous['virt_tot'] = retrievecsv($metnum, $lines, ${'PASSO' . $metnum});
        } else { // restarting from scratch !
            $previous['virt_tot'] = $initial;
        }
        $previous['esp_count'] = 0;
    }
    $last = (float) $val_count;
    $prev = (float) $previous['esp_count'];
    if ($last > $prev) {
        $diff = $last - $prev; // Increment vitural meter
        $previous['virt_tot'] += $diff;
        if ($previous['virt_tot'] >= ${'PASSO' . $metnum}) { // passed over
            $previous['virt_tot'] -= ${'PASSO' . $metnum};
        }
        $previous['esp_count'] = $val_count;
    } elseif ($last < $prev) { // surge or esp counter restart
        $previous['esp_count'] = 0;
    }
    
    // Saving previous values
    $data = json_encode($previous);
    file_put_contents($prevfile, $data);
    $val    = $previous['virt_tot'];
    // Output
    $outstr = utf8_decode("${'ID' . $metnum}($val*${'UNIT' . $metnum})\n");
    echo "$outstr";
} elseif ($argv[1] == '-Humidity') {
    $val    = $val_humid;
    $outstr = utf8_decode("Humidity($val*hum)\n");
    echo "$outstr";
} elseif ($argv[1] == '-prev') {
    if (file_exists($prevfile)) {
        echo "\n$prevfile :\n\n";
        $data     = file_get_contents($prevfile);
        $previous = json_decode($data, true);
        print_r($previous);
    }
}
?>

megamarco83
Posts: 72
Joined: Tue Apr 14, 2015 9:25 am

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Wed Dec 19, 2018 3:52 pm

i add a note.
i reboot the rasberry to try it and it seams that req_espeasy.php is not recording the decimal part at reboot

this is my csv, last one meter is gas.

15:40,32989113,25681700,82688196,59079446,24840,2.4,99.9,9920,37166,,46.56,,30.88,39.00,214.40,20.2,33.2,21.1,38.1,1213.45
15:40,32989113,25681700,82688196,59079446,24840,2.4,99.9,9920,37166,,46.56,,30.88,39.00,214.40,20.2,33.2,21.1,38.1,1213.45
15:45,32989177,25681700,82688260,59079446,24840,2.2,99.9,9984,37166,,46.50,,30.25,32.69,215.80,20.1,33,21.1,38.1,1213

i reboot at 15:42 and in csv was recorded 1213.45
after reboot at 15:45 the value recorded is 1213 (without decimal part)

the json after 15:42 show:
{"virt_tot":1213,"esp_count":0}

so it seams that is cutted the decimal part


inside php line 12 i have:
$initial = 1212.41; // initial value

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Wed Dec 19, 2018 4:29 pm

For the live you need to set the value $val_live, i dunno what it is.
For the reboot bug, change the line 48 in "Retrieve last know value in csv" part (was int) :

Code: Select all

datareturn = (float) trim($array[$meternum]);

megamarco83
Posts: 72
Joined: Tue Apr 14, 2015 9:25 am

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Wed Dec 19, 2018 5:25 pm

jeanmarc wrote:
Wed Dec 19, 2018 4:29 pm
For the live you need to set the value $val_live = $espjson['Count'];, i dunno what it is.
For the reboot bug, change the line 48 in "Retrieve last know value in csv" part (was int) :

Code: Select all

datareturn = (float) trim($array[$meternum]);
if i enabled line 123

Code: Select all

$val_live  = $espjson['Count'];
i receive an error...
but should not be:

Code: Select all

$val_live  = $espjson['TaskValues'][0]['Value'];
due to new structure of json of espeasy...

Code: Select all

pi@raspberrypi:~ $ curl http://192.168.0.204/json?tasknr=3
{
"TaskValues": [
{"ValueNumber":1,
"Name":"Count",
"NrDecimals":0,
"Value":0
},
{"ValueNumber":2,
"Name":"Total",
"NrDecimals":0,
"Value":0
},
{"ValueNumber":3,
"Name":"Time",
"NrDecimals":0,
"Value":0
}],
"TTL":30000,
"DataAcquisition": [
{"Controller":1,
"IDX":0,
"Enabled":"false"
},
{"Controller":2,
"IDX":0,
"Enabled":"false"
},
{"Controller":3,
"IDX":0,
"Enabled":"false"
}],
"TaskInterval":30,
"Type":"Generic - Pulse counter",
"TaskName":"gas",
"TaskEnabled":"true",
"TaskNumber":3

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest