Virtual device attempt to concatenate a nil value (field 'v')
Posted: Friday 25 October 2024 10:33
Hi All,
I have a virtual device which keeps track of my monthly total usage (general managed counter) idx 204.
All of a sudden my script stopped working with the following error:
2024-10-25 10:17:11.025 Error: dzVents: An error occurred when calling event handler MaandVerbruik
2024-10-25 10:17:11.025 Error: dzVents: ...data/scripts/dzVents/generated_scripts/MaandVerbruik.lua:36: attempt to concatenate a nil value (field 'v')
bleow the script:
local httpResponses = "monthTotalVerbruik"
return {
on = {
timer = { "every hour" },
--devices = {64},
httpResponses = { httpResponses .. "*" }
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(64) -- Replace xxxx with ID of energyDevice you want to track
monthTotal = dz.devices(204) -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
-- ****************************** No changes required below this line *********************************************
local function logWrite(str,level)
dz.log(tostring(str),level or dz.LOG_DEBUG)
end
local function triggerJSON(id, period, delay)
local delay = delay or 0
local URLString = "http://192.168.1.7:8080/json.htm?type=g ... ter&range=" ..
period .. "&idx=" .. id
dz.openURL({ url = URLString,
method = "GET",
callback = httpResponses .. "_" .. period}).afterSec(delay)
end
local function calculateMonthTotal(rt)
local monthTotal = 0
local currentMonth = dz.time.rawDate:sub(1,7)
for id, result in ipairs(rt) do
if rt[id].d:sub(1,7) == currentMonth then
logWrite(rt[id].d .. " ==>> " .. rt[id].v)
monthTotal = monthTotal + rt[id].v + rt[id].v2
end
end
return monthTotal * 1000
end
if not item.isHTTPResponse then
triggerJSON(usageDevice.id, "month")
elseif item.ok then -- statusCode == 2xx
monthTotal.update(0,calculateMonthTotal(item.json.result))
else
logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")" ,dz.LOG_ERROR)
logWrite(item.data)
end
end
}
I cannot determine what is wrong and why it all of a sudden keeps giving me this error as of Yesterday, all the help appreciated.
Thanks,
Hemant
I have a virtual device which keeps track of my monthly total usage (general managed counter) idx 204.
All of a sudden my script stopped working with the following error:
2024-10-25 10:17:11.025 Error: dzVents: An error occurred when calling event handler MaandVerbruik
2024-10-25 10:17:11.025 Error: dzVents: ...data/scripts/dzVents/generated_scripts/MaandVerbruik.lua:36: attempt to concatenate a nil value (field 'v')
bleow the script:
local httpResponses = "monthTotalVerbruik"
return {
on = {
timer = { "every hour" },
--devices = {64},
httpResponses = { httpResponses .. "*" }
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
usageDevice = dz.devices(64) -- Replace xxxx with ID of energyDevice you want to track
monthTotal = dz.devices(204) -- Create as virtual managed counter (energy) and change yyyy to the ID of the new device
-- ****************************** No changes required below this line *********************************************
local function logWrite(str,level)
dz.log(tostring(str),level or dz.LOG_DEBUG)
end
local function triggerJSON(id, period, delay)
local delay = delay or 0
local URLString = "http://192.168.1.7:8080/json.htm?type=g ... ter&range=" ..
period .. "&idx=" .. id
dz.openURL({ url = URLString,
method = "GET",
callback = httpResponses .. "_" .. period}).afterSec(delay)
end
local function calculateMonthTotal(rt)
local monthTotal = 0
local currentMonth = dz.time.rawDate:sub(1,7)
for id, result in ipairs(rt) do
if rt[id].d:sub(1,7) == currentMonth then
logWrite(rt[id].d .. " ==>> " .. rt[id].v)
monthTotal = monthTotal + rt[id].v + rt[id].v2
end
end
return monthTotal * 1000
end
if not item.isHTTPResponse then
triggerJSON(usageDevice.id, "month")
elseif item.ok then -- statusCode == 2xx
monthTotal.update(0,calculateMonthTotal(item.json.result))
else
logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")" ,dz.LOG_ERROR)
logWrite(item.data)
end
end
}
I cannot determine what is wrong and why it all of a sudden keeps giving me this error as of Yesterday, all the help appreciated.
Thanks,
Hemant