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

Offline jorisdekloris

  • Sr. Newbie
  • *
  • Posts: 23
  • Karma: +0/-1
Could someone help me out with my Lua file? I think i've missed configured it somehow because no data is reported on emoncms.

Code: [Select]
-- API Key
local API_KEY = "xxxx"

-- 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="Emon1", deviceId = 223, serviceId='urn:micasaverde-com:serviceId:EnergyMetering1', serviceVar="Watts" }, -- Send device energy
{ key="Emon2", deviceId = 224, serviceId='urn:micasaverde-com:serviceId:EnergyMetering1', serviceVar="Watts" },
{ key='Emon3', deviceId = 225, serviceId="urn:micasaverde-com:serviceId:EnergyMetering1", serviceVar="Watts"}, -- Send switch status
}

local NODE_ID = 10
local TOTAL_KEY = 'Total'

Offline Spanners

  • Full Member
  • ***
  • Posts: 247
  • Karma: +15/-5
Try removing the comma after the last entry in the variable list.

Offline jorisdekloris

  • Sr. Newbie
  • *
  • Posts: 23
  • Karma: +0/-1
I missed that. Thanks!

I also double checked the API write key but still no reading end up in emoncms. Is there something else i can check prior to finding out on how to get the logs out of the Vera?

Offline akbooer

  • Master Member
  • *******
  • Posts: 5880
  • Karma: +251/-69
  • "Less is more"
It's not the comma.  That's perfectly valid Lua syntax.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline jorisdekloris

  • Sr. Newbie
  • *
  • Posts: 23
  • Karma: +0/-1
What am i looking for in the logs?

Something like this?

Code: [Select]
06 03/14/18 21:40:50.131 Device_Variable::m_szValue_set device: 227 service: urn:upnp-org:serviceId:altui1 variable: Version was: v2.16 now: v2.16 #hooks: 0 upnp: 0 skip: 0 v:(nil)/NONE duplicate:1 <0x73db2520>
06 03/14/18 21:40:50.673 Device_Variable::m_szValue_set device: 227 service: urn:upnp-org:serviceId:altui1 variable: DataStorageProviders was: { "emoncms": { "url": "", "callback": "sendValueToStorage_emoncms", "parameters": [ { "default": 1, "type": "number", "key": "nodeid", "label": "Node ID" }, { "type": "number", "key": "feedid", "label": "Feed ID" }, { "type": "text", "key": "inputkey", "label": "Input Key name" }, { "type": "text", "key": "readwritekey", "label": "Read/Write API Key" }, { "type": "url", "key": "graphicurl", "label": "Graphic Url", "ifheight": 460, "default": "http://emoncms.org/vis/realtime?feedid={1}&embed=1&apikey={3}" } ] }, "thingspeak": { "url": "", "callback": "sendValueToStorage_thingspeak", "parameters": [ { "type": "number", "key": "channelid", "label": "Channel ID" }, { "type": "text", "key": "readkey", "label": "Read API Key" }, { "type": "text", "key": "writekey", "label": "Write API Key" }, { "default": 1, "type": "number", "key": "fieldnum", "label": "Field Number" }, { "default": "//api.thingspeak.com/channels/{0}/charts/{3}?key={1}&width=450&height=260&results=60&dynamic=true", "type": "url", "key": "graphicurl", "label": "Graphic Url" } ] }, "IFTTT": { "url": "", "callback": "sendValueToStorage_ifttt", "parameters": [ { "type": "text", "key": "webhookkey", "label": "Web Hook Key" }, { "default": "vera_data_published", "type": "text", "key": "eventname", "label": "Hook Event Name" } ] } } now: { "emoncms": { "url": "", "callback": "sendValueToStorage_emoncms", "parameters": [ { "key": "nodeid", "type": "number", "default": 1, "label": "Node ID" }, { "type": "number", "key": "feedid", "label": "Feed ID" }, { "type": "text", "key": "inputkey", "label": "Input Key name" }, { "type": "text", "key": "readwritekey", "label": "Read/Write API Key" }, { "type": "url", "key": "graphicurl", "label": "Graphic Url", "ifheight": 460, "default": "http://emoncms.org/vis/realtime?feedid={1}&embed=1&apikey={3}" } ] }, "IFTTT": { "url": "", "callback": "sendValueToStorage_ifttt", "parameters": [ { "type": "text", "key": "webhookkey", "label": "Web Hook Key" }, { "key": "eventname", "type": "text", "default": "vera_data_published", "label": "Hook Event Name" } ] }, "thingspeak": { "url": "", "callback": "sendValueToStorage_thingspeak", "parameters": [ { "type": "number", "key": "channelid", "label": "Channel ID" }, { "type": "text", "key": "readkey", "label": "Read API Key" }, { "type": "text", "key": "writekey", "label": "Write API Key" }, { "default": 1, "type": "number", "key": "fieldnum", "label": "Field Number" }, { "default": "//api.thingspeak.com/channels/{0}/charts/{3}?key={1}&width=450&height=260&results=60&dynamic=true", "type": "url", "key": "graphicurl", "label": "Graphic Url" } ] } } #hooks: 0 upnp: 0 skip: 0 v:(nil)/NONE duplicate:0 <0x73db2520>
06 03/14/18 21:40:50.715 Device_Variable::m_szValue_set device: 227 service: urn:upnp-org:serviceId:altui1 variable: DataStorageProviders was: { "emoncms": { "url": "", "callback": "sendValueToStorage_emoncms", "parameters": [ { "key": "nodeid", "type": "number", "default": 1, "label": "Node ID" }, { "type": "number", "key": "feedid", "label": "Feed ID" }, { "type": "text", "key": "inputkey", "label": "Input Key name" }, { "type": "text", "key": "readwritekey", "label": "Read/Write API Key" }, { "type": "url", "key": "graphicurl", "label": "Graphic Url", "ifheight": 460, "default": "http://emoncms.org/vis/realtime?feedid={1}&embed=1&apikey={3}" } ] }, "IFTTT": { "url": "", "callback": "sendValueToStorage_ifttt", "parameters": [ { "type": "text", "key": "webhookkey", "label": "Web Hook Key" }, { "key": "eventname", "type": "text", "default": "vera_data_published", "label": "Hook Event Name" } ] }, "thingspeak": { "url": "", "callback": "sendValueToStorage_thingspeak", "parameters": [ { "type": "number", "key": "channelid", "label": "Channel ID" }, { "type": "text", "key": "readkey", "label": "Read API Key" }, { "type": "text", "key": "writekey", "label": "Write API Key" }, { "default": 1, "type": "number", "key": "fieldnum", "label": "Field Number" }, { "default": "//api.thingspeak.com/channels/{0}/charts/{3}?key={1}&width=450&height=260&results=60&dynamic=true", "type": "url", "key": "graphicurl", "label": "Graphic Url" } ] } } now: { "emoncms": { "url": "", "callback": "sendValueToStorage_emoncms", "parameters": [ { "default": 1, "type": "number", "key": "nodeid", "label": "Node ID" }, { "type": "number", "key": "feedid", "label": "Feed ID" }, { "type": "text", "key": "inputkey", "label": "Input Key name" }, { "type": "text", "key": "readwritekey", "label": "Read/Write API Key" }, { "type": "url", "key": "graphicurl", "label": "Graphic Url", "ifheight": 460, "default": "http://emoncms.org/vis/realtime?feedid={1}&embed=1&apikey={3}" } ] }, "IFTTT": { "url": "", "callback": "sendValueToStorage_ifttt", "parameters": [ { "type": "text", "key": "webhookkey", "label": "Web Hook Key" }, { "key": "eventname", "type": "text", "default": "vera_data_published", "label": "Hook Event Name" } ] }, "thingspeak": { "url": "", "callback": "sendValueToStorage_thingspeak", "parameters": [ { "type": "number", "key": "channelid", "label": "Channel ID" }, { "type": "text", "key": "readkey", "label": "Read API Key" }, { "type": "text", "key": "writekey", "label": "Write API Key" }, { "default": 1, "type": "number", "key": "fieldnum", "label": "Field Number" }, { "default": "//api.thingspeak.com/channels/{0}/charts/{3}?key={1}&width=450&height=260&results=60&dynamic=true", "type": "url", "key": "graphicurl", "label": "Graphic Url" } ] } } #hooks: 0 upnp: 0 skip: 0 v:(nil)/NONE duplicate:0 <0x73db2520>
06 03/14/18 21:40:50.748 Device_Variable::m_szValue_set device: 227 service: urn:upnp-org:serviceId:altui1 variable: DataStorageProviders was: { "emoncms": { "url": "", "callback": "sendValueToStorage_emoncms", "parameters": [ { "default": 1, "type": "number", "key": "nodeid", "label": "Node ID" }, { "type": "number", "key": "feedid", "label": "Feed ID" }, { "type": "text", "key": "inputkey", "label": "Input Key name" }, { "type": "text", "key": "readwritekey", "label": "Read/Write API Key" }, { "type": "url", "key": "graphicurl", "label": "Graphic Url", "ifheight": 460, "default": "http://emoncms.org/vis/realtime?feedid={1}&embed=1&apikey={3}" } ] }, "IFTTT": { "url": "", "callback": "sendValueToStorage_ifttt", "parameters": [ { "type": "text", "key": "webhookkey", "label": "Web Hook Key" }, { "key": "eventname", "type": "text", "default": "vera_data_published", "label": "Hook Event Name" } ] }, "thingspeak": { "url": "", "callback": "sendValueToStorage_thingspeak", "parameters": [ { "type": "number", "key": "channelid", "label": "Channel ID" }, { "type": "text", "key": "readkey", "label": "Read API Key" }, { "type": "text", "key": "writekey", "label": "Write API Key" }, { "default": 1, "type": "number", "key": "fieldnum", "label": "Field Number" }, { "default": "//api.thingspeak.com/channels/{0}/charts/{3}?key={1}&width=450&height=260&results=60&dynamic=true", "type": "url", "key": "graphicurl", "label": "Graphic Url" } ] } } now: { "emoncms": { "url": "", "callback": "sendValueToStorage_emoncms", "parameters": [ { "default": 1, "type": "number", "key": "nodeid", "label": "Node ID" }, { "type": "number", "key": "feedid", "label": "Feed ID" }, { "type": "text", "key": "inputkey", "label": "Input Key name" }, { "type": "text", "key": "readwritekey", "label": "Read/Write API Key" }, { "type": "url", "key": "graphicurl", "label": "Graphic Url", "ifheight": 460, "default": "http://emoncms.org/vis/realtime?feedid={1}&embed=1&apikey={3}" } ] }, "IFTTT": { "url": "", "callback": "sendValueToStorage_ifttt", "parameters": [ { "type": "text", "key": "webhookkey", "label": "Web Hook Key" }, { "default": "vera_data_published", "type": "text", "key": "eventname", "label": "Hook Event Name" } ] }, "thingspeak": { "url": "", "callback": "sendValueToStorage_thingspeak", "parameters": [ { "type": "number", "key": "channelid", "label": "Channel ID" }, { "type": "text", "key": "readkey", "label": "Read API Key" }, { "type": "text", "key": "writekey", "label": "Write API Key" }, { "default": 1, "type": "number", "key": "fieldnum", "label": "Field Number" }, { "default": "//api.thingspeak.com/channels/{0}/charts/{3}?key={1}&width=450&height=260&results=60&dynamic=true", "type": "url", "key": "graphicurl", "label": "Graphic Url" } ] } } #hooks: 0 upnp: 0 skip: 0 v:(nil)/NONE duplicate:0 <0x73db2520>
06 03/14/18 21:40:50.749 Device_Variable::m_szValue_set device: 227 service: urn:upnp-org:serviceId:altui1 variable: VariablesToSend was:  now:  #hooks: 0 upnp: 0 skip: 0 v:(nil)/NONE duplicate:1 <0x73db2520>
06
« Last Edit: March 14, 2018, 04:54:23 pm by jorisdekloris »

Offline jorisdekloris

  • Sr. Newbie
  • *
  • Posts: 23
  • Karma: +0/-1
I tried to run the EnergyMonitor.lua in the Test Luup code section and now it pushed to emoncms. All zero's that is :) and thats not correct.

I changed Watts to KWH in my Lua file and now its actually displaying values :)
« Last Edit: March 14, 2018, 05:35:45 pm by jorisdekloris »

Offline onepointbrewing

  • Jr. Member
  • **
  • Posts: 85
  • Karma: +2/-1
Im sort of there but not....

I dont code so im just trying to decypher what everyone has done on this thread and have no idea what Im doing.

Ive managed to get the lua file into my VE and put

emoncode = require("EnergyMonitor")
emoncode.EnergyMonitorOnTimer()

into startup lua

Should I be using the "read API" or the "read & write API" key?

I used the read and write API and eventually Emoncms populated, however it only did it once, and never updated again despite the feed saying updated (in 30+min increments)

And could some kind soul take a look at my lua file and tell me if there are any syntax issues

Code: [Select]
-- Setup your account at http://emoncms.org/
-- See API documentation at http://emoncms.org/input/api

module("EnergyMonitor", package.seeall)

-- API Key
local API_KEY = "e89.........d6e8f82"

-- 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="Test", deviceId = 37, serviceId='urn:micasaverde-com:serviceId:EnergyMetering1', serviceVar="Watts" }, -- Send device energy
{ key='Other1', calculate=function() return 15 end, serviceVar="Watts" } -- Send a constant value
}

local NODE_ID = 10
local TOTAL_KEY = 'Total'

-- Upload Frequency in seconds
local updateInterval = 10

-- Log debug messages
local DEBUG = true



-- You shouldn't need to change anything below this line --


local http = require('socket.http')
http.TIMEOUT = 3

local BASE_URL = "http://emoncms.org/input/post.json?apikey=" .. API_KEY
local Log = function (text) luup.log('EnergyMonitor Logger: ' .. (text or "empty")) end
local lastFullUpload = 0

local items = {} -- contains items: { time, deviceId, value }

local function StartCallbackTimer(interval)
luup.call_delay("EnergyMonitorOnTimer", interval or updateInterval, nil)
end

local function InitWatch()
StartCallbackTimer(1)
end

local function AddKeyValuePair(key, value)
local item = string.format("%s:%s", key, tostring(value))
items[#items + 1] = item
end

local function SerializeData()
local dataText = "{" .. table.concat(items, ",") .. "}"
return dataText
end

local function ResetData()
items = {}
end

local function SendData()
local data = SerializeData()
ResetData()

local parameters = "&node=" .. tostring(NODE_ID) .. "&json=" .. data
local url = BASE_URL .. parameters
if (DEBUG) then Log("Updating with: " .. parameters) end
http.request(url)
end

local function AddAllVariablesAndTotal()
local total = 0
for i, v in ipairs(VARIABLES) do
local value
if v.deviceId then
value = luup.variable_get(v.serviceId, v.serviceVar, v.deviceId)
elseif v.calculate then
value = v.calculate()
end
value = tonumber(value) or 0
if v.serviceVar == "Watts" then
total = total + value
end
AddKeyValuePair(v.key, value)
end
AddKeyValuePair(TOTAL_KEY, total)
end

function EnergyMonitorOnTimer()
StartCallbackTimer()
AddAllVariablesAndTotal()
SendData()
end

-- Added by RTS
_G.FunctionName = FunctionName

InitWatch()


any help would be greatly appreciated

Offline onepointbrewing

  • Jr. Member
  • **
  • Posts: 85
  • Karma: +2/-1
OK,
I posted some other posts here just now in frustration and have subsequently deleted them.

What I will do though is point people in a direction that ABSOLUTELY worked for me.

Follow the instructions in the link ive posted (im not related in any way to this company nor do I know them)

All you have to do is change your devices AND read/write API key, put this in startup.lua

emoncode = require("EnergyMonitor")
emoncode.EnergyMonitorOnTimer()

and bingo you have energy monitoring.

Heres the link:
https://www.livehouseautomation.com.au/blogs/news/using-emoncms-to-log-power-use

Thank you to "livehouseautomation.com.au" for this step by step.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5880
  • Karma: +251/-69
  • "Less is more"
I was about to reply in detail to your Lua syntax issue, but I'm glad you don't need the response, now.

Also pleased to see that you are up and running and have shared your solutions with others here.  That's indeed what the forum is all about.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.