dzVents implementation of identifying user via domoticz log

Post Reply
User avatar
waaren
Posts: 1237
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

dzVents implementation of identifying user via domoticz log

Post by waaren » 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.

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))  , 
                                                "youremaill@hotmail.com" )     
       end
    end
}
 
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

User avatar
DewGew
Posts: 264
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi
Domoticz version: V4.9741
Location: Sweden
Contact:

Re: dzVents implementation of identifying user via domoticz log

Post by DewGew » Monday 18 June 2018 11:07

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
Raspberry Pi 2B - domoticz - Aeon Labs Z-Stick GEN5 - RFlink gateway
NanoPi NEO-air - domoticz - REGO6XX interface
Dashticz V2 - Homebridge

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest