Contribution: Monitor and visualize energy usage with emoncms.org in Vera

Sound like a simple syntax error in the combined code stopping it all from loading in the first place.

Hard to imagine how, since it should be very straight-forward, but can you in fact post the entire code that doesn’t work in startup? Otherwise it’s just guesswork.

Hi akbooer, copy of the rest of the startup lua attached if you want to cast your eye over it.

Thanks.

Well, for a start, line 27 is invalid syntax:

os.rename(baseFile .. tostring(i) .. ".txt", baseFile .. tostring(i 1) .. ".txt")

… I believe you mean tostring(i-1).

line 55 also…

dt.year = tonumber(yStr)   2000

line 103…

local tms = string.format(".d  ",math.floor (1000 * (time  1))) 

… I believe this should be string.format("%d ", … although this might just be a text encoding issue?
… also (time 1) should just be b[/b] ?

line 119…

math.floor((uptime a86400) / 3600), math.floor((uptime o3600) / 60))

line 137…

local today = time - (time 86400)

Having fixed all those, there are also a number of undefined globals and other things you may want to fix…

Analyzing the source code: 12 warnings.
untitled.lua:39: unused local variable 'snapE'; consider removing or replacing with '_'
untitled.lua:49: first assignment to global variable 'mStr'
untitled.lua:49: first assignment to global variable 'dStr'
untitled.lua:49: first assignment to global variable 'yStr'
untitled.lua:49: first assignment to global variable 'hhStr'
untitled.lua:49: first assignment to global variable 'mmStr'
untitled.lua:49: first assignment to global variable 'ssStr'
untitled.lua:65: first assignment to global variable 'tstamp'
untitled.lua:141: unused local variable 'tmpE'; consider removing or replacing with '_'
untitled.lua:155: first assignment to global variable 'date'
untitled.lua:163: unused local variable 'retn'; consider removing or replacing with '_'
untitled.lua:163: unused local variable 'err'; consider removing or replacing with '_'

So, I can’t see that this can ever have worked. It looks like you come from the world of strongly typed languages? Would strongly that you use an IDE with a good code analyzer.

Hi Akbooer,

I can’t take credit for writing any of that code - I just stole it from various forum posts. :-[

http://forum.micasaverde.com/index.php/topic,31292.0.html

Thank you for the feedback on the issues with it, for now I’ve just removed it entirely as I’m not having Luup/Vera Restart issues (for the first time in years). But I swear it did work! :slight_smile:

Just gave it a try and it doesn’t seem to post the inputs for me… similar to BlueSmurf

Edit: Figured it out.

  1. Went back to the unmodified file from wezzix. The edit from BlueSmurf were one of the problems…
  2. Uploading through the UI app creator compresses the lua file creating an .lzo file on the vera. I SCP’d the file to prevent the compression

Now it all works.

having an issue getting the data to show up correctly.
in my log emoncms log files i have this error
2016-08-01 02:08:55.105|ERROR|input_controller.php|Format error: csv value is not numeric
2016-08-01 02:09:05.103|ERROR|input_controller.php|Format error: csv value is not numeric

in my vera logs i have this
luup_log:0: EnergyMonitor Logger: Updating with: &node=1&json={WashRoom Temp:69.4,Total:0}

(i was trying to log a temp sensor, the input shows up (the node, but the value is always zero)
i suspect it’s because of the format error…
any idea what may be wrong?

I don’t know if it helps, but this:

json={WashRoom Temp:69.4,Total:0}

is not valid JSON.

Try this instead?

json={"WashRoom Temp":69.4,"Total":0}

[quote=“akbooer, post:27, topic:190754”]I don’t know if it helps, but this:

json={WashRoom Temp:69.4,Total:0}

is not valid JSON.

Try this instead?

json={"WashRoom Temp":69.4,"Total":0} [/quote]

Thanks for the tip… I’ll give it a go this evening.

Hi Guys,

Hopefully this is an easy one for someone familiar with Lua.

Just finished the build of my 3-Phase energy monitoring system, a hybrid of Mysensors and Open Energy Monitor. I have it reporting into Vera, and uploading to emoncms.org

local VARIABLES = {
	{ key="Emon1", deviceId = 70, serviceId='urn:micasaverde-com:serviceId:EnergyMetering1', serviceVar="Watts" }, -- Send device energy
	{ key="Emon2", deviceId = 73, serviceId='urn:micasaverde-com:serviceId:EnergyMetering1', serviceVar="Watts" },
	{ key="Emon3", deviceId = 74, serviceId='urn:micasaverde-com:serviceId:EnergyMetering1', serviceVar="Watts" }

All good so far. What I’d like to do is add a new key value which is the total of these 3 values. The original lua script has an example for sending a constant value

{ key="Other", calculate=function() return 15 end, serviceVar="Watts" } -- Send a constant value

Is there a way to modify that so the returned value is the sum of the first 3? Or does this require processing within the script itself - I see there’s code there that effectively sums all of the values reported. Which is fine if you’re only sending the 3 phase values, but I have other devices reporting watts that I don’t want included in the total.

[quote=“Spanners, post:29, topic:190754”]What I’d like to do is add a new key value which is the total of these 3 values. The original lua script has an example for sending a constant value

{ key="Other", calculate=function() return 15 end, serviceVar="Watts" } -- Send a constant value

Is there a way to modify that so the returned value is the sum of the first 3? Or does this require processing within the script itself - I see there’s code there that effectively sums all of the values reported. Which is fine if you’re only sending the 3 phase values, but I have other devices reporting watts that I don’t want included in the total.[/quote]

Something like:

{ key="Other", calculate=function() return luup.variable_get('urn:micasaverde-com:serviceId:EnergyMetering1', 'Watts', 70) + luup.variable_get('urn:micasaverde-com:serviceId:EnergyMetering1', 'Watts', 73) + luup.variable_get('urn:micasaverde-com:serviceId:EnergyMetering1', 'Watts', 74) end, serviceVar="Watts" }

I’m struggling to get this working and am hoping for some help. :slight_smile:

I’ve got a Vera3 running UI7.

I followed wezzix instructions at the top of this thread, ie, created an account at emoncms, amended and loaded the EnergyMonitor.lua file and entered the startup code.
This didn’t generate any inputs in emoncms so I followed RTS’s instructions and added the following to the start of the EnergyMonitor.lua file

module("EnergyMonitor", package.seeall)
This to the end of the file:

_G.EnergyMonitorOnTimer = EnergyMonitorOnTimer

And, this in the Edit Startup Lua window:

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

However, I get the following error in the top of the dashboard:

ERROR: error in lua for scenes and events

When i look at the log file I see the following error on multiple occasions…

LuaInterface::CallFunction_Timer-5 function EnergyMonitorOnTimer failed [string “module(“EnergyMonitor”,package.seeall)…”]:17: attempt to perform arithmetic on a nil value

Any helpful tips?

I got everything working. There was a syntax error in my code.

However, I’ve got a weird problem. I’ve set up a number of dashboards, all of which are visible via a url containing the relevant API. I can see the dashboards on my ipad via Safari but they refuse to display in a webtile from within Homewave.

Is there anything unusual about the way Emoncms displays dashboards that could explain this problem???

Of course, it’s possible it’s a Homewave issue and not an Emomcms issue…

No longer working after upgrade to version 1.7.919.

Getting “ERROR : Error in lua for scenes and events” >:(

UPDATE:

Code:
emoncode = require(“EnergyMonitor”)
emoncode.EnergyMonitorOnTimer()

Fixed and working. Syntax error!

EDIT: Added complete EnergyMonitor.lua code
///////////////////////////////////////////////////

I’m having some trouble getting this to work.

My objective is to:
Measure/log energy consumption on my house. (Do I need two CT clamps or just one for this? Am in the USA)
Measure/log energy consumption on my 240v kiln. (Do I need two CT clamps or just one for this? Am in the USA)
I have TWO - Aeon Home Energy Monitor (v1)'s (one for the breaker box and one for the kiln). Presently only one is setup as I am trying to get the house consumption data working first…which is troublesome :slight_smile:

Here’s where I’m at…
I’ve successfully got the Aeon Home Energy Monitor (v1) to talk to my VeraPlus. I can see the actual Watts and KWH on the Vera Dashboard. I’ve also go the VeraPlus talking to Emoncms (it sends my INPUTs to Emoncms with the keys I’ve entered). I’ve also created a FEED in Emoncms.

Unfortunately, on Emoncms I am only seeing ZEROS (“0”) as my inputs. What could be the problem? Any ideas?

Setup:
VeraPlus. UI7 (latest update). Aeon Home Energy Monitor (v1) with two current clamps powered by USB (1s update interval to VeraPlus).

Questions:
Is “device name” in the VeraPlus UI the “key” needed in the VARIABLES section of the EnergyMonitor.lua file?
Where do you find the specific “serviceVar” for the devices?

Which of the following do I need (or can I) to publish to Emoncms?
My Aeon Home Energy Monitor shows up as 3 devices…
1st is the total Watts & KWH (in Vera UI, I named this “totalwkwh”)
2nd is Watt & KWH from 1st CT Clamp reading (in Vera UI, I named this “hema”)
3nd is Watt & KWH from 2nd CT Clamp reading (in Vera UI, I named this “hemb”)

Screenshots are attached.
Normally, I’m pretty capable with mild programming and setup, but this is stumping me!

Thanks in advance :slight_smile:

My code is below:
—Startup Lua—

[code]module(“EnergyMonitor”, package.seeall)

– Setup your account at http://emoncms.org/
– See API documentation at Emoncms - input api

– API Key
local API_KEY = “REMOVED FOR POST”

– 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=“totalwkwh”, deviceId = 065, serviceId=‘urn:schemas-micasaverde-com:device:PowerMeter:1’, serviceVar=“Watts” }, – Send Joined Device data
{ key=“hema”, deviceId = 67, serviceId=‘urn:schemas-micasaverde-com:device:PowerMeter:1’, serviceVar=“Watts” }, – Send CT 1 device data
{ key=“hema”, deviceId = 67, serviceId=‘urn:schemas-micasaverde-com:device:PowerMeter:1’, serviceVar=“CurrentLevel” }, – Send CT 1 device data
{ key=“hemb”, deviceId = 66, serviceId=‘urn:schemas-micasaverde-com:device:PowerMeter:1’, serviceVar=“Watts” }, – Send CT 2 device data
{ key=“hemb”, deviceId = 66, serviceId=‘urn:schemas-micasaverde-com:device:PowerMeter:1’, serviceVar=“CurrentLevel” }, – Send CT 2 device data
}

– Add the following to your Vera’s Startup Lua (without the preceding dashes) to run the logging on every Vera restart
– emoncode = require(“EnergyMonitor”)
– emoncode.EnergyMonitorOnTimer()

local NODE_ID = 10
local TOTAL_KEY = ‘Total’

– Upload Frequency in seconds ORIGINALLY 60
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

_G.EnergyMonitorOnTimer = EnergyMonitorOnTimer
InitWatch()
[/code]

Hello world burger!

It looks like your EnergyMonitor.lua is missing the bottom part of the code.

There is a section of code where it states:
– You shouldn’t need to change anything below this line –

The code ends with:
_G.EnergyMonitorOnTimer = EnergyMonitorOnTimer
InitWatch()

[quote=“VallejoIII, post:36, topic:190754”]Hello world burger!

It looks like your EnergyMonitor.lua is missing the bottom part of the code.

There is a section of code where it states:
– You shouldn’t need to change anything below this line –

The code ends with:
_G.EnergyMonitorOnTimer = EnergyMonitorOnTimer
InitWatch()[/quote]

My apologies I forgot to mention I truncated the code to only the top section where you modify it yourself. I’ve edited/updated my post to reflect that.

Any thoughts on why I’m having trouble getting it to function as expected?

Bonus: I’m also unable to get my USB flash drive to mount, any ideas? :slight_smile:

[quote=“worldburger, post:35, topic:190754”]EDIT: Added complete EnergyMonitor.lua code
///////////////////////////////////////////////////

I’m having some trouble getting this to work.

My objective is to:
Measure/log energy consumption on my house. (Do I need two CT clamps or just one for this? Am in the USA)
Measure/log energy consumption on my 240v kiln. (Do I need two CT clamps or just one for this? Am in the USA)
I have TWO - Aeon Home Energy Monitor (v1)'s (one for the breaker box and one for the kiln). Presently only one is setup as I am trying to get the house consumption data working first…which is troublesome :slight_smile:

Here’s where I’m at…
I’ve successfully got the Aeon Home Energy Monitor (v1) to talk to my VeraPlus. I can see the actual Watts and KWH on the Vera Dashboard. I’ve also go the VeraPlus talking to Emoncms (it sends my INPUTs to Emoncms with the keys I’ve entered). I’ve also created a FEED in Emoncms.

Unfortunately, on Emoncms I am only seeing ZEROS (“0”) as my inputs. What could be the problem? Any ideas?

Setup:
VeraPlus. UI7 (latest update). Aeon Home Energy Monitor (v1) with two current clamps powered by USB (1s update interval to VeraPlus).

Questions:
Is “device name” in the VeraPlus UI the “key” needed in the VARIABLES section of the EnergyMonitor.lua file?
Where do you find the specific “serviceVar” for the devices?

Which of the following do I need (or can I) to publish to Emoncms?
My Aeon Home Energy Monitor shows up as 3 devices…
1st is the total Watts & KWH (in Vera UI, I named this “totalwkwh”)
2nd is Watt & KWH from 1st CT Clamp reading (in Vera UI, I named this “hema”)
3nd is Watt & KWH from 2nd CT Clamp reading (in Vera UI, I named this “hemb”)

Screenshots are attached.
Normally, I’m pretty capable with mild programming and setup, but this is stumping me!

Thanks in advance :slight_smile:

My code is below:
—Startup Lua—

[code]module(“EnergyMonitor”, package.seeall)

– Setup your account at http://emoncms.org/
– See API documentation at Emoncms - input api

– API Key
local API_KEY = “REMOVED FOR POST”

– 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=“totalwkwh”, deviceId = 065, serviceId=‘urn:schemas-micasaverde-com:device:PowerMeter:1’, serviceVar=“Watts” }, – Send Joined Device data
{ key=“hema”, deviceId = 67, serviceId=‘urn:schemas-micasaverde-com:device:PowerMeter:1’, serviceVar=“Watts” }, – Send CT 1 device data
{ key=“hema”, deviceId = 67, serviceId=‘urn:schemas-micasaverde-com:device:PowerMeter:1’, serviceVar=“CurrentLevel” }, – Send CT 1 device data
{ key=“hemb”, deviceId = 66, serviceId=‘urn:schemas-micasaverde-com:device:PowerMeter:1’, serviceVar=“Watts” }, – Send CT 2 device data
{ key=“hemb”, deviceId = 66, serviceId=‘urn:schemas-micasaverde-com:device:PowerMeter:1’, serviceVar=“CurrentLevel” }, – Send CT 2 device data
}

– Add the following to your Vera’s Startup Lua (without the preceding dashes) to run the logging on every Vera restart
– emoncode = require(“EnergyMonitor”)
– emoncode.EnergyMonitorOnTimer()

local NODE_ID = 10
local TOTAL_KEY = ‘Total’

– Upload Frequency in seconds ORIGINALLY 60
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

_G.EnergyMonitorOnTimer = EnergyMonitorOnTimer
InitWatch()
[/code][/quote]

Any ideas, people? :slight_smile:

hi. I found Energymonitor.lua file in other forum because i can’t find it on emocms website. they remove it? By the way this is the code im running, but i have no input at all on emocms, already add on startup : emoncode = require(“EnergyMonitor”)
emoncode.EnergyMonitorOnTimer()

it gives "error on scenes and events " how can i compile lua code to check errors?i never used lua code

i didn’t understand the node part? on code say node10?

[code]module(“EnergyMonitor”, package.seeall)

– Setup your account at http://emoncms.org/
– See API documentation at Emoncms - input api

– API Key
local API_KEY = b24925 […my key…] dd9f775

– 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

– { 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

local VARIABLES = {
–{ key=“PoolFilter”, deviceId = 869, serviceId=‘urn:micasaverde-com:serviceId:EnergyMetering1’, serviceVar=“Watts” }, – Send device energy
{ key=‘ValvulaRega1’, deviceId=105, serviceId=“urn:upnp-org:serviceId:SwitchPower1”, serviceVar=“Status”}, – Send switch status
{ key=‘TempExterior’, deviceId=111, serviceId=“urn:upnp-org:serviceId:TemperatureSensor1”, serviceVar=“CurrentTemperature”}, – Send Temperature
{ key=‘HumidadeTerra’, deviceId=109, serviceId=“urn:micasaverde-com:serviceId:HumiditySensor1”, serviceVar=“CurrentLevel”}, – Send Humidity
–{ key=‘FrontLight’, deviceId=740, serviceId=“urn:micasaverde-com:serviceId:LightSensor1”, serviceVar=“CurrentLevel”} – Send Light Level
}

– Add the following to your Vera’s Startup Lua (without the preceding dashes) to run the logging on every Vera restart
– emoncode = require(“EnergyMonitor”)
– emoncode.EnergyMonitorOnTimer()

local NODE_ID = 10
local TOTAL_KEY = ‘Total’

– Upload Frequency in seconds

– 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

_G.EnergyMonitorOnTimer = EnergyMonitorOnTimer
InitWatch()
[/code]

ok. its working. i just forget " " on api key ::).

So there is the full code working with with,temperature and humidity sensors.

[code]module(“EnergyMonitor”, package.seeall)

– Setup your account at http://emoncms.org/
– See API documentation at Emoncms - input api

– API Key
local API_KEY = “b24925 […CHANGE HERE YOUR API key…] dd9f775”

– 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

– { 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

local VARIABLES = {
{ key=“PoolFilter”, deviceId = 869, serviceId=‘urn:micasaverde-com:serviceId:EnergyMetering1’, serviceVar=“Watts” }, – Send device energy
{ key=‘ValvulaRega1’, deviceId=105, serviceId=“urn:upnp-org:serviceId:SwitchPower1”, serviceVar=“Status”}, – Send switch status
{ key=‘TempExterior’, deviceId=111, serviceId=“urn:upnp-org:serviceId:TemperatureSensor1”, serviceVar=“CurrentTemperature”}, – Send Temperature
{ key=‘HumidadeTerra’, deviceId=109, serviceId=“urn:micasaverde-com:serviceId:HumiditySensor1”, serviceVar=“CurrentLevel”}, – Send Humidity
{ key=‘FrontLight’, deviceId=740, serviceId=“urn:micasaverde-com:serviceId:LightSensor1”, serviceVar=“CurrentLevel”} – Send Light Level
}

– Add the following to your Vera’s Startup Lua (without the preceding dashes) to run the logging on every Vera restart
– emoncode = require(“EnergyMonitor”)
– emoncode.EnergyMonitorOnTimer()

local NODE_ID = 10
local TOTAL_KEY = ‘Total’

– Upload Frequency in seconds

– 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

_G.EnergyMonitorOnTimer = EnergyMonitorOnTimer
InitWatch()[/code]