Ankündigung

Einklappen
Keine Ankündigung bisher.

Pluginhilfe Zählerstände (Tag/Woche/Monat/Jahr)

Einklappen
Dieses Thema ist geschlossen.
X
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

    [wiregate] Pluginhilfe Zählerstände (Tag/Woche/Monat/Jahr)

    Hallo,

    habe mich länger nicht mit Wiregate beschäftigt und wollte jetzt in den Feiertagen ein generisches Plugin schreiben, welches einen beliebigen aktuellen Zählerstand in ein rrd wegschreibt und aufgelaufene Tages-/Wochen-/Monats-/Jahrewerte zurückliefert.

    Code:
    ##################
    ### ZAEHLER ###
    ##################
    
    $plugin_info{$plugname.'_cycle'} = 0;
    
    my $rrd="zaehler";
    my $rrdFile="/var/www/rrd/$rrd.rrd";
    my $counter_ga = "9/0/11";      # Gruppenadresse Zaehlerstand, s.a. $laststate (DPT9)
    
    $plugin_subscribe{$counter_ga}{$plugname} = 1;
    
    # rrd file erzeugen, falls es noch nicht existiert
    if (!-e $rrdFile) {
        RRDs::create($rrdFile,
          '--step' => 86400,  # 24 h
          'DS:value:COUNTER:90000:0:2147483647',
          # 1 d (für 5 y)            7 d (für 25 y)
          'RRA:AVERAGE:0.5:1:1826', 'RRA:AVERAGE:0.5:7:1300',
          'RRA:MIN:0.5:1:1826',     'RRA:MIN:0.5:7:1300',
          'RRA:MAX:0.5:1:1826',     'RRA:MAX:0.5:7:1300',
        );
    
        if (RRDs::error) {
          plugin_log($plugname, "Create COUNTER-RRDs failed for $rrdFile: ".RRDs::error);
        } else {
          plugin_log($plugname, "Created COUNTER-RRD for $rrdFile");
        }
        
    }
    
    # Telegramm eingetroffen
    if (($msg{'apci'} eq "A_GroupValue_Write") and ($msg{'dst'} eq $counter_ga)) {
        my $laststate = knx_read($counter_ga,3600,9);   
        update_rrd($rrd, "", $laststate, "COUNTER"); 
        plugin_log($plugname,"Zaehlerstand: ".$laststate);
        return "Zaehlerstand: $laststate ";      
    }
    Dazu u.a. folgende Fragen:
    wie kann ich aus dem vorhandenen rrd den Zählerstand von vor x Tagen auslesen?
    wie kann ich mir den zugehörigen Graphen im Browser anzeigen lassen?

    Danke für Eure Hilfe!
    Gruss
    Uwe

    #2
    habe den Code mit Infos hier aus dem Forum etwas umgeschrieben, bekomme aber leider keine vernünftigen Werte aus dem rrd gelesen:

    Code:
    ##################
    ### OELZAEHLER ###
    ##################
    
    $plugin_info{$plugname.'_cycle'} = 240;   # muss kuerzer sein als RRD timeout
    
    my $rrd="oel_zaehler";
    my $rrdFile="/var/www/rrd/${rrd}.rrd";
    my $oelcounter_ga = "9/0/11";             # Gruppenadresse Zaehlerstand (DPT9)
    my $laststate;
    
    $plugin_subscribe{$oelcounter_ga}{$plugname} = 1;
    
    if (!-e $rrdFile) {
    
        RRDs::create($rrdFile,              # s.a. http://apfelboymchen.net/gnu/rrd/create/
          '--step' => 300,                  # Wert nur alle 5min annehmen
          'DS:value:COUNTER:4200:0:100000', # timeout 1h10m:min Wert:max Wert
          # 1 Tag (5min Aufloesung) 1 Woche (15min Aufloesung) 1 Monat (1h Aufloesung)  1Jahr (6h Aufloesung)    
          'RRA:AVERAGE:0.5:1:288', 'RRA:AVERAGE:0.5:3:672', 'RRA:AVERAGE:0.5:3:744', 'RRA:AVERAGE:0.5:3:1480',
          'RRA:MIN:0.5:1:288',     'RRA:MIN:0.5:3:672',     'RRA:MIN:0.5:3:744',     'RRA:MIN:0.5:3:1480',
          'RRA:MAX:0.5:1:288',     'RRA:MAX:0.5:3:672',     'RRA:MAX:0.5:3:744',     'RRA:MAX:0.5:3:1480',
        );
        
    
        if (RRDs::error) {
          plugin_log($plugname, "Create COUNTER-RRDs failed for $rrdFile: ".RRDs::error);
        } else {
          plugin_log($plugname, "Created COUNTER-RRD for $rrdFile");
        }
        
    }
    
    # Telegramm eingetroffen
    if ($msg{'apci'} eq "A_GroupValue_Write" and $msg{'dst'} eq $oelcounter_ga) {
        $laststate = decode_dpt9($msg{'data'});
    } else {
        $laststate = knx_read($oelcounter_ga,3600,9);
    }    
    
    $plugin_info{$plugname.'_Oelzaehler'} = $laststate if ($laststate > 0);
    update_rrd("$rrd","","$laststate","COUNTER") if ($laststate > 0); 
    plugin_log($plugname,"Zaehlerstand: ".$laststate);
    
    my $ds   = "MIN"; # Datasource: MIN AVERAGE MAX - egal bei Werten <180h
    my $start = "now-5min"; # AT-STYLE TIME SPECIFICATION 
    my $end   = "now";
    
    my ($min,$max);
    my ($dbstart, $step, $names, $data) =
    	RRDs::fetch("$rrdFile", "--start=$start","--end=$end", $ds);
    #
    foreach my $line (@$data) {
       foreach my $val (@$line) {
    	next unless defined $val;
    	$min = $val unless defined $min;
    	$max = $val unless defined $max;
    	$min = $val if $val < $min;
      	$max = $val if $val > $max;
        }
    }
    
    return "Zaehlerstand: $laststate ($min)";
    Irgendwo stand, dass beim Update der Gruppenadresse in $msg{'value'} der Wert der GA stehen würde, das funktioniert bei mir nur mit decode_dpt9($msg{'data'}), auch die Werte beim anlegen des rrd habe ich nochmal geändert ohne wirklich zu wissen, ob die so jetzt ok sind. Mittlerweile hätte ich wirklich viele Fragen zur Plugin Programmierung ...
    Gruss
    Uwe

    Kommentar


      #3
      in $msg{'value'} steht nur ein Wert, wenn dem WG der DPT bekannt ist. Dazu muss die GA mit korrektem DPT im WG importiert oder angelegt werden.

      Das RRD kannst du mit dem RRD Tool des Wiregate anzeigen. Das RRD Tool generiert auch die Grafiken die du dann z.B. als Kamerabilder in eine externe Visu einbinden kannst.
      Gruss Patrik alias swiss

      Kommentar


        #4
        hab jetzt 2 plugins fertig. Vielleicht hat jemand Vorschläge bezüglich Umgang mit den RRDs

        Das folgende Plugin ist für einen Braun Ölzaehler, der an eine UVR1611 angeschlossen ist. Der UVR BC/E Konverter bringt die Impulse als Absolutwert an den EIB Bus

        Code:
        ##################
        ### OELZAEHLER ###
        ##################
        
        #http://192.168.178.98//graph.pl?--start=-84h;--end=now&DEF:ds0=/var/www/rrd/oel.rrd:value:AVERAGE&XPORT:ds0
        
        $plugin_info{$plugname.'_cycle'} = 60;   # muss kuerzer sein als RRD timeout
        
        my $rrdName="oel";
        my $rrdFile="/var/www/rrd/${rrdName}.rrd";
        my $oelcounter_ga = "9/0/11";             # Gruppenadresse Oel Ticks (DPT9)
        my ($current_value,$current_braun,$min,$max,$Diff);
        my $Tagesverbrauch=0;
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
        $year += 1900;
        $mon += 1;
        
        ######################
        sub create_rrdFile {
        ######################
        
          if (!-e $rrdFile) {
        
            RRDs::create($rrdFile,              # s.a. http://apfelboymchen.net/gnu/rrd/create/
              '--step' => 300,                  # Wert nur alle 5min annehmen
              'DS:value:GAUGE:4200:0:100000', # timeout 1h10m:min Wert:max Wert
              # 1 Tag (5min Aufloesung) 1 Woche (15min Aufloesung) 1 Monat (1h Aufloesung)  1Jahr (6h Aufloesung)    
              'RRA:AVERAGE:0.5:1:288', 'RRA:AVERAGE:0.5:3:672', 'RRA:AVERAGE:0.5:3:744', 'RRA:AVERAGE:0.5:3:1480',
              'RRA:MIN:0.5:1:288',     'RRA:MIN:0.5:3:672',     'RRA:MIN:0.5:3:744',     'RRA:MIN:0.5:3:1480',
              'RRA:MAX:0.5:1:288',     'RRA:MAX:0.5:3:672',     'RRA:MAX:0.5:3:744',     'RRA:MAX:0.5:3:1480',
            );
            
        
            if (RRDs::error) {
              plugin_log($plugname, "Create COUNTER-RRDs failed for $rrdFile: ".RRDs::error);
            } else {
              plugin_log($plugname, "Created COUNTER-RRD for $rrdFile");
            }
            
          }
        
        }
        
        ######################
        sub get_minmax {
        ######################
        
          my $ds   = "AVERAGE"; # Datasource: MIN AVERAGE MAX - egal bei Werten <180h
          my $start = "now-4h"; # AT-STYLE TIME SPECIFICATION 
          my $end   = "now";
        
          my ($dbstart, $step, $names, $data) =
        	RRDs::fetch('/var/www/rrd/'.$rrdName.'.rrd',"--start=$start","--end=$end", $ds);
        
          foreach my $line (@$data) {
             foreach my $val (@$line) {
        	next unless defined $val;
        	$min = $val unless defined $min;
        	$max = $val unless defined $max;
        	$min = $val if $val < $min;
          	$max = $val if $val > $max;
             }
          }
        
        }
        
        ######################
        sub reset_values {
        ######################
          $plugin_info{$plugname.'_Braunzaehler'} = 4712;
          $plugin_info{$plugname.'_Tagesverbrauch_akt'} = 0;
          $plugin_info{$plugname.'_Tageszaehler'} = 0; 
          $plugin_info{$plugname.'_Wochenzaehler'} = 0;
          $plugin_info{$plugname.'_Monatszaehler'} = 0;
          $plugin_info{$plugname.'_Jahreszaehler'} = 0; 
          $plugin_info{$plugname.'_Jahr_'.$year}    = 0;
        }
        
        ####################
        # MAIN
        ####################
        
        $plugin_subscribe{$oelcounter_ga}{$plugname} = 1;
        
        #reset_values();
        create_rrdFile();
        
        my $write = $plugin_info{$plugname.'_written'};
        
        # Telegramm eingetroffen
        if ($msg{'apci'} eq "A_GroupValue_Write" and $msg{'dst'} eq $oelcounter_ga) {
            $current_value = decode_dpt9($msg{'data'});
        } else {
            $current_value = knx_read($oelcounter_ga,3600,9);
        }    
        $current_value=int($current_value);
        
        if ($hour == 0  and $write == 0) {
        
          $plugin_info{$plugname.'_written'} = 1;
          
          $plugin_info{$plugname.'_Braunzaehler_alt'} = $plugin_info{$plugname.'_Braunzaehler'};
          $plugin_info{$plugname.'_Tagesstart_alt'} = $plugin_info{$plugname.'_Tagesstart'};
          $plugin_info{$plugname.'_Tagesstart'} = $current_value; 
          $plugin_info{$plugname.'_Tagesverbrauch_akt'} = 0; 
        
          $plugin_info{$plugname.'_Tageszaehler_alt'} = $plugin_info{$plugname.'_Tageszaehler'};
          $plugin_info{$plugname.'_Tageszaehler'} = 0;       # Tageszahler 0 setzen
        
          if ($wday == 1) { # Montag
            $plugin_info{$plugname.'_Wochenzaehler_alt'} = $plugin_info{$plugname.'_Wochenzaehler'};
            $plugin_info{$plugname.'_Wochenzaehler'} = 0;    # Wochenzaehler 0 setzen
          }
        
          if ($mday == 1) { # 1. Tag des Monats
            $plugin_info{$plugname.'_Monatszaehler_alt'} = $plugin_info{$plugname.'_Monatszaehler'}; 
            $plugin_info{$plugname.'_Monatszaehler'} = 0;    # Monatszaehler 0 setzen
          }
        
          if ($yday == 0) { # 1. Tag des Jahres
            my $lastyear=$year-1;
            $plugin_info{$plugname.'_Braun_'.$lastyear} = $plugin_info{$plugname.'_Braunzaehler'};
            $plugin_info{$plugname.'_Jahr_'.$lastyear} = $plugin_info{$plugname.'_Jahreszaehler'};
            $plugin_info{$plugname.'_Jahreszaehler_alt'} = $plugin_info{$plugname.'_Jahreszaehler'}; 
            $plugin_info{$plugname.'_Jahreszaehler'} = 0;    # Jahreszaehler 0 setzen
          }
        
        } elsif ($hour == 0 and $write == 1) {
          $plugin_info{$plugname.'_written'} = 1;  
        } else {
          $plugin_info{$plugname.'_written'} = 0;
        } 
        
        #get_minmax(); 
        
        if (defined $plugin_info{$plugname.'_Tagesstart'} and $current_value > $plugin_info{$plugname.'_Tagesstart'} ) { 
             $plugin_info{$plugname.'_Aktuell'} = $current_value;
        
             $Tagesverbrauch=$current_value-$plugin_info{$plugname.'_Tagesstart'};
             $Tagesverbrauch=int($Tagesverbrauch);
        
             $Diff=$Tagesverbrauch-$plugin_info{$plugname.'_Tagesverbrauch_akt'};
             $plugin_info{$plugname.'_Tagesverbrauch_akt'}=$Tagesverbrauch;
        
             $plugin_info{$plugname.'_Braunzaehler'}  += $Diff; 
             $plugin_info{$plugname.'_Tageszaehler'}  += $Diff;
             $plugin_info{$plugname.'_Wochenzaehler'} += $Diff;
             $plugin_info{$plugname.'_Jahreszaehler'} += $Diff;
             
             $current_braun=$plugin_info{$plugname.'_Braunzaehler'};
             plugin_log($plugname,"Zaehlerstand:   ".$current_value);
             plugin_log($plugname,"Tagesverbrauch: ".$Tagesverbrauch);
             update_rrd("$rrdName","",$Tagesverbrauch); 
        }
        
        return "Zaehlerstand: $current_braun ($current_value), Tagesverbrauch: $Tagesverbrauch Liter";
        Das folgende Plugin holt aktuelle Stromwerte vom "Sunwatch" (Optokoppler an EON Stromzähler)

        Code:
        ####################
        ### STROMZAEHLER ###
        ####################
        
        $plugin_info{$plugname.'_cycle'} = 600;
        
        my $sunwatch="192.168.178.99";
        
        my $rrdName="strom";
        my $rrdFile="/var/www/rrd/${rrdName}.rrd";
        my ($Bezug,$Lieferung,$Erzeugung,$Eigenverbrauch,$Stromverbrauch);
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
        $year += 1900;
        $mon += 1;
        
        ######################
        sub create_rrdFile {
        ######################
        
          if (!-e $rrdFile) {
        
            RRDs::create($rrdFile,              # s.a. http://apfelboymchen.net/gnu/rrd/create/
              '--step' => 300,                  # Wert nur alle 5min annehmen
              'DS:value:GAUGE:4200:0:100000', # timeout 1h10m:min Wert:max Wert
              # 1 Tag (5min Aufloesung) 1 Woche (15min Aufloesung) 1 Monat (1h Aufloesung)  1Jahr (6h Aufloesung) 
              'RRA:AVERAGE:0.5:1:288', 'RRA:AVERAGE:0.5:3:672', 'RRA:AVERAGE:0.5:3:744', 'RRA:AVERAGE:0.5:3:1480',
              'RRA:MIN:0.5:1:288',     'RRA:MIN:0.5:3:672',     'RRA:MIN:0.5:3:744',     'RRA:MIN:0.5:3:1480',
              'RRA:MAX:0.5:1:288',     'RRA:MAX:0.5:3:672',     'RRA:MAX:0.5:3:744',     'RRA:MAX:0.5:3:1480',
            );
        
        
            if (RRDs::error) {
              plugin_log($plugname, "Create COUNTER-RRDs failed for $rrdFile: ".RRDs::error);
            } else {
              plugin_log($plugname, "Created COUNTER-RRD for $rrdFile");
            }
        
          }
        
        }
        
        ######################
        sub get_data {
        ######################
          $Bezug=qx(echo -e "GETPAR;2;0;Eplus" | /bin/nc $sunwatch 22222)/1000;
          $Bezug=int($Bezug);
          $plugin_info{$plugname.'_Bezug'} = $Bezug if ($Bezug > 0);
        
          $Lieferung=qx(echo -e 'GETPAR;2;0;Eminus' | /bin/nc $sunwatch 22222)/1000;
          $Lieferung=int($Lieferung);
          $plugin_info{$plugname.'_Lieferung'} = $Lieferung if ($Lieferung > 0);
        
          $Erzeugung=qx(echo -e 'GETPAR;3;0;Eplus' | /bin/nc $sunwatch 22222)/1000;
          $Erzeugung=int($Erzeugung);
          $plugin_info{$plugname.'_Erzeugung'} = $Erzeugung if ($Erzeugung > 0);
        
          $Eigenverbrauch=$Erzeugung-$Lieferung;
          $Eigenverbrauch=int($Eigenverbrauch);
          $plugin_info{$plugname.'_Eigenverbrauch'} = $Eigenverbrauch if ($Eigenverbrauch > 0);
        
          $Stromverbrauch=$Eigenverbrauch+$Bezug;
          $Stromverbrauch=int($Stromverbrauch);
          $plugin_info{$plugname.'_Stromverbrauch'} = $Stromverbrauch if ($Stromverbrauch > 0);
        }
        
        ####################
        # MAIN           
        ####################
        
        create_rrdFile();
        get_data();
        
        my $write = $plugin_info{$plugname.'_written'};
        
        if ($yday == 0 and $hour == 0 and $write == 0) {  # 1. Tag des Jahres morgens zwischen 00:00 und 00:59
        
            $plugin_info{$plugname.'_written'} = 1;
        
            my $lastyear=$year-1;
            $plugin_info{$plugname.'_Stromverbrauch_'.$lastyear} = $plugin_info{$plugname.'_Stromverbrauch'};
            $plugin_info{$plugname.'_Eigenverbrauch_'.$lastyear} = $plugin_info{$plugname.'_Eigenverbrauch'};
            $plugin_info{$plugname.'_Erzeugung_'.$lastyear} = $plugin_info{$plugname.'_Erzeugung'};
            $plugin_info{$plugname.'_Lieferung_'.$lastyear} = $plugin_info{$plugname.'_Lieferung'};
            $plugin_info{$plugname.'_Bezug_'.$lastyear} = $plugin_info{$plugname.'_Bezug'};
        
        } elsif ($hour == 0 and $write == 1) {
        
            $plugin_info{$plugname.'_written'} = 1; 
        
        } else {
        
            $plugin_info{$plugname.'_written'} = 0;
        
        }
        
        update_rrd("$rrdName","Stromverbrauch",$plugin_info{$plugname.'_Stromverbrauch'}); 
        update_rrd("$rrdName","Eigenverbrauch",$plugin_info{$plugname.'_Eigenverbrauch'});
        update_rrd("$rrdName","Erzeugung",$plugin_info{$plugname.'_Erzeugung'});
        update_rrd("$rrdName","Lieferung",$plugin_info{$plugname.'_Lieferung'});
        update_rrd("$rrdName","Bezug",$plugin_info{$plugname.'_Bezug'});
        
        return "Erzeugung: $Erzeugung kWh Lieferung: $Lieferung kWh, Bezug: $Bezug kWh, Eigenverbrauch: $Eigenverbrauch kWh, Gesamtverbrauch: $Stromverbrauch kWh ($Bezug+$Eigenverbrauch)";
        Gruss
        Uwe

        Kommentar

        Lädt...
        X