Upload emonpi data (perl script)

This is a really basic script to send my household consumption and voltage data to pvoutput. It sends data with a 15 minute delay to allow the solar data (in my case from the Enphase auto feed) to get fed first. If you don’t and don’t have a donation / sponsored account things go wrong - it’s documented, but I forget where.

After editing the access keys and feed IDs, run this every 5 minutes out of cron. You will also get the requests written to the log file so it can be replayed if something goes wrong.

Hope this might help someone in the future.

#!/usr/bin/perl -w

use strict;
use LWP::UserAgent;
use JSON::PP;
use POSIX qw(strftime);

# Change this for local requirements
$ENV{TZ} = 'Europe/London';

# Get the "Read Key" from your emonpi / emoncms set-up
my $EMONCMS_APIKEY = 'XXX';
# I only use this against my local emonpi but it should work against any emoncms
my $EMONCMS_BASE = 'http://localhost/emoncms';

# Edit these to suit
my $PVOUTPUT_APIKEY = 'XXX';
my $PVOUTPUT_SYSID = 'YYY';
my $PVOUTPUT_BASE = 'http://pvoutput.org/service/r2/addstatus.jsp';

# log each raw request here
open(LOG, ">>/tmp/pvoutput-pl.log") || die;

my $ua = LWP::UserAgent->new;
my $json = JSON::PP->new;

my $delay = 900;
my $now = $ARGV[0] || time;
my $then = $now - $delay;

# Grab the feed IDs (the numbers) from your Feeds page
my %EMONCMS_FEEDS = (
        voltage => XX, # measures voltage Vrms
        home => YY,  # accumulated home consumption in kWh
);
my %EMONCMS_VALUES;

foreach (keys %EMONCMS_FEEDS) {
        #my $f = "${EMONCMS_BASE}/feed/timevalue.json?apikey=${EMONCMS_APIKEY}&id=$EMONCMS_FEEDS{$_}";

        my $f = "${EMONCMS_BASE}/feed/data.json?apikey=${EMONCMS_APIKEY}&id=$EMONCMS_FEEDS{$_}&start=${then}000&end=${now}000&interval=$delay";

        my $r = $ua->get($f);

        die $r->status_line unless $r->is_success;
        my $j = $json->decode($r->decoded_content);

        $EMONCMS_VALUES{$_} = $j;
}

my $timestamp = $then;
my $date = strftime("%Y%m%d", localtime($timestamp));
my $time = strftime("%H:%M", localtime($timestamp));
my $voltage =  $EMONCMS_VALUES{voltage}->[0][1];
my $home_wh = $EMONCMS_VALUES{home}->[0][1] * 1000;

my $pvoutput_request = "${PVOUTPUT_BASE}?key=${PVOUTPUT_APIKEY}&sid=${PVOUTPUT_SYSID}&d=$date&t=$time&c1=1&v3=${home_wh}&v6=$voltage";

print LOG "${pvoutput_request}\n";

my $r = $ua->get(${pvoutput_request});
print LOG $r->status_line unless $r->is_success;

I struggled for ages to get my Emonpi to upload generation and consumption data to PV Output. I am not a programmer and finally gave up late last year and bought a second hand Solar-Log from eBay which does the job, although I had to buy a special meter to go with it to read my consumption which hasn’t been installed yet (electrician job).

@pgalbavy,

I have been looking for this solution for very long and only managed to find it today.
I am not a developer, but have basic understanding of programming.

I can see it is a perl script and I was able to edit the key values in a text editor on my PC.

Now what do I do with the file-content:

Suppose I have to install perl? How
Where / what folder shall I create the file, and with what extension.
Do I make it executable and just run it with cron every 5 minutes?

Your or any other person that is in the know, help will be really appreciated

There are many tutorials on the net such as https://learn.perl.org/installing/windows.html

Perl files typically have the extension “.pl” and are run by the installed interpreter which executes the script commands -

perl c:\myfolder\script.pl

On windows use Task Scheduler to run the above command every 5-minutes.