I’ve got some LUA code running on a Vera Plus. Unfortunately, the variables all post “0” values.
Any ideas? Below is the Lua code from my Vera Plus device. I’ve also included a link to the Vera forum (where no one seems to read/reply).
module("EnergyMonitor", package.seeall)
-- Setup your account at http://emoncms.org/
-- See API documentation at http://emoncms.org/input/api
-- API Key
local API_KEY = "REMOVED FOR SECURITY REASONS"
-- 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="HEM", deviceId = 155, serviceId='urn:schemas-micasaverde-com:device:PowerMeter:1', serviceVar="Watts" },
{ key="HEM", deviceId = 155, serviceId='urn:schemas-micasaverde-com:device:PowerMeter:1', serviceVar="CurrentLevel" },
{ key="kilnwelder", deviceId = 135, serviceId='urn:schemas-micasaverde-com:device:PowerMeter:1', serviceVar="Watts" },
{ key="kilnwelder", deviceId = 135, serviceId='urn:schemas-micasaverde-com:device:PowerMeter:1', serviceVar="CurrentLevel" },
{ key='TestVar', calculate=function() return 15 end, serviceVar="dsmtest" } -- Send a constant value
}
-- 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()
http://forum.micasaverde.com/index.php/topic,35953.msg321520.html#msg321520