I have a problem where my script produces an e-mail when it's not supposed to, with partly unexpected content. I have implemented a simple freezer alarm with 5 Proove 433 MHz thermometers, a RFXcom, a Windows 7 computer, Domoticz 2.3563 and Lua. The main part consists of a device script that monitors the reported temperatures and raises an alarm if out of bounds, which works fine. In addition to this there is a watchdog time script that should check that the thermometers have reported recently, and otherwise should notify me by e-mail.
The problem is that an e-mail is always sent, regardless of if the conditions that lead there are true, and the content is incorrect.
Script: script_time_checkdevices.lua
Code: Select all
¨-- Checks periodically if all sensors have been received recently, and
-- otherwise sends an e-mail to tell us to go check the lost one.
CheckInterval = 10 -- How often we should check for lost devices(min)
DeviceTimeout = 30*60 -- After how long time a device is considered lost (s)
MessageInterval = 20 --8*3600 -- Minimum interval between e-mails (s)
MessageEmail = '[email protected];[email protected]' -- Where messages should be sent.
commandArray = {}
now = os.time()
-- Run every 'CheckInterval' as long no e-mail has already been sent the last 'MessageInterval' seconds
if (os.date('%M') % CheckInterval) == 0 then
print('Checking that sensors are alive...')
for i, v in pairs(otherdevices) do print(otherdevices_lastupdate[i]) end -- Debug
-- Get last update time for each device
for i, v in pairs(otherdevices) do
s = otherdevices_lastupdate[i]
print(i) -- Debug
print(s) -- Debug
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)
lastAlive = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
-- If not seen since timeout, send an email
if (lastAlive + DeviceTimeout) < now then
print(i .. ' has not been seen since ' .. s .. '.')
print('Debug msg 1')
-- If an email was sent recently, don't send one
if uservariables['MessageTime'] + MessageInterval < now then
subject = i .. ' sensor has been lost'
message = 'The temperature sensor ' .. i .. ' has not been seen since '.. s .. '. Now it´s ' .. os.date('%c', now) .. '. The sensor is maybe out of batteries?'
print('Debug msd 2: ' .. s)
commandArray['SendEmail'] = subject .. '#' .. message .. '#' .. MessageEmail
commandArray['Variable:MessageTime'] = tostring(os.time())
end
end
end
if next(commandArray) == nil then print ('All OK.') end
end
return commandArray
Code: Select all
2015-11-19 14:20:00.222 LUA: Checking that sensors are alive...
2015-11-19 14:20:00.223 LUA: 2015-11-19 14:19:40
2015-11-19 14:20:00.223 LUA: 2015-11-19 14:19:41
2015-11-19 14:20:00.223 LUA: 2015-11-19 14:18:42
2015-11-19 14:20:00.223 LUA: 2015-11-19 14:19:42
2015-11-19 14:20:00.223 LUA: 2015-11-19 14:19:36
2015-11-19 14:20:00.223 LUA: Freezer5
2015-11-19 14:20:00.224 LUA: 2015-11-19 14:19:40
2015-11-19 14:20:00.224 LUA: Freezer2
2015-11-19 14:20:00.224 LUA: 2015-11-19 14:19:41
2015-11-19 14:20:00.224 LUA: Freezer3
2015-11-19 14:20:00.224 LUA: 2015-11-19 14:18:42
2015-11-19 14:20:00.224 LUA: Freezer1
2015-11-19 14:20:00.224 LUA: 2015-11-19 14:19:42
2015-11-19 14:20:00.224 LUA: Freezer4
2015-11-19 14:20:00.224 LUA: 2015-11-19 14:19:36
2015-11-19 14:20:00.224 LUA: All OK.
The strange things, to my understanding, areFreezer2 sensor has been lost
[email protected] <[email protected]> 20 november 2015 01:20
Svara: [email protected]
Till: [email protected]
Kopia: [email protected]
The temperature sensor Freezer2 has not been seen since 2015-11-19 11:41:17. Now it´s Thu Nov 19 14:20:00 2015. The sensor is maybe out of batteries?
- The e-mail is sent although the condition "(lastAlive + DeviceTimeout) < now" is false (line 32)
- "s" is written as another time in the e-mail than it's otherwise defined to, as seen in the log entry. 2015-11-19 11:41:17 is roughly when the system was restarted. Before this restart it printed the time of restart before that.
- "All OK" at the end of the log is printed (line 47), even though this is only supposed to be printed if the "commandArray" is empty
- "Debug msg 1" and "Debug msg 2" (lines 34-35 & 40) are not printed to the log, although the e-mail is sent
For some time I thought that the Lua interpreter or Domoticz server somehow kept the command array or the e-mail content in cache between the runs, but if I edit the "message" string, the content of the e-mail changes. The string "s" is still however written out as "2015-11-19 11:41:17".
Any thoughts on this?
Cheers, Arvid