Another way to monitor device and send notification

Moderator: leecollings

Post Reply
PieterS
Posts: 197
Joined: Wednesday 31 May 2017 16:06
Target OS: NAS (Synology & others)
Domoticz version: 2024.7
Location: NL
Contact:

Another way to monitor device and send notification

Post by PieterS »

Last week I got an notification from Lufdaten.info that my sensor was down for 24 hours.... So I made this script with DZ-vents. It scans every 5 minuts the log of Domoticz and if necassary it sends a notification with Pushover to my phone. :D

Trigger was another topic on this site. https://www.domoticz.com/forum/viewtopi ... RL#p184824
So this is a rebuild from that script. But is does a good job.. Maybe it could be better...

Code: Select all

-- status fijnstofmeter.lua

myHttpResponse = "getDomoticzErrorLog"

return {
    on      =   {   httpResponses   =   { myHttpResponse },
                    timer           =   { "every 5 minutes"},
                --    devices         =   { "getErrorTrigger"}   -- Only used for development and testing
                },

    logging =   {   level           =   domoticz.LOG_INFO,
                    marker          =   " status http://192.168.xx.xx/data.json" },

    data    =   {   lastlogtime     =   { initial = 0 }}, 

    execute = function(dz, trigger)
        
        local violationMessage = "Error opening url: http://192.168.1.41/data.json"      -- Change to reflect the messages you want to catch
        
        local function askDomoticzForLogLines()
            local lastLogTime = dz.data.lastlogtime            -- get time last logrequest
            local logLevel    = 4                              -- loglevel (1=normal,2=Status,4=error,268435455=all
            local jsonString  = "/json.htm?type=command" .. 
                                "&param=getlog" ..
                                "&lastlogtime=" .. tostring(lastLogTime) ..
                                "&loglevel=" .. tostring(logLevel)
            
            local logURL      =  dz.settings["Domoticz url"] .. jsonString
            
            dz.openURL  ({ url      = logURL,
                           method   = "GET", 
                           callback = myHttpResponse 
                        })

            dz.data.lastlogtime = os.time(os.date('*t'))        -- store current Time as seconds from epoch  
            dz.log(logURL,dz.LOG_DEBUG)
        end

        local function findViolation()
           local violation = ""
           if trigger.json.result ~= nil then               -- Only when there are errormessages in the log
               local resultTable = trigger.json.result
               for i = #resultTable,1,-1 do                 -- traverse backwards to get latest violation first
                    if      string.find(resultTable[i].message,violationMessage) and
                        not(string.find(resultTable[i].message,"dzVents")) then     -- We don't want debug messages to interfere
                            violation = violation .. "\n".. resultTable[i].message
                    end
                end
            end
            return violation
        end

        if trigger.isDevice or trigger.isTimer then
            askDomoticzForLogLines()                     -- get the relevant loglines
        elseif trigger.ok then
            local violation = findViolation()
            if violation == "" then
                dz.log("No (new) violations found",dz.LOG_DEBUG)
            else    
                dz.log("violation(s) found: " .. violation,dz.LOG_INFO)
                dz.notify("fijnstofmeter offline!!",violation, dz.PRIORITY_NORMAL,nil,nil,dz.NSS_PUSHOVER)
            end    
        else
            dz.log("Access problem to log",dz.LOG_ERROR)
       end
    end
}
Synology with Domoticz build (V2024.7) in Docker
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest