OK, here’s a revision I’ve made, and I’ll go over each bit in comparison to yours:
local variables = {"Watts"}
local NRG_SER = "urn:micasaverde-com:serviceId:EnergyMetering1"
local total = 0
for lul_device in pairs(luup.devices) do
local readings = {} -- create empty array called readings
for _, variable in ipairs(variables) do -- using the id and value in variables table above
local value = luup.variable_get(NRG_SER, variable, lul_device)
table.insert( readings, { variable, tonumber(value) } -- populate readings table
end
for _, reading in ipairs(readings) do
local Variable = reading[1]
local Value = reading[2] or 0
total = total + Value
print(Variable.. " - ".. Value)
end
end
print("Total watts = " ..total)
- Since you don’t use
attr
in the loop, you can just drop it.
-
variables
is an array, so you should use ipairs()
to iterate over it.
- In this particular usage, we don’t need to care about the array index looping over
variables
, so we use “_” as a placeholder for a discarded value (the array index).
- Use
table.insert()
to add elements to the (new) readings
array rather than an index from an unrelated array.
- Since
readings
is an array, we’ll use ipairs()
to iterate over it.
- By declaring
total
as local
in the second loop interior, you excluded the global scope of the previous declaration of total
so it could not total up properly. You don’t want to make a new local variable, so don’t use the local
keyword/declaration.
- Since you’ve already
tonumber()
'd the state variable value before putting it into readings
, you don’t need to do that again or dance around it with extra tests. The only thing needed is the or 0
to make sure that if the tonumber()
failed ended up returning nil, you get 0 as a default/alternative value.
Finally, since you are only totaling one state variable, Watts
, there’s a lot of gyration here for little benefit. You could eliminate the first loop entirely and put the fetch of Watts directly in the second loop. If you intend to add more variables in future, then you have a different problem: total
will end up totaling all of those different variables, even though they may have nothing to do with one-another (e.g. totaling Watts
and KWH
together in one value makes no sense). So fix that, your code would need to look more like this:
local variables = {"Watts"}
local NRG_SER = "urn:micasaverde-com:serviceId:EnergyMetering1"
local total = {}
for lul_device, attr in pairs(luup.devices) do
local readings = {} -- create empty array called readings
for _, variable in ipairs(variables) do -- using the id and value in variables table above
local value = luup.variable_get(NRG_SER, variable, lul_device)
table.insert( readings, { variable, tonumber(value) } -- populate readings table
end
for _, reading in pairs(readings) do
local Variable = reading[1]
local Value = reading[2] or 0
total[Variable] = (total[Variable] or 0) + Value
print(Variable.. " - ".. Value)
end
end
for v,d in pairs(total) do
print("Total "..v.." = "..d)
end
Consider this pseudo-code. There may be small errors that need to be fixed; I haven’t run it, I’m just winging it here.