Script virtmeter.php [GUIDA]

Discussioni relative al software di monitoraggio energetico METERN
pinodm
Messaggi: 9
Iscritto il: 05/01/2018, 14:09

Re: Script virtmeter.php [GUIDA]

Messaggioda pinodm » 12/01/2020, 20:12

metus ha scritto:
pinodm ha scritto:Dopo l'aggiornamento a metern 1.2.1 non funziona più!
Ora inizio il debug :( ma chiedo: qualcuno ha già risolto?


La soluzione è qui:
megamarco83 ha scritto:ciao io sono in ferie, quidni rieso a rispondere limitatamente.
per quanto mi riguarda io avevo metern 1.1 e con lo script in prima pagina funzionava virtmeter senza problemi.
poi dall'aggiornamento a metern 1.2 jeanmarck mi ha spiegato che ha cambiato eliminando il comando LIVEPOOL che ora è diventato LIVECOMMAND
editando virtmetr della prima pagina fatto da garluk ho risolto.
nel post:
viewtopic.php?f=4&t=2003&start=180#p8369
ho allegato la versione di virtmeter.php modificata da me in modo che vada con metern 1.2 con le modifiche sopra citate.
spero che così possiate risolvere
ciao
Marco


Io ho aggiornato tempo fa e fatto subito la modifica manualmente e funziona ancora tutto perfettamente
Spero ti sia utile


Grazie mille!


metus
Messaggi: 120
Iscritto il: 10/05/2019, 1:04

Re: Script virtmeter.php [GUIDA]

Messaggioda metus » 09/02/2020, 11:22

Volevo chiedere se anche a voi quando riavviate raspberry il contatore virtuale creato con virtmeter per il controllo dei prelievi (consumi-produzione) dia sempre un valore alto anche se non è vero. Mi sono accorto in quanto ho settato da meterN una notifica via telegram al superamento di 4200W di potenza prelevata dalla rete enel. Ad ogni riavvio del raspberry mi arriva la notifica ed effettivamente da log di meterN mi dice "09/02/2020 10:55:11 #3 Prelievi have reached 4.689 Wh"
Ma vi assicuro non è vero.
Ad ogni riavvio ricevo la notifica (il valore che viene visualizzato nel messaggio cambia sempre di valore:

Codice: Seleziona tutto

09/02/2020 10:55:11   #3 Prelievi have reached 4.689 Wh

09/02/2020 10:54:38   Starting meterN on boot (798)

09/02/2020 10:10:02   #3 Prelievi have reached 4.598 Wh

09/02/2020 10:08:21   Starting meterN on boot (800)

09/02/2020 08:15:03   #3 Prelievi have reached 4.243 Wh

09/02/2020 00:00:02   Clean up events log

08/02/2020 20:20:16   Starting meterN (14961)

08/02/2020 18:10:01   #3 Prelievi have reached 7.128 Wh

08/02/2020 18:09:50   Starting meterN on boot (631)

08/02/2020 17:45:02   #3 Prelievi have reached 6.780 Wh

08/02/2020 17:41:57   Starting meterN on boot (615)

08/02/2020 17:35:01   #3 Prelievi have reached 6.628 Wh

08/02/2020 17:34:20   Starting meterN on boot (589)

08/02/2020 16:33:49   Starting meterN (10865)

08/02/2020 07:30:01   #3 Prelievi have reached 4.359 Wh

08/02/2020 00:00:01   Clean up events log

07/02/2020 08:45:02   #3 Prelievi have reached 4.219 Wh

07/02/2020 00:00:02   Clean up events log

06/02/2020 08:00:01   #3 Prelievi have reached 4.236 Wh

06/02/2020 00:00:01   Clean up events log

05/02/2020 22:10:01   #3 Prelievi have reached 11.463 Wh


Sotto riporto il mio sensore dei prelievi. Logicamente a regime tutto funziona.

Immagine

Gli altri sensori virtuali sono tutti impostati come tipo Elect Other mentre solo il reale modbus che legge i consumi di casa trifase è di tipo Elect House consumption e i tre inverter di tipo Elect House production quindi non dovrebbero esserci problemi.

Per completezza riporto lo script virtmeter.php

Codice: Seleziona tutto

#!/usr/bin/php
<?php
//
// This script will simulate a meter into a meterN compatible format.
// This script will evaluate a meter's pool and/or calculate the meter only in time band.
// This script, also, calculates withdrawn energy (imported energy from grid), exported enery
// and self consumption energy.
// The virtmeter should be configured in meterN as 'Elect, Other'
// If you use virtmeter to estimate house consumption, the virtmeter should be configured in
// meterN as 'Elect, House consumption'
//
// Usage:
//     virtmeter {virtmeternum} {meternumlisttoadd|0} {meternumlisttosub|0} {energy|power} {F1|F2|F3|F23|0}
//     virtmeter {virtmeternum} {impmeternum} {expmeternum} {housecenergy|housecpower} {F1|F2|F3|F23|0}
//     virtmeter {virtmeternum} 0 0 {impenergy|expenergy|selfcenergy|imppower|exppower|selfcpower} {F1|F2|F3|F23|0}
//
//     Where
//         virtmeternum       --> meterID of virtual meter
//         impmeternum        --> meterID of total meter
//         expmeternum        --> meterID of total meter
//         meternumlisttoadd  --> list of meterID to add (0 no add)
//         meternumlisttosub  --> list of meterID to sub (0 no sub)
//
//   
// Example:
//      virtmeter 14 9,11 8 energy 0    -> met14 = met9 + met11 - met8
//      virtmeter 15 12 0 energy F1     -> met15 = met12 (only if F1)
//      virtmeter 16 12 0 energy F23    -> met16 = met12 (only if F23)
//      virtmeter 18 0 0 impenergy 0    -> met18 = withdrawn energy (imported from grid)
//      virtmeter 19 0 0 expenergy F1   -> met19 = exported energy (only if F1)
//      virtmeter 20 0 0 selfcenergy F2 -> met20 = self consumption energy (only if F2)
//
//
// How to:
// 1) Copy the script in /var/www/comapps
//    change owner with command 'chown www-data.www-data virtmeter.php'
//    change permission with command 'chmod +x virtmeter.php'
//    make a link with command 'ln -s /var/www/comapps/virtmeter.php /usr/local/bin/virtmeter'
// 2) In meterN, set your real meters as 'Elect' 'House production | House consumption | Other'
// 3) Set virtual meter in meterN. The type should be 'Elect Other'
//    Set the 'Main command' and 'Live command' as needed like examples
// 4) Configure the script if necessary ($pathtomn)
//
//
// Known issues
// This script does not handle the passover (future feature)
//
//
// Copyright (C) 2017 by Garluk
//

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Path to metern (edit if necessary)
$pathtomn = '/var/www/metern';

//
// No edit should be needed bellow
//
// -----------------------------------------------------------------------------
$version = "0.4.0";

if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}

if (!isset($argv[1], $argv[2], $argv[3], $argv[4], $argv[5])) {
   echo(usage($version));
   die("\nAbording: no valid argument given.\n");
}

//
// function usage: write help screen
//
function usage($version)
{
   $txt = "virtmeter v$version\n"
      . "This script will simulate a meter into a meterN compatible format. \n"
      . "This script will evaluate a meter's pool and/or calculate the meter only in time band.\n"
      . "This script, also, calculates withdrawn energy (imported energy from grid), \n"
      . "exported enery and self consumption energy.\n\n"
      . "Usage:\n"
      . "   virtmeter {virtmeternum} {meternumlisttoadd|0} {meternumlisttosub|0} {energy|power} {<fascia>}\n"
      . "   virtmeter {virtmeternum} {impmeternum} {expmeternum} {housecenergy|housecpower} {<fascia>}\n"
      . "   virtmeter {virtmeternum} 0 0 {impenergy|expenergy|selfcenergy|imppower|exppower|selfcpower} {<fascia>}\n"
      . "\nWhere:\n"
      . "   virtmeternum\tmeterID of virtual meter\n"
      . "   impmeternum\tmeterID of total meter\n"
      . "   expmeternum\tmeterID of total meter\n"
      . "   meternumlisttoadd\tlist of meterID to add (0 no add)\n"
      . "   meternumlisttosub\tlist of meterID to sub (0 no sub)\n"
      . "   energy\tEstimates virtual meter energy\n"
      . "   impenergy\tWithdrawn energy (imported energy from grid)\n"
      . "   expenergy\tExported energy (exported energy to grid)\n"
      . "   selfcenergy\tSelf consumption energy\n"
      . "   housecenergy\tHouse consumption energy (if you have a own a total import/export and production meter)\n"
      . "   power\t\n"
      . "   imppower\t\n"
      . "   exppower\t\n"
      . "   selfcpower\t\n"
      . "   housecpower\t\n"
      . "   <fascia>\t{F1 | F2 | F3 | F23 | 0}\n\n"
      . "Example:\n"
      . "   virtmeter 14 9,11 8 energy 0    -> met14 = met9 + met11 - met8\n"
      . "   virtmeter 15 12 0 energy F1     -> met15 = met12 (only if F1)\n"
      . "   virtmeter 16 12 0 energy F23    -> met16 = met12 (only if F23)\n"
      . "   virtmeter 18 0 0 impenergy 0    -> met18 = withdrawn energy (imported from grid)\n"
      . "   virtmeter 19 0 0 expenergy F1   -> met19 = exported energy (only if F1)\n"
      . "   virtmeter 20 0 0 selfcenergy F2 -> met20 = self consumption energy (only if F2)\n"
      ;

   return $txt;
}


//
// function isFesta: ritorna true se il giorno è festivo
//
//      I giorni di festa sono:
//         - 1 e 6 gennaio;
//         - lunedì dell'Angelo;
//         - 25 aprile;
//         - 1 maggio;
//         - 2 giugno;
//         - 15 agosto;
//         - 1 novembre;
//         - 8, 25 e 26 dicembre.
//
function isFesta($tsTime)
{
   $feste = Array(101, 106, 425, 501, 602, 815, 1101, 1208, 1225, 1226);
   
   // Controllo il giorno di Pasquetta
   if (date('Y-m-d', strtotime('+1 day', easter_date())) == date('Y-m-d', $tsTime))
      return (true);
   
   // Controllo le altre feste
   $data100 = (date('n', $tsTime) * 100) + date('d', $tsTime);
   foreach ($feste as $festa) {
      if ($festa == $data100)
         return (true);
   }
   
   return false;
}


//
// function fascia: ritorna la fascia tariffaria
//
// Fasce tariffarie: F1, F2, F3
//
// Le fasce sono:
// F1 (ore di punta):    dalle ore 8.00 alle ore 19.00 dal lunedì al venerdì
// F2 (ore intermedie):  dalle ore 7.00 alle ore 8.00 e dalle ore 19.00 alle
//                       ore 23.00 dal lunedì al venerdì e dalle ore 7.00 alle
//                       ore 23.00 del sabato;
// F3 (ore fuori punta): dalle ore 00.00 alle ore 7.00 e dalle ore 23.00 alle
//                       ore 24.00 dal lunedì al sabato e tutte le ore della
//                       domenica e dei giorni:
//                              1 e 6 gennaio;
//                              lunedì dell'Angelo;
//                              25 aprile;
//                              1 maggio;
//                              2 giugno;
//                              15 agosto;
//                              1 novembre;
//                              8, 25 e 26 dicembre.
//
function fascia($tsTime)
{
   $giorno = date("w", $tsTime);   // 0 (dom) -> 6 (sab)
   $ora = date('Hi', $tsTime);      // 23:35 -> 2335
   // Il confronto orario tiene conto del ritardo di metern ad acquisire il dato
   // e considera un possibile ritardo di misura fino a 60 secondi rispetto alle
   // ore "intere". Le letture al termine della fascia oraria vengono
   // considerate appartenenti alla fascia oraria precedente.
   // La prima lettura nella nuova fascia oraria avviene dopo 5 minuti, quindi
   // risulta sempre nell'intervallo temporale di spettanza ed il suo incremento
   // viene aggiunto naturalmente alla fascia oraria corretta.

   // Festivo o domenica -> Fascia F3
   if (isfesta($tsTime) || $giorno == 0)
      return 'F3';

   // se al di fuori della fascia "07:00 - 23:00" --> Fascia F3
   if (!(($ora > "0700") && ($ora <= "2300")))
      return 'F3';

   // se è sabato nella fascia "07:00 - 23:00" --> Fascia F2
   if (($giorno == 6) && ($ora > "0700") && ($ora <= "2300"))
      return 'F2';

   // se si è nella fascia "07:00 - 08:00" oppure nella fascia "19:00 - 23:00" --> Fascia F2
   if ((($ora > "0700") && ($ora <= "0800")) || (($ora > "1900") && ($ora <= "2300")))
      return 'F2';

   // Tutto il resto F1
   return 'F1';
}


//
// function isFascia: restituisce true se l'ora è nella fascia
//
function isFascia($tsTime, $F)
{
   $result = fascia($tsTime);
   if ($result == $F)
      return true;
   if ($F == '0')
      return true;
   if (($F == 'F23') && (($result == 'F2') || ($result == 'F3')))
      return true;

   return false;
}


//
// function getvalue: Get data and validate with IEC 62056 data set structure
//
function getvalue($id, $cmd)
{
   $datareturn = null;
   $giveup = 0;
   $regexp = "/^$id\(-?[0-9\.]+\*[A-z0-9³²%°]+\)$/i"; //ID(VALUE*UNIT)
   
   while (!isset($datareturn) && $giveup < 3) { // Try 3 times
      exec($cmd, $datareturn);
      $datareturn = trim(implode($datareturn));

      if (preg_match($regexp, $datareturn)) {
         $datareturn = preg_replace("/^$id\(/i", '', $datareturn, 1); // VALUE*UNIT)
         $datareturn = preg_replace("/\*[A-z0-9³²%°]+\)$/i", '', $datareturn, 1); // VALUE
         settype($datareturn, 'int');
      } else {
         $datareturn = null;
      }
      $giveup++;
   }
   
   return $datareturn;
}


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



if (($argv[1] != NULL) && ($argv[2] != NULL) && ($argv[3] != NULL)
   && ($argv[4] != NULL) && ($argv[5] != NULL)
   && ($argv[4] == 'power' || $argv[4] == 'energy' ||
      $argv[4] == 'housecpower' || $argv[4] == 'housecenergy' ||
      $argv[4] == 'imppower' || $argv[4] == 'exppower' || $argv[4] == 'selfcpower' ||
      $argv[4] == 'impenergy' || $argv[4] == 'expenergy' || $argv[4] == 'selfcenergy')
   && ($argv[5] == 'F1' || $argv[5] == 'F2' || $argv[5] == 'F3' ||
      $argv[5] == 'F23' || $argv[5]=='0')
   ) {
   // Init variables
   $metnumtoadd  = array();
   $metnumtosub  = array();
   $val = 0.0;
   $toadd = 0.0;
   $tosub = 0.0;
   $prod = 0.0;
   $cons = 0.0;
   $addnumlist = array();
   $subnumlist = array();
   $prodlist = array();
   $conslist = array();

   // Virtual meter
    $metnum = $argv[1];
   // Get Meter Numbers to Add into an Array of int
   $metnumtoadd = json_decode('[' . $argv[2] . ']', true);
   // Get Meter Numbers to Sub into an Array of int
   $metnumtosub = json_decode('[' . $argv[3] . ']', true);
   // Fascia
    $fascia = $argv[5];
   
   // Set call time
   $calltime = time();

   define('checkaccess', true);
   include("$pathtomn/config/config_main.php");
   include("$pathtomn/config/memory.php");
   date_default_timezone_set($DTZ);
   
   // Read MeterN meters types and config
   for ($i = 1; $i <= $NUMMETER; $i++) {
      include("$pathtomn/config/config_met$i.php");
      if (in_array($i, $metnumtoadd, true) && !${"SKIPMONITORING$i"})
         $addnumlist[] = $i;
      if (in_array ($i, $metnumtosub, true) && !${"SKIPMONITORING$i"})
         $subnumlist[] = $i;
      if (${'PROD' . $i} == 1) {
         $prodlist[] = $i;
      }
      if (${'PROD' . $i} == 2) {
         $conslist[] = $i;
      }
   }

   // open mN shm memory
   $data = file_get_contents($MEMORY);
   $memarray = json_decode($data, true);

   // open mN shm live memory
   $data = file_get_contents($LIVEMEMORY);
   $livemem = json_decode($data, true);

   $nowUTC  = strtotime(date("Ymd H:i:s"));
   if ($nowUTC - $livemem['UTC'] > 40) {
      die("Abording: Too late mN live values\n");
   }

   // Retrive previus value
   $ok = true;
   $prevvalue = array();
   for ($i = 0; $i < count($addnumlist); $i++) {
      if (isset($memarray["Totalcounter$addnumlist[$i]"])) {
         $prevvalue["Totalcounter$addnumlist[$i]"] = $memarray["Totalcounter$addnumlist[$i]"];
      } else {
         $ok = false;
      }
   }
   for ($i = 0; $i < count($subnumlist); $i++) {
      if (isset($memarray["Totalcounter$subnumlist[$i]"])) {
         $prevvalue["Totalcounter$subnumlist[$i]"] = $memarray["Totalcounter$subnumlist[$i]"];
      } else {
         $ok = false;
      }
   }
   for ($i = 0; $i < count($prodlist); $i++) {
      if (isset($memarray["Totalcounter$prodlist[$i]"])) {
         $prevvalue["Totalcounter$prodlist[$i]"] = $memarray["Totalcounter$prodlist[$i]"];
      } else {
         $ok = false;
      }
   }
   for ($i = 0; $i < count($conslist); $i++) {
      if (isset($memarray["Totalcounter$conslist[$i]"])) {
         $prevvalue["Totalcounter$conslist[$i]"] = $memarray["Totalcounter$conslist[$i]"];
      } else {
         $ok = false;
      }
   }
   if (isset($memarray["Totalcounter$metnum"])) {
      $prevvalue["Totalcounter$metnum"] = $memarray["Totalcounter$metnum"];
   } else {
      $ok = false;
   }
   if ($ok == false) {
      $output = array();
      $output = glob($pathtomn . '/data/csv/*.csv');
      sort($output);
      $cnt = count($output);
   
      if ($cnt > 0) {
         $lines = file($output[$cnt - 1]);
         $contalines = count($lines);
         if ($contalines > 1) {
            $lastarray = preg_split("/,/", $lines[$contalines - 1]);
         } elseif ($contalines == 1 && file_exists($output[$cnt - 2])) {
            // yesterday, if current only header
            $lines = file($output[$cnt - 2]);
            $contalines = count($lines);
            $prevarray = preg_split("/,/", $lines[$contalines - 2]);
         }

         $prevvalue = array();
         for ($i = 0; $i < count($addnumlist); $i++) {
            $datareturn = null;
            $prevvalue["Totalcounter$addnumlist[$i]"] = retrievecsv($addnumlist[$i], $lines, ${'PASSO' . $addnumlist[$i]}, $datareturn);
         }
         for ($i = 0; $i < count($subnumlist); $i++) {
            $datareturn = null;
            $prevvalue["Totalcounter$subnumlist[$i]"] = retrievecsv($subnumlist[$i], $lines, ${'PASSO' . $subnumlist[$i]}, $datareturn);
         }
         for ($i = 0; $i < count($prodlist); $i++) {
            $datareturn = null;
            $prevvalue["Totalcounter$prodlist[$i]"] = retrievecsv($prodlist[$i], $lines, ${'PASSO' . $prodlist[$i]}, $datareturn);
         }
         for ($i = 0; $i < count($conslist); $i++) {
            $datareturn = null;
            $prevvalue["Totalcounter$conslist[$i]"] = retrievecsv($conslist[$i], $lines, ${'PASSO' . $conslist[$i]}, $datareturn);
         }
         $datareturn = null;
         $prevvalue["Totalcounter$metnum"] = retrievecsv($metnum, $lines, ${'PASSO' . $metnum}, $datareturn);
      } else {
         // no csv, starting from scratch !
         $prevvalue = array();
         for ($i = 0; $i < count($addnumlist); $i++) {
            $prevvalue["Totalcounter$addnumlist[$i]"] = 0.0;
         }
         for ($i = 0; $i < count($subnumlist); $i++) {
            $prevvalue["Totalcounter$subnumlist[$i]"] = 0.0;
         }
         for ($i = 0; $i < count($prodlist); $i++) {
            $prevvalue["Totalcounter$prodlist[$i]"] = 0.0;
         }
         for ($i = 0; $i < count($conslist); $i++) {
            $prevvalue["Totalcounter$conslist[$i]"] = 0.0;
         }
         $prevvalue["Totalcounter$metnum"] = 0.0;
      }
   }
   
   if (($argv[4] == 'power') && isFascia($calltime, $fascia)) {
      $cnt = count($addnumlist);
        for ($i = 0; $i < $cnt; $i++) {
         if (isset(${'LIVECOMMAND' . $addnumlist[$i]}) == 1 && isset($livemem["${'METNAME'.$addnumlist[$i]}$addnumlist[$i]"])) {
            $toadd += $livemem["${'METNAME'.$addnumlist[$i]}$addnumlist[$i]"];
         }
      }
        $cnt = count($subnumlist);
        for ($i = 0; $i < $cnt; $i++) {
         if (isset(${'LIVECOMMAND' . $subnumlist[$i]}) == 1 && isset($livemem["${'METNAME'.$subnumlist[$i]}$subnumlist[$i]"])) {
            $tosub += $livemem["${'METNAME'.$subnumlist[$i]}$subnumlist[$i]"];
         }
      }
      
      $val = $toadd - $tosub;
   } elseif (($argv[4] == 'imppower') && (isFascia($calltime, $fascia))) {
        for ($i = 0; $i < count($prodlist); $i++) {
         if (isset(${'LIVECOMMAND' . $prodlist[$i]}) == 1 && isset($livemem["${'METNAME'.$prodlist[$i]}$prodlist[$i]"])) {
            $prod += $livemem["${'METNAME'.$prodlist[$i]}$prodlist[$i]"];
         }
      }
        for ($i = 0; $i < count($conslist); $i++) {
         if (isset(${'LIVECOMMAND' . $conslist[$i]}) == 1 && isset($livemem["${'METNAME'.$conslist[$i]}$conslist[$i]"])) {
            $cons += $livemem["${'METNAME'.$conslist[$i]}$conslist[$i]"];
         }
      }
      
      $val = $cons - $prod;
      if ($val < 0) {
         $val = 0.0;
      }
   } elseif (($argv[4] == 'exppower') && (isFascia($calltime, $fascia))) {
        for ($i = 0; $i < count($prodlist); $i++) {
         if (isset(${'LIVECOMMAND' . $prodlist[$i]}) == 1 && isset($livemem["${'METNAME'.$prodlist[$i]}$prodlist[$i]"])) {
            $prod += $livemem["${'METNAME'.$prodlist[$i]}$prodlist[$i]"];
         }
      }
        for ($i = 0; $i < count($conslist); $i++) {
         if (isset(${'LIVECOMMAND' . $conslist[$i]}) == 1 && isset($livemem["${'METNAME'.$conslist[$i]}$conslist[$i]"])) {
            $cons += $livemem["${'METNAME'.$conslist[$i]}$conslist[$i]"];
         }
      }
      
      $val = $prod - $cons;
      if ($val < 0) {
         $val = 0.0;
      }
   } elseif (($argv[4] == 'selfcpower') && (isFascia($calltime, $fascia))) {
        for ($i = 0; $i < count($prodlist); $i++) {
         if (isset(${'LIVECOMMAND' . $prodlist[$i]}) == 1 && isset($livemem["${'METNAME'.$prodlist[$i]}$prodlist[$i]"])) {
            $prod += $livemem["${'METNAME'.$prodlist[$i]}$prodlist[$i]"];
         }
      }
        for ($i = 0; $i < count($conslist); $i++) {
         if (isset(${'LIVECOMMAND' . $conslist[$i]}) == 1 && isset($livemem["${'METNAME'.$conslist[$i]}$conslist[$i]"])) {
            $cons += $livemem["${'METNAME'.$conslist[$i]}$conslist[$i]"];
         }
      }
      
      if ($prod > $cons) {
         $val = $cons;
      } else {
         $val = $prod;
      }
   } elseif (($argv[4] == 'housecpower') && (isFascia($calltime, $fascia))) {
      // HouseConsumation = Prod + Tot
      // Production
        for ($i = 0; $i < count($prodlist); $i++) {
         if (isset(${'LIVECOMMAND' . $prodlist[$i]}) == 1 && isset($livemem["${'METNAME'.$prodlist[$i]}$prodlist[$i]"])) {
            $prod += $livemem["${'METNAME'.$prodlist[$i]}$prodlist[$i]"];
         }
      }

      $toadd = $livemem["${'METNAME'.$addnumlist[0]}$addnumlist[0]"];
      $tosub = $livemem["${'METNAME'.$subnumlist[0]}$subnumlist[0]"];
      $val = $prod + $toadd - $tosub;
   } elseif (($argv[4] == 'energy') && (isFascia($calltime, $fascia))) {
      $cnt = count($addnumlist);
      for ($i = 0; $i < $cnt; $i++) {
         $value = getvalue(${'ID' . $addnumlist[$i]}, ${'COMMAND' . $addnumlist[$i]});
         $toadd += ($value - $prevvalue["Totalcounter$addnumlist[$i]"]);
      }
      $cnt = count($subnumlist);
      for ($i = 0; $i < $cnt; $i++) {
         $value = getvalue(${'ID' . $subnumlist[$i]}, ${'COMMAND' . $subnumlist[$i]});
         $tosub += ($value - $prevvalue["Totalcounter$subnumlist[$i]"]);
      }
      
      if (isset($prevvalue["Totalcounter$metnum"])) {
         $val = $prevvalue["Totalcounter$metnum"] + $toadd - $tosub;
      } else {
         $val = $toadd - $tosub;
      }
      if ($val >= ${'PASSO' . $metnum}) {
         $val -= ${'PASSO' . $metnum};
      }
   } elseif (($argv[4] == 'energy') && (!isFascia($calltime, $fascia))) {
      if (isset($prevvalue["Totalcounter$metnum"])) {
         $val = $prevvalue["Totalcounter$metnum"];
      } else {
         $val = 0.0;
      }
   } elseif (($argv[4] == 'impenergy') && (isFascia($calltime, $fascia))) {
      for ($i = 0; $i < count($prodlist); $i++) {
         $value = getvalue(${'ID' . $prodlist[$i]}, ${'COMMAND' . $prodlist[$i]});
         if ($value != null) {
            $prod += ($value - $prevvalue["Totalcounter$prodlist[$i]"]);
         }
      }
      if ($prod < 0) {
         $prod = 0;
      }
      for ($i = 0; $i < count($conslist); $i++) {
         $value = getvalue(${'ID' . $conslist[$i]}, ${'COMMAND' . $conslist[$i]});
         $cons += ($value - $prevvalue["Totalcounter$conslist[$i]"]);
      }
      
      $val = $cons - $prod;
      if ($val < 0) {
         $val = 0.0;
      }
      if (isset($prevvalue["Totalcounter$metnum"])) {
         $val += $prevvalue["Totalcounter$metnum"];
      }
      if ($val >= ${'PASSO' . $metnum}) {
         $val -= ${'PASSO' . $metnum};
      }
   } elseif (($argv[4] == 'impenergy') && (!isFascia($calltime, $fascia))) {
      if (isset($prevvalue["Totalcounter$metnum"])) {
         $val = $prevvalue["Totalcounter$metnum"];
      } else {
         $val = 0.0;
      }
   } elseif (($argv[4] == 'expenergy') && (isFascia($calltime, $fascia))) {
      for ($i = 0; $i < count($prodlist); $i++) {
         $value = getvalue(${'ID' . $prodlist[$i]}, ${'COMMAND' . $prodlist[$i]});
         if ($value != null) {
            $prod += ($value - $prevvalue["Totalcounter$prodlist[$i]"]);
         }
      }
      if ($prod < 0) {
         $prod = 0;
      }
      for ($i = 0; $i < count($conslist); $i++) {
         $value = getvalue(${'ID' . $conslist[$i]}, ${'COMMAND' . $conslist[$i]});
         $cons += ($value - $prevvalue["Totalcounter$conslist[$i]"]);
      }
      
      $val = $prod - $cons;
      if ($val < 0) {
         $val = 0.0;
      }
      if (isset($prevvalue["Totalcounter$metnum"])) {
         $val += $prevvalue["Totalcounter$metnum"];
      }
      if ($val >= ${'PASSO' . $metnum}) {
         $val -= ${'PASSO' . $metnum};
      }
   } elseif (($argv[4] == 'expenergy') && (!isFascia($calltime, $fascia))) {
      if (isset($prevvalue["Totalcounter$metnum"])) {
         $val = $prevvalue["Totalcounter$metnum"];
      } else {
         $val = 0.0;
      }
   } elseif (($argv[4] == 'selfcenergy') && (isFascia($calltime, $fascia))) {
      for ($i = 0; $i < count($prodlist); $i++) {
         $value = getvalue(${'ID' . $prodlist[$i]}, ${'COMMAND' . $prodlist[$i]});
         if ($value != null) {
            $prod += ($value - $prevvalue["Totalcounter$prodlist[$i]"]);
         }
      }
      if ($prod < 0) {
         $prod = 0;
      }
      for ($i = 0; $i < count($conslist); $i++) {
         $value = getvalue(${'ID' . $conslist[$i]}, ${'COMMAND' . $conslist[$i]});
         $cons += ($value - $prevvalue["Totalcounter$conslist[$i]"]);
      }
      
      if ($prod > $cons) {
         $val = $cons;
      } else {
         $val = $prod;
      }
      if (isset($prevvalue["Totalcounter$metnum"])) {
         $val += $prevvalue["Totalcounter$metnum"];
      }
      if ($val >= ${'PASSO' . $metnum}) {
         $val -= ${'PASSO' . $metnum};
      }
   } elseif (($argv[4] == 'selfcenergy') && (!isFascia($calltime, $fascia))) {
      if (isset($prevvalue["Totalcounter$metnum"])) {
         $val = $prevvalue["Totalcounter$metnum"];
      } else {
         $val = 0.0;
      }
   } elseif (($argv[4] == 'housecenergy') && (isFascia($calltime, $fascia))) {
      for ($i = 0; $i < count($prodlist); $i++) {
         $value = getvalue(${'ID' . $prodlist[$i]}, ${'COMMAND' . $prodlist[$i]});
         if ($value != null) {
            if ($value >= $prevvalue["Totalcounter$prodlist[$i]"]) {
               $prod += ($value - $prevvalue["Totalcounter$prodlist[$i]"]);
            } else {
               $prod += ($value + ${'PASSO' . $prodlist[0]} - $prevvalue["Totalcounter$prodlist[$i]"]);
            }
         }
      }
      if ($prod < 0) {
         $prod = 0;
      }
      
      $value = getvalue(${'ID' . $addnumlist[0]}, ${'COMMAND' . $addnumlist[0]});
      if ($value >= $prevvalue["Totalcounter$subnumlist[0]"]) {
         $imp = ($value - $prevvalue["Totalcounter$addnumlist[0]"]);
      } else {
         $imp = ($value + ${'PASSO' . $addnumlist[0]} - $prevvalue["Totalcounter$addnumlist[0]"]);
      }
      
      $value = getvalue(${'ID' . $subnumlist[0]}, ${'COMMAND' . $subnumlist[0]});
      if ($value >= $prevvalue["Totalcounter$subnumlist[0]"]) {
         $exp = ($value - $prevvalue["Totalcounter$subnumlist[0]"]);
      } else {
         $exp = ($value + ${'PASSO' . $subnumlist[0]} - $prevvalue["Totalcounter$subnumlist[0]"]);
      }
      
      $val = $prod + $imp - $exp;
      if ($val < 0) {
         $val = 0.0;
      }
      if (isset($prevvalue["Totalcounter$metnum"])) {
         $val += $prevvalue["Totalcounter$metnum"];
      }
      if ($val >= ${'PASSO' . $metnum}) {
         $val -= ${'PASSO' . $metnum};
      }
   } elseif (($argv[4] == 'housecenergy') && (!isFascia($calltime, $fascia))) {
      if (isset($prevvalue["Totalcounter$metnum"])) {
         $val = $prevvalue["Totalcounter$metnum"];
      } else {
         $val = 0.0;
      }
   }
   
   // Set precision
   settype($val, 'float');
   $val = round($val, ${'PRECI' . $metnum});

   // Output
   switch ($argv[4]) {
      case 'power' :
         $str = utf8_decode("${'LID'.$metnum}($val*${'LIVEUNIT'.$metnum})");
         break;
      case 'imppower' :
         $str = utf8_decode("${'LID'.$metnum}($val*${'LIVEUNIT'.$metnum})");
         break;
      case 'exppower' :
         $str = utf8_decode("${'LID'.$metnum}($val*${'LIVEUNIT'.$metnum})");
         break;
      case 'selfcpower' :
         $str = utf8_decode("${'LID'.$metnum}($val*${'LIVEUNIT'.$metnum})");
         break;
      case 'energy' :
         $str = utf8_decode("${"ID$metnum"}($val*${'UNIT'.$metnum})");
         break;
      case 'impenergy' :
         $str = utf8_decode("${"ID$metnum"}($val*${'UNIT'.$metnum})");
         break;
      case 'expenergy' :
         $str = utf8_decode("${"ID$metnum"}($val*${'UNIT'.$metnum})");
         break;
      case 'selfcenergy' :
         $str = utf8_decode("${"ID$metnum"}($val*${'UNIT'.$metnum})");
         break;
      default:
         $str = '';
   }
   echo "$str\n";
} else {
   echo(usage($version));
   die("\nAbording: no valid argument given.\n");
}
?>


Grazie in anticipo per un vostro parere o semplicemente per dirmi se anche a voi capita


metus
Messaggi: 120
Iscritto il: 10/05/2019, 1:04

Re: Script virtmeter.php [GUIDA]

Messaggioda metus » 16/02/2020, 9:45

Flane, ieri mi hanno illuminato... concettualmente sbagliavo... il campo che si può compilare sui contatori di meterN ti avvisa sul consumo Wh (e non W quindi la potenza instantanea). Ho stupidamente frainteso ciò che permetteva di fare meterN. Sarebbe bello in futuro jm introducesse un controllo anche sul campo live...W...potenza instantanea per intenderci.

Per concludere al riavvio mi arriva la notofica perchè quando riavvio ho giá superato i 4200Wh di consumo :D

Avatar utente
Flane
Messaggi: 2460
Iscritto il: 16/01/2016, 15:02

Re: Script virtmeter.php [GUIDA]

Messaggioda Flane » 16/02/2020, 9:51

No ho capito cosa vuoi fare, ma se vuoi controllare i consumi istantanei per una "gestione carichi" trovi il mio script /var/www/MyScripts/loadcheck.php

che però va sistemato ora che RPINotify non funziona più ..... :(

metus
Messaggi: 120
Iscritto il: 10/05/2019, 1:04

Re: Script virtmeter.php [GUIDA]

Messaggioda metus » 16/02/2020, 9:56

Volevo esattamente quello che faceva il tuo loadcheck però da quando RPINotify non va più non so come modificarlo per farlo funzionare... inoltre da quando metern ha aggiunto telegram speravo di usare la funzione integrata di metern ma come ti dicevo ho frainteso. Nel topic RPInotify ne parliamo

p_max
Messaggi: 14
Iscritto il: 24/03/2020, 17:12

Re: Script virtmeter.php [GUIDA]

Messaggioda p_max » 31/03/2020, 23:09

Buonasera a tutti.
Mi accodo a questo thread. La mia situazione è la seguente: ho meterN 1.2.3 su NAS con docker e script virtmeter 0.4.1.
Ho due contatori per misurazione consumo agli indirizzi 1 e 2. Volevo impostare un virtmeter che facesse la semplice somma dei due.
Impostato come di seguito gli indicatori funzionano correttamente, l'indicazione della potenza istantanea compare e scompare (0W-valore attuale-0W-e via di seguito) per i meter 1 e 2 mentre il meter virtuale resta sempre a zero. Dove sta l'inghippo?

Immagine

Avatar utente
maxsemp
Messaggi: 85
Iscritto il: 13/04/2018, 11:44

Re: Script virtmeter.php [GUIDA]

Messaggioda maxsemp » 01/04/2020, 9:02

Prima di aggiungere e/o togliere un meter devi sempre fermare metern e poi riavviarlo, altrimenti non funziona.

p_max
Messaggi: 14
Iscritto il: 24/03/2020, 17:12

Re: Script virtmeter.php [GUIDA]

Messaggioda p_max » 01/04/2020, 9:05

Fatto da pannello ADMIN. Fermo, aggiungo meter in MAIN e imposto in METERS & SENSORS, riavvio.

Avatar utente
Flane
Messaggi: 2460
Iscritto il: 16/01/2016, 15:02

Re: Script virtmeter.php [GUIDA]

Messaggioda Flane » 01/04/2020, 9:33

Quando si presenta questo azzeramento altalenante di qualche misuratore è perchè c'è un errore nei comandi di lettura di qualche misuratore.
Fai i test per tutti i comandi (live e main) di tutti i misuratori e indicatori.


Torna a “MeterN”

Chi c’è in linea

Visitano il forum: Nessuno e 5 ospiti