PowerMeter from PowerShell

image I was trying to get my Home Energy Monitor application working to Google PowerMeterimage this evening. To get things moving quickly I decided to prototype in PowerShell (as you the full sugary goodness of the .Net framework for free). Here’s the details on accessing PowerMeter from PowerShell…

Although it is called Google PowerMeter, it is simply a service that records variables (of either cumulative or instantaneous values).

Firstly you need to get registered for an account, and it is not obvious how you actually get a Google PowerMeter account if you don’t have some of the supported devices or don’t have a contract with one partner utility companies. The easy way is to put together a url that basically requests a new account. the format of the url is :-

https://www.google.com/powermeter/device/activate?mfg=MMM&model=PPP&did=DDD&cvars=N

All the details are on this page. I have a CurrentCost Envi, so my url became:

https://www.google.com/powermeter/device/activate?mfg=CurrentCost&model=Envi&did=8097&dvars=1

Note I’m using dvars at the end instead of cvar – dvars are for durational measurements and cvars are for instantaneous measurements – you need to get these right or your uploads will fail. the dvars=1 means I want only 1 variable (energy), I could have opted for more (dvars=2, or dvars=3 etc), but 1 will do for now.

imageWhen you’ve created your url, simply browse to it. Google will authenticate you with your usual Google account and then ask you to give a friendly name to the variable(s) you created. When complete you’ll be presented with a activation code. You can get this activation again by browsing to your settings page in Google PowerMeter.  From this activation code you need 3 pieces of data as highlighted below :

image

The first is your ‘auth token’, the second is your ‘user id’ and the third is your ‘device id’.

Now for the PowerShell script. It is fairly simple, it creates an Xml string with the start date of the reading, the duration (in seconds) and the value and then uploads this to Google PowerMeter. It does need some Headers adding first to make sure your sending the correct Content-Type and to make sure you are authorized…

$auth = "YOUR AUTH TOKEN"
$user = "YOUR USER ID"
$device = "YOUR DEVICE ID"
$variable = "d1"
$url = "https://www.google.com/powermeter/feeds/event"
$var = "https://www.google.com/powermeter/feeds/user/$user/$user/variable/$device.$variable"

$start = [System.Xml.XmlConvert]::ToString([System.DateTime]::Now)
$duration = 1
$energy = 9999

$xmlData = @"
    <feed xmlns=`"http://www.w3.org/2005/Atom`"
    xmlns:meter=`"http://schemas.google.com/meter/2008`"
    xmlns:batch=`"http://schemas.google.com/gdata/batch`">
    <entry>
        <category scheme=`"http://schemas.google.com/g/2005#kind`"
        term=`"http://schemas.google.com/meter/2008#durMeasurement`"/>
        <meter:subject>{0}</meter:subject>
        <batch:id>A.1</batch:id>
        <meter:startTime uncertainty=`"1.0`">{1}</meter:startTime>
        <meter:duration uncertainty=`"1.0`">{2}</meter:duration>
        <meter:quantity uncertainty=`"0.001`" unit=`"kW h`">{3}</meter:quantity>
    </entry>
    </feed>
"@

$rdgData = [string]::Format($xmlData, $var, $start, $duration, $energy)

$wc = New-Object System.Net.WebClient
$whc = New-Object System.Net.WebHeaderCollection$res
$whc.Add("Content-Type", "application/atom+xml")
$whc.Add("Authorization", "AuthSub token=`"$auth`"")
$wc.Headers = $whc

$response = $wc.UploadString($url, "POST", $rdgData)

Now you have the xml response in the $response variable. To check this you can simply ([xml]$response).feed.entry.status.code – you’re looking for a 201 (‘Created’).
You should now have a measurement lodged with Google PowerMeter !!  Enjoy…

GEO 51.4043388366699:-1.2875679731369
  • dm3281

    Very cool

  • Dave M

    Hello: How do I change the duration of the reading data I wanto upload

  • @DaveM
    Just change the $duration variable to the number of seconds you want the duration to be.

    .. Ken

  • dm3281

    Ken –

    I’m not sure the best way to contact you, so I’ll do it here. It’s related to EnergyMonitor anyway… 🙂

    1) The example you having on Coding4Fun for installing/uninstalling the EnergyMonitor.EXE service within VisualStudio has a typo on the uninstall. You used a /I instead of /U for uninstall.

    2) The binary download for the EnergyMonitor that you have on CodePlex doesn’t appear to work when trying to run INSTALLUTIL. I’m getting an assembly load failure (yes, I’m on x64 and tried using the x64 version of INSTALLUTIL.EXE). I ended up just downloading your source and using the .EXE form there. Worked great!

    3) Finally, I hate you. Your great article inspired me and I, too, purchased a CurrentCost system (only $129 at Amazon) and installed today. I had to load the Win7 drivers (even though I read reports I wouldn’t have to on Win7… Wierd.

    I’m using CurrentCost software right now to update Google’s PowerMeter, but hope to get your service running tonight and begin using it…

    I’m thinking it would be nice to have a Facebook plugin (I will probably play with this tonight and see what I can come up with…)

    I thought it would be cool to also have a simple Plugin that wrote to a known registry key the current settings. Then a Windows Live Writer Plugin to read this key and write out a simple tagline with the value. Kind of like "What’s playing…" but instead "What’s my energy consumption…" 🙂

    Anyway, those are the two ideas I have.

    Thanks for the great post and code!!!

  • @dm3281 Glad you liked it, and thanks for the feedback.
    I have a bunch of updates and a couple of new plugins to upload to Codeplex – hope to around to it this weekend.

    Let me know how it goes with your plugins also…

    .. Ken

  • dm3281

    Ken — is there anything special that needs to be done to get the graph to start displaying data?

    I followed your procedure and get a 201 created response back… if I go to the device activation page and hit the check measurement link and refresh, it says my device (powershell) is sending data and the time stamp changes…

    Unfortunately, the graph is just blank.

    I’ve tried re-activating and creating new devices with no luck.

    Downloading the raw data seems to show my data. I’ve changed the $enery = 9999 to various values… Sample below from CSV file.

    StartTime, EndTime, Usage (kW h)
    20101017T190003.110Z,20101017T190004.110Z,0.3185
    20101017T190108.405Z,20101017T190109.405Z,0.3185
    20101017T190348.055Z,20101017T190349.055Z,9999.0
    20101017T190558.328Z,20101017T190559.328Z,9999.0
    20101017T190600.486Z,20101017T190601.486Z,9999.0
    20101017T190602.738Z,20101017T190603.738Z,9999.0
    20101017T190609.047Z,20101017T190610.047Z,6666.0

  • dm3281

    Looks like the weekly and monthly graph is showing data but the daily is showing up blank. Wierd

  • dm3281

    Ken, I think I figured out the the $duration variable = 1 was causing nothign to display on the daily graph? I changed it to 600 (10 minutes) and it seems to be graphing now?

    I’m not sure if this is correct or not… I’d imagine I’d capture the data over 10 minutes and average it before sending?

  • @dm3281 The $duration = 1 was simply a sample.

    You’re correct, best bet is to sample over some time period and then send the reading with $duration set to the number of seconds in that time period.

    .. Ken

  • jlorsong

    Hey — you going to be updating Codeplex with some new Plugins anytime soon?

  • @jlorsong I have a database plugin I have been working on (supports MSSQL and MySQL backends) – that’ll be up there in a few days.

    Did you have something specific you were looking for ?

    .. KJ

  • jlorsong

    I was looking for a way to get rid of my CurrentCost console program and have your service in stead. Like the plugin idea so I can do stuff with the data….

  • jlorsong

    I still want to use Google PowerMeter and your service

  • @jlorsong – There is a Google PowerMeter plugin already. Not sure why it’s not on Codeplex – must have overlooked that. I’ll post it in the next couple of days, along with the database plugin.

    If you need it sooner, just email me ken AT kapie DOT com and I’ll send you it…

    .. KJ

  • dm3281

    Ken, have you had a chance to update codeplex yet? I’m off next week and plan on working on some of the enhancements that I discussed previously.

    Thanks!

  • @dm3281 I have been updating it today, added a real installer, got the first version of the DatabasePlugin done, (not yet tested though)so will be posting it all to Codeplex this evening…

    Regards .. Ken

  • dm3281

    That is great news! Thanks for your effort in this — this is a cool project!!!

    Did I see where you said you’ve also created a plugin to post data to Google PowerMeter as well? If so, I hope to get a copy of that too since I don’t want to lose my ability of posting my data to Google; but still want to take advantage of all your cool things…

    I’ve been playing with iPhone programming and have installed Mono… wonder if a Mono iPhone application similar to your WP7 is in order? 🙂

    Based on all my research (googling) I’ve only found one guy who wrote his own backend PHP/Cacti system for recording his data (instead of going to Google PowerMeter) and then writing an iPhone application to display the graphics…..

  • Jay,

    The API for Google PowerMeter is here : http://code.google.com/apis/powermeter/docs/meter_api_entry_schemas.html

    You should be able to retrieve data for your sensors…

    If you’re having problems getting it working let me know – send me your code and we’ll see what we can do…

    .. Ken