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

Offline jamac

  • Jr. Member
  • **
  • Posts: 63
  • Karma: +0/-1
Re: Contribution: Monitor and visualize energy usage with emoncms.org in Vera
« Reply #30 on: December 27, 2016, 01:58:26 am »
I'm struggling to get this working and am hoping for some help.  :)

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

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

Code: [Select]
_G.EnergyMonitorOnTimer = EnergyMonitorOnTimer
And, this in the Edit Startup Lua window:
Code: [Select]
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?
« Last Edit: December 27, 2016, 02:02:04 am by jamac »

Offline jamac

  • Jr. Member
  • **
  • Posts: 63
  • Karma: +0/-1
Re: Contribution: Monitor and visualize energy usage with emoncms.org in Vera
« Reply #31 on: January 03, 2017, 01:28:56 am »
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...

« Last Edit: January 05, 2017, 08:36:25 pm by jamac »

Offline VallejoIII

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
Re: Contribution: Monitor and visualize energy usage with emoncms.org in Vera
« Reply #32 on: February 27, 2017, 05:37:09 pm »
No longer working after upgrade to version 1.7.919.

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

Offline VallejoIII

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
UPDATE:

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

Fixed and working. Syntax error!
« Last Edit: March 05, 2017, 04:30:43 pm by VallejoIII »

Offline worldburger

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
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 :)

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 :)

My code is below:
---Startup Lua---
Code: [Select]
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 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()
« Last Edit: April 05, 2017, 07:45:30 pm by worldburger »

Offline VallejoIII

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
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()

Offline worldburger

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
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()

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? :)

Offline worldburger

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
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 :)

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 :)

My code is below:
---Startup Lua---
Code: [Select]
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 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()

Any ideas, people? :)

Offline tmaster

  • Newbie
  • *
  • Posts: 10
  • Karma: +0/-0
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: [Select]
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 = 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()
« Last Edit: April 30, 2017, 08:22:04 am by tmaster »

Offline tmaster

  • Newbie
  • *
  • Posts: 10
  • Karma: +0/-0
ok. its working. i just forget " " on api key  ::).

So there is the full code working with with,temperature and humidity sensors.
Code: [Select]
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 = "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()


Offline worldburger

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
Can someone tell me what is wrong with this code? I can get the inputs to post to emoncms but the variables all post "0".  Where is my mistake?

Notes: I have setup the startup Lua code as directed. Both of my deviceId's match the two Aeon sensors I have (the main device, not the CT clamps).

Code: [Select]
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()

Offline VallejoIII

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
Here is my code if it can help in finding the error.

Cheers!
« Last Edit: May 20, 2017, 06:08:17 pm by VallejoIII »

Offline peterv

  • Sr. Newbie
  • *
  • Posts: 22
  • Karma: +0/-0
I followed RTS's instructions and added the following to the start of the EnergyMonitor.lua file
Code: [Select]
module("EnergyMonitor", package.seeall)

This to the end of the file:
Code: [Select]
_G.EnergyMonitorOnTimer = EnergyMonitorOnTimer
And, this in the Edit Startup Lua window:
Code: [Select]
emoncode = require("EnergyMonitor")
emoncode.EnergyMonitorOnTimer()

I don't think that all three things are necessary. The first two should do the job. When I add emoncode.EnergyMonitorOnTimer() in start up lua, I can see two consecutive post requests in the log. Removing that line still keeps the script working.