Erro attempt to concatenate a nil value (global 'device') Topic is solved

Moderator: leecollings

Post Reply
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Erro attempt to concatenate a nil value (global 'device')

Post by JanvdW »

To check if my Zigbee devices are still the alive I implemented a script as described here.

Code: Select all

local devices = {  "Achter beweging",
                    "Badkamer beweging",
                    "Overloop beweging",
                    "Badkamer temp en vocht",
                    "Douche temp en vocht",
                    "Plug koelkast",
                    "Plug vriezer" }
no_data_timer = 300 	                        -- Date time-out in minutes
email_address = "[email protected]"	    	-- Mailadres for e-mail notifications; empty for standard notifications

function datetimedifferencenow(s)            -- Function to calculate difference between given timestamp (s) and current time
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end

time = os.date("*t")
commandArray = {}

-- Once a failing device is detected (meaning no update received in the last <no_data_timer> minutes), this device is stored in the user variable "No update from sensor" and a notification is sent
-- To prevent a notification storm, no update is send out as long as the user variable contains a value
if (uservariables["No update from sensor"] ~= tostring("")) then
    if (datetimedifferencenow(otherdevices_lastupdate[uservariables["No update from sensor"]]) < no_data_timer*60) then
        commandArray["Variable:No update from sensor"] = tostring("")
		if (email_address == tostring("")) then
			commandArray["SendNotification"] = "Sensor \"" .. uservariables["No update from sensor"] .. "\" verstuurt weer data."
			print ("Received data from sensor \"" .. uservariables["No update from sensor"] .. "\" again. Notification was sent.")
		else
			commandArray["SendEmail"]="Domoticz sensor waarschuwing#Sensor \"" .. device .. "\" verstuurt weer data.#" .. email_address
			print ("Received data from sensor \"" .. uservariables["No update from sensor"] .. "\" again. Email was sent.")
		end
    end
end

-- If there is no device stored in the user variable "No update from sensor", all devices are checked if they send data in the last <no_data_timer> minutes.
if (uservariables["No update from sensor"] == tostring("")) then
    for i,device in pairs(devices) do
        -- If check device did not give an update in the last <no_data_timer> minutes then send notification and write device to user variable "No update from sensor".
        if (datetimedifferencenow(otherdevices_lastupdate[device]) >= no_data_timer*60) then
            commandArray["Variable:No update from sensor"] = tostring(device)
            if (email_address == tostring("")) then
				commandArray["SendNotification"] = "Sensor \"" .. device .. "\" heeft meer dan " .. no_data_timer .. " minuten geen data verstuurd."
				print ("No data received from sensor \"" .. device .. "\" for more than " .. no_data_timer .. " minutes. Notification was sent.")
			else
				commandArray["SendEmail"]="Domoticz sensor time-out#Sensor \"" .. device .. "\" heeft meer dan " .. no_data_timer .. " minuten geen data verstuurd.#" .. email_address
				print ("No data received from sensor \"" .. device .. "\" for more than " .. no_data_timer .. " minutes. Notification sent. Email was sent.")
			end
            
            break -- stop the for loop.
        end
    end
end

-- To make sure a failing sensor is not forgotten, every morning at 8:00 a notification is send if the sensor is failing for > 24 hours.
if (uservariables["No update from sensor"] ~= tostring("") and time.hour==8 and time.min==0) then
    if (datetimedifferencenow(otherdevices_lastupdate[uservariables["No update from sensor"]]) >= 86400) then
		if (email_address == tostring("")) then
			commandArray["SendNotification"] = "Ter herinnering, sensor \"" .. uservariables["No update from sensor"] .. "\" verstuurt nog steeds geen data."
			print ("Reminder notification was sent for sensor \"" .. uservariables["No update from sensor"] .. "\" that it is not sending data for more than 24 hours.")
		else
			commandArray["SendEmail"]="Domoticz sensor time-out reminder#Ter herinnering, sensor \"" .. uservariables["No update from sensor"] .. "\" verstuurt nog steeds geen data.#" .. email_address
			print ("Reminder email was sent for sensor \"" .. uservariables["No update from sensor"] .. "\" that it is not sending data for more than 24 hours.")
		end
    end
end

return commandArray
Now and then an error appears in my Domoticz log and, despite of a lot of similar issues on the web I can't find what causes it:

Code: Select all

Error: EventSystem: in Status Zigbee sensors en plugs: [string "local devices = { "Achter beweging", ..."]:36: attempt to concatenate a nil value (global 'device')
The scripts triggers on time, as instructed. I tried to trigger on device, but that makes no difference I am wondering what can cause the error.
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Erro attempt to concatenate a nil value (global 'device')

Post by jvdz »

The issue is in your code. Field device in unknown in that part of the code so you need to change that. ;)
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Erro attempt to concatenate a nil value (global 'device')

Post by JanvdW »

You're right. I overlooked this :(. Thanks for setting me on the right track.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest