Author Topic: Contribution: Monitor and visualize energy usage with emoncms.org in Vera  (Read 10182 times)

Offline wezzix

  • Sr. Newbie
  • *
  • Posts: 36
  • Karma: +2/-0
I found a really nice looking service called emoncms.org to visualise energy consumption measured in Vera. It's free and open source. You can use emoncms.org or setup your own server at home. You can also use it to log arbitrary numerical variables in vera, such as battery level, light, temperature etc. You can create custom dashboards and export to CSV.

Here is how I did it:

Setup Vera (instructions for UI7)
  • Register for an account at emoncms.org
  • Look up your write API key on your profile page
  • Download the attached EnergyMonitor.lua
  • Edit the file, enter your emoncms API key and setup your devices in VARIABLES according to the sample code already there
  • Please note that the energy can be fetched in two ways. The first way is directly from the Watts variable of your device. The second way is to use a lua function to calculate the energy. The second option is useful if you don't control the device directly from Vera. I use it to measure if my computer is on using a PingSensor, and send the power based on the sensor reading. I also have an 'Other' where I enter the average power of external devices such as Washmachines etc.
  • In the Vera UI, go to Apps -> Develop Apps -> Luup Files
  • On the right, click "Choose File". Browse to and select your edited EnergyMonitor.lua file, and click "Upload"
  • On the left, click "Edit Startup Lua" and add the following line:
Code: [Select]
require("EnergyMonitor.lua")
  • Click "Go"
  • Enjoy your data flowing to emoncms.org!

Setup emoncms inputs and feeds
You will immediately find your data here: http://emoncms.org/input/view
The inputs are by default not stored. Therefore, you will need to setup what is known as "feeds". Feeds is where the data is stored.

Feeds and inputs are very flexible, and therefore not very intuitive in the beginning. There are some guides at the bottom of this page which explains how to create feeds from your inputs:
http://openenergymonitor.org/emon/guide

In short, what I did was this:
  • Go to your inputs at http://emoncms.org/input/view
  • Find the input called Total and click the Spanner icon to access its processes.
  • You will now create three feeds:
  • Enter the following under Add process: Log to feed / CREATE NEW: / Total W / Fixed Interval With Averaging (PHPFIWA) / 10 s. Click Add.
  • Enter the following under Add process: Power to kWh / CREATE NEW: / Total kWh / Fixed Interval No Averaging (PHPFINA) / 10 s. Click Add.
  • Enter the following under Add process: Power to kWh/d / CREATE NEW: / Total kWhd. Click Add.
  • If everything is setup correctly, you will now find your feeds at http://emoncms.org/feed/list

Setup emoncms energy app
Now for the fun part, setting up the dashboard.
  • Click Apps > My Electric
  • Click the Spanner icon at the top right
  • Choose feeds as follows:
  • Power feed (Watts): Total W
  • Bar graph feed: Total kWh
  • Wh or kWh feed?: kWh elapsed
  • Set currency and unit cost if you wish.
  • Click Save.
  • Enjoy your energy dashboard!

The result
Attached is the code and examples of the two dashboards. My Electric displays current and historic energy consumption. I played around with My Solar to visualize how much energy I could save if I would switch to LED instead of halogen lights.
You can also design your own dashboard. I recommend that you watch the YouTube introduction before designing.

This code could be made into a Vera app. If anyone feels like taking the time to make an app, feel free to use my code as a base - just mention my name somewhere :)
« Last Edit: January 27, 2016, 03:29:09 pm by wezzix »

Offline FredrikY

  • Sr. Newbie
  • *
  • Posts: 22
  • Karma: +0/-0
Really realy nice, will give it a try! Good work!

Offline konradwalsh

  • Hero Member
  • *****
  • Posts: 564
  • Karma: +19/-6
thanks for this...

still struggling to get the feeds running..

could do with some more examples..

track on time of a switch for example

Offline wezzix

  • Sr. Newbie
  • *
  • Posts: 36
  • Karma: +2/-0
I found an error in the code and just uploaded a fixed file (deviceId was missing from VARIABLES).

I also made "Total" exclude any variables but Watts. That means that you can easily do arbitrary logging of switches, light level, battery level etc.
I included an example for a switch also:
Set up the feed as in my first post (first feed), that is, use the "Log to feed" processing. You can simply check on your feed by clicking on the "eye"-icon in the feed list. Attached is a sample of how my switch looks really zoomed in (zoom in by click and select in the graph). Not sure if that fits your needs. You can always export the feed data as CSV.

Code: [Select]
-- Setup your devices here. You can use a function to calculate the power as illustrated in the sample.
-- For device logging, use: key, deviceId, serviceId, serviceVar
-- For function based logging, use: key, calculate, serviceVar
local VARIABLES = {
{ key="Livingroom", deviceId = 12, serviceId='urn:micasaverde-com:serviceId:EnergyMetering1', serviceVar="Watts" }, -- Send device energy
{ key="Kitchen", deviceId = 13, serviceId='urn:micasaverde-com:serviceId:EnergyMetering1', serviceVar="Watts" },
{ key='KitchenSwitch', deviceId=13, serviceId="urn:upnp-org:serviceId:SwitchPower1", serviceVar="Status"}, -- Send switch status
{ key='Computer', calculate=function() return (IsComputerPingSensorTripped() and 38 or 1) end, serviceVar="Watts" }, -- Send variable value
{ key='Other', calculate=function() return 15 end, serviceVar="Watts" } -- Send a constant value
}

Offline amg0

  • Sr. Hero Member
  • ******
  • Posts: 2478
  • Karma: +153/-8
yes it looks quite good.
for your info, I made an native integration for it in ALTUI plugin. http://apps.mios.com/plugin.php?id=8246

Offline konradwalsh

  • Hero Member
  • *****
  • Posts: 564
  • Karma: +19/-6
quality....

Thanks for that

Offline wezzix

  • Sr. Newbie
  • *
  • Posts: 36
  • Karma: +2/-0
@amg0, ALTUI looks like a great piece of work, I might try that sometime.
I looked through the documentation but I didn't find any mention of emoncms. Did I miss something, or would you please shed a little light on the level of integration?

Offline amg0

  • Sr. Hero Member
  • ******
  • Posts: 2478
  • Karma: +153/-8
@amg0, ALTUI looks like a great piece of work, I might try that sometime.
I looked through the documentation but I didn't find any mention of emoncms. Did I miss something, or would you please shed a little light on the level of integration?
yes it is integrated. From a variable you can configure a data push to emoncns.

Offline simonclark

  • Full Member
  • ***
  • Posts: 147
  • Karma: +6/-9
I am guessing it only pushes to one of thingspeak, economy or datayours?

Offline amg0

  • Sr. Hero Member
  • ******
  • Posts: 2478
  • Karma: +153/-8
I am guessing it only pushes to one of thingspeak, economy or datayours?

out of the box, it pushes any variable value to either thingspeak or DataYours2; 
but it also provides an web interface to integrate some other data push destination. that requires though that a piece of code is calling a web service into ALTUI to register then it will receive, again as a web service, the value of the variables that the user has chosen to save; that piece of code can then do the push to whatever data storage they want.
more info here: http://forum.micasaverde.com/index.php/topic,35860.0.html

Offline mmd

  • Newbie
  • *
  • Posts: 10
  • Karma: +0/-0
Re: Contribution: Monitor and visualize energy usage with emoncms.org in Vera
« Reply #10 on: February 14, 2016, 11:26:58 am »
UPDATE: Nevermind, figured out the device was urn:micasaverde-com:serviceId:EnergyMetering1

Having a bit of problem reading my energy monitor.  The script seems to be communicating with Emoncms, but not reading the watts.  Here's the code I put in.  I've also attached the device config.  I've tried modifiying the key name to only use a number and not include any spaces, but it doesn't seem to make a difference.  Any ideas?  Thanks!

local VARIABLES = {
   { key="Grid Total", deviceId = 25, serviceId='urn:schemas-micasaverde-com:device:PowerMeter:1', serviceVar="Watts" }, -- Send device energy
   { key='Other', calculate=function() return 15 end, serviceVar="Watts" } -- Send a constant value
}
« Last Edit: February 14, 2016, 12:53:57 pm by mmd »

Offline mmd

  • Newbie
  • *
  • Posts: 10
  • Karma: +0/-0
Re: Contribution: Monitor and visualize energy usage with emoncms.org in Vera
« Reply #11 on: February 16, 2016, 04:13:52 pm »
Awesome info Wezzix!  I ended up writing a python script to pull SolarEdge data from their API and post to emoncms too.  The code runs on a Synology NAS that I can detail if anyone wants more info.  I have my HEM collecting usage data based on your script with the two being correlated on the My Solar app.

#! /usr/bin/python
import datetime
import http.client
import json, urllib.request

#Gather wattage from SolarEdge API
solarEdgeAPIKey="APIKEY"
url = "https://monitoringapi.solaredge.com/site/1/overview.json?api_key=" + solarEdgeAPIKey
#url = "https://monitoringapi.solaredge.com/site/1/power?api_key=" + solarEdgeAPIKey + "&startTime=2016-02-16%2008:00:00&endTime=2016-02-16%2011:30:00"
response = urllib.request.urlopen(url).read().decode('utf8')


data = json.loads(response)
watts = repr(data['overview']['currentPower']['power'])

#print(data['overview']['currentPower']['power'])

# Local EMONCMS API Key
#EMONKEY = "APIKEY"

# EMONCMS.org API Key
EMONKEY = "APIKEY"
now = datetime.datetime.now()

#---Local---#
#conn = http.client.HTTPConnection("192.168.3.20:80")
#request = "/emoncms/input/post.json?apikey=" + EMONKEY  + "&node=1&json={power:" + watts + "}"

#---Hosted---#
conn = http.client.HTTPConnection("emoncms.org:80")
request = "/input/post.json?apikey=" + EMONKEY  + "&node=20&json={power:" + watts + "}"
# emoncms uses a GET not a POST
conn.request("GET", request)
response = conn.getresponse()


Offline wezzix

  • Sr. Newbie
  • *
  • Posts: 36
  • Karma: +2/-0
Re: Contribution: Monitor and visualize energy usage with emoncms.org in Vera
« Reply #12 on: February 17, 2016, 02:04:20 am »
Thanks mmd, I'm glad you found it useful!

Offline stibbzy

  • Sr. Newbie
  • *
  • Posts: 29
  • Karma: +1/-0
Awesome info Wezzix!  I ended up writing a python script to pull SolarEdge data from their API and post to emoncms too.  The code runs on a Synology NAS that I can detail if anyone wants more info.  I have my HEM collecting usage data based on your script with the two being correlated on the My Solar app.

Hi mmd,

I'd love any more info you might have on pulling your SolarEdge data with the python script. I tried playing around with the script in your post, but kept getting urlopen error 3 for some reason :(

Thanks!

Offline Spanners

  • Full Member
  • ***
  • Posts: 212
  • Karma: +12/-5
    • https://www.livehouseautomation.com.au
Hi All,

Had a go at setting this up today. Got the Lua file edited and uploaded and put the line into the Startup Lua to call it.

When I go to http://emoncms.org/input/view I'm not seeing any inputs, so I assume that something isn't working as expected.

I've attached my lua file - is there anything wrong in the code or do I have some other issue?


UPDATE: If I paste the code into the Test Luup box, it works and starts sending updates to Emoncms. So for some reason the startup Lua code:

-- emoncms.org data uploader
require("EnergyMonitor.lua")

Isn't working - they are the first lines in my startup.



« Last Edit: July 25, 2016, 12:49:14 am by BlueSmurf »