Maybe this can be an option for you.
viewtopic.php?p=273355#p273355
This a script which reads out the log and send a message.
i've adjusted myself to send messages by Prowl
for example
Code: Select all
local SCRIPT_VAR = 'ErrorLogCallback'
local SCRIPT_NAME = 'ErrorLog'
local LOG_LEVEL = domoticz.LOG_STATUS -- zet op LOG_ERROR bij productie
return {
on = {
httpResponses = { SCRIPT_VAR },
timer = { "every hour" },
-- devices = { 290 },
},
logging = {
level = LOG_LEVEL,
marker = SCRIPT_NAME
},
data = {
lastlogtime = { initial = 0 }
},
execute = function(dz, item)
--------------------------------------------------------------------
-- Zoeken naar foutmeldingen in Domoticz log
--------------------------------------------------------------------
local searchTerms = { "Error", "Warning", "Failure" }
local ignoreStrings = {
"CheckAuthToken", "dzVents: Info:", "dzVents: !Info:", "dzVents: Debug:",
"transport", "http", "Discarding device", "CConnection_connect",
"PARSE", "KeyError", "nan"
}
local function isRelevant(str)
if not str or str == "" then return false end
for _, ignore in ipairs(ignoreStrings) do
if str:find(ignore) then return false end
end
for _, term in ipairs(searchTerms) do
if str:find(term) then return true end
end
return false
end
--------------------------------------------------------------------
-- Log opvragen (lastlogtime direct vastzetten!)
--------------------------------------------------------------------
local function requestLog()
local baseUrl = dz.settings['Domoticz url']
local previousTime = dz.data.lastlogtime
local now = os.time()
local url = string.format(
"%s/json.htm?type=command¶m=getlog&lastlogtime=%d&loglevel=4",
baseUrl,
previousTime
)
dz.openURL({ url = url, callback = SCRIPT_VAR })
-- BELANGRIJK: direct opslaan → crash-/restart-safe
dz.data.lastlogtime = now
end
--------------------------------------------------------------------
-- Extract recente logregels
--------------------------------------------------------------------
local function findLogMessage(json)
if json and json.result and type(json.result) == "table" then
for i = #json.result, 1, -1 do
local msg = json.result[i].message
if isRelevant(msg) then return msg end
end
end
return nil
end
--------------------------------------------------------------------
-- Timestamp loskoppelen van de rest
--------------------------------------------------------------------
local function splitAfterTimestamp(msg)
local prefix, suffix = msg:match(
"^(%d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d[^\n]-:%s)(.+)"
)
if prefix and suffix then return prefix, suffix end
return msg, ""
end
--------------------------------------------------------------------
-- Schoonmaken van foutmeldingen (zonder interpretatie)
--------------------------------------------------------------------
local function prettyMessage(msg)
if not msg or msg == "" then return "" end
local ts, rest = msg:match(
"^(%d%d%d%d%-%d%d%-%d%d %d%d:%d%d:%d%d.-:%s)(.+)"
)
ts = ts or ""
rest = rest or msg
rest = rest:gsub('b"[^"]*"', "[data]")
rest = rest:gsub("{[^}]*}", "[details]")
rest = rest:gsub("%s+", " "):gsub("^%s*(.-)%s*$", "%1")
return ts .. rest
end
--------------------------------------------------------------------
-- Fallback parsing als JSON stukgelopen is
--------------------------------------------------------------------
local function manualParseFromRaw(raw)
if not raw or raw == "" then return nil end
if not (raw:find('"LastLogTime"') or raw:find('"GetLog"')) then return nil end
local lastRelevant = nil
for line in raw:gmatch("[^\r\n]+") do
if line:find('"message"') then
local msg = line:match('"message"%s*:%s*"(.*)"%s*,?%s*$')
if msg and isRelevant(msg) then
lastRelevant = msg
end
end
end
return lastRelevant
end
--------------------------------------------------------------------
-- HOOFDLOGICA
--------------------------------------------------------------------
if item.isDevice or item.isTimer then
requestLog()
elseif item.isHTTPResponse then
if not item.ok then
dz.log(
"HTTP fout bij ophalen log. Statuscode: " ..
tostring(item.statusCode),
dz.LOG_ERROR
)
return
end
if item.json then
local found = findLogMessage(item.json)
if found then
local p1, p2 = splitAfterTimestamp(found)
local pretty = prettyMessage(p1 .. p2)
dz.log("Relevante foutmelding gevonden: " .. pretty, dz.LOG_FORCE)
dz.notify("Domoticz Foutmelding", pretty, dz.PRIORITY_HIGH)
end
else
local rawData = item.data or ""
if rawData:find("<!DOCTYPE html>") then
dz.log(
"HTML response (loginpagina). Voeg 127.0.0.1 toe aan 'Lokale Netwerken'.",
dz.LOG_ERROR
)
return
end
local manualMsg = manualParseFromRaw(rawData)
if manualMsg then
local pretty = prettyMessage(manualMsg)
dz.log("Relevante foutmelding (fallback): " .. pretty, dz.LOG_FORCE)
dz.notify("Domoticz Foutmelding", pretty, dz.PRIORITY_HIGH)
else
dz.log("HTTP response bevat geen geldige JSON.", dz.LOG_ERROR)
end
end
end
end
}