Power to kwh/day extended to kwhd_plus and kwhd_minus

I have a Battery System with Xtender 3.5kW which I charge with  5kW Solar only when the house would export energy. I discharge the battery in when the house buy energiy from the grid. My goal is to hold the import / export energy to zero.

Sorry for my english its not my first language.

I read out the actual power from the Battery system. While charging it is a positive power. In the evening I switch to grid feed mode I discharge the batterie in this time it is a negative power.

I want to know how many kWh I charged per day and how many kWh I discharged. The input process power to kWhd would sum up both values

I extend in /www/emoncms/Modules/input the file process_model.php

add 2 lines to get_process_list()

$list[31] = array(_("PowPlus to kWh/d"),ProcessArg::FEEDID,"powerplus_to_kwhd",1,DataType::DAILY,"Power");     // Klaus
$list[32] = array(_("PowMinus  to kWh/d"),ProcessArg::FEEDID,"powerminus_to_kwhd",1,DataType::DAILY,"Power"); //Klaus

please just take the next free array numbers in $list[...]

and define functions "powerplus_to_kwhd" and "powerminus_to_kwhd"

If this is usefull for anybody

greetings from rainy Munich (3.38 kw sun input this day)

Klaus

 

 

public function powerplus_to_kwhd($feedid, $time_now, $value)
    {
      $new_kwh = 0;

      // Get last value
      $last = $this->feed->get_timevalue($feedid);
     
      $last['time'] = strtotime($last['time']);
      if (!isset($last['value'])) $last['value'] = 0;
      $last_kwh = $last['value']*1;
      $last_time = $last['time']*1;

      if ($last_time && ((time()-$last_time)<7200))
      {
        // kWh calculation
        $time_elapsed = ($time_now - $last_time);
        $kwh_inc = ($time_elapsed * $value) / 3600000.0;
if ($kwh_inc > 0) {
            $new_kwh = $last_kwh + $kwh_inc;
}
else {
     $new_kwh = $last_kwh;
}    
      } else {
        // in the event that redis is flushed the last time will
        // likely be > 7200s ago and so kwh inc is not calculated
        // rather than enter 0 we enter the last value
        $new_kwh = $last_kwh;
      }
     
      $feedtime = mktime(0, 0, 0, date("m",$time_now), date("d",$time_now), date("Y",$time_now));
      $this->feed->update_data($feedid, $time_now, $feedtime, $new_kwh);

      return $value;
    }

    public function powerminus_to_kwhd($feedid, $time_now, $value)
    {
      $new_kwh = 0;

      // Get last value
      $last = $this->feed->get_timevalue($feedid);
     
      $last['time'] = strtotime($last['time']);
      if (!isset($last['value'])) $last['value'] = 0;
      $last_kwh = $last['value']*1;
      $last_time = $last['time']*1;

      if ($last_time && ((time()-$last_time)<7200))
      {
        // kWh calculation
        $time_elapsed = ($time_now - $last_time);
        $kwh_inc = ($time_elapsed * $value) / 3600000.0;
if ($kwh_inc < 0) {
            $new_kwh = $last_kwh + $kwh_inc;
}
else {
     $new_kwh = $last_kwh;
}    
      } else {
        // in the event that redis is flushed the last time will
        // likely be > 7200s ago and so kwh inc is not calculated
        // rather than enter 0 we enter the last value
        $new_kwh = $last_kwh;
      }
     
      $feedtime = mktime(0, 0, 0, date("m",$time_now), date("d",$time_now), date("Y",$time_now));
      $this->feed->update_data($feedid, $time_now, $feedtime, $new_kwh);
      return $value;
    }