Process for creating 15-min intervals with kWh

There is currently a process that allows for power_to_kwhd(). Would it be possible to write a function that does power_to_kwh_per30min?

What would need to be changed in the following code for that?

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

    // Get last value
    $last = $this->feed->get_timevalue($feedid);

    if (!isset($last['value'])) $last['value'] = 0;
    $last_kwh = $last['value']*1;
    $last_time = $last['time']*1;

    // only update if last datapoint was less than 2 hour old
    // this is to reduce the effect of monitor down time on creating
    // often large kwh readings.
    $time_elapsed = ($time_now - $last_time);   
    if ($time_elapsed>0 && $time_elapsed<7200) { // 2hrs
        // kWh calculation
        $kwh_inc = ($time_elapsed * $value) / 3600000.0;
        $new_kwh = $last_kwh + $kwh_inc;
        $this->log->info("power_to_kwh() feedid=$feedid last_kwh=$last_kwh kwh_inc=$kwh_inc new_kwh=$new_kwh last_time=$last_time time_now=$time_now");
    } 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;

    $padding_mode = "join";
    $this->feed->insert_data($feedid, $time_now, $time_now, $new_kwh, $padding_mode);
    return $value;

Hello FP50, this is a good application for using the approach outlined here:

But rather than set mode to daily, leave mode blank and set the interval to 1800 (seconds), then set delta = 1. That will return the kwh per half hour period.

Thanks, works like a charm!

Hi, would it also be possible to have this data being posted into a feed?

1 Like