Page 1 of 1

dzVents implementation of identifying user via domoticz log

Posted: Friday 15 June 2018 2:40
by waaren
The dzVents variant of the Lua script of @jdvz for @ericP to enable an Email when someone switched a device. This script uses the async openURL function of dzVents to get the latest logmessages.

Thanks to Jos and Eric.

Code: Select all

-- getUserFromLog.lua 

triggerDevice = 969  -- define the switch you want to monitor

return {
    on      =   {   httpResponses   =   { "getDomoticzLogLines" },
                    devices         =   { triggerDevice}},
                    
    logging =   {   level     =   domoticz.LOG_INFO,
                    marker    =   "getuserFromLog"    },

    execute = function(dz, trigger)
        
        local function askDomoticzForLogLines()
            local lastLogTime = os.time(os.date('*t')) - 10    -- current Time as seconds from epoch  - 10 should get enough logging
            local logLevel    = 2                              -- loglevel (1=normal,2=Status,4=error,268435455=all
            local jsonString  = "/json.htm?type=command&param=getlog&" ..
                                "lastlogtime=" .. tostring(lastLogTime) ..
                                "&loglevel=" .. logLevel
   
            dz.openURL  ({ url      = dz.settings["Domoticz url"] .. jsonString,   -- loglevel 2 ==>> Status:logURL,
                           method   = "GET", callback = "getDomoticzLogLines" })
        end
        
        -- Get information recent log entries for switch action
        function getSwitchedByInfo(device)
           local resultTable = trigger.json.result
           for i = #resultTable,1,-1 do                  -- Read backwards to get latest switchoperation first
                if      string.find(resultTable[i].message,device.name) and                             -- Look for Device name , update Time 
                        string.find(resultTable[i].message,device.lastUpdate.rawTime) and               -- and strings User: and initiated...
                        string.find(resultTable[i].message,"User:") and
                        string.find(resultTable[i].message,"initiated a switch command") then
                        return      resultTable[i].message:sub(resultTable[i].message:find("User:")+6,  -- Return username 
                                    resultTable[i].message:find("initiated a switch command")-1) 
                end
            end
            return "Unknown- or local user or System"   -- This might happen when a user has logged in domoticz from a network defined in 
        end                                             -- [SETUP] -->> [SETTINGS] -->> [SYSTEM] -->> [LOCAL NETWORKS (NO USERNAME/PASSWORD):] 
                                                        -- Or switch is activated via an event / schedule / etc
                                                        
        
        if trigger.isDevice then
            askDomoticzForLogLines()
        else     
            dz.email (  "Alarm notification " , dz.devices(triggerDevice).name .. " has been turned " .. 
                                                dz.devices(triggerDevice).state .. " by:  " .. 
                                                getSwitchedByInfo(dz.devices(triggerDevice))  , 
                                                "[email protected]" )     
       end
    end
}
 

Re: dzVents implementation of identifying user via domoticz log

Posted: Monday 18 June 2018 11:07
by DewGew
waaren wrote: Friday 15 June 2018 2:40 The dzVents variant of the Lua script of @jdvz for @ericP to enable an Email when someone switched a device. This script uses the async openURL function of dzVents to get the latest logmessages.

Thanks to Jos and Eric.
This script works great