lastUpdate.minutesAgo for security state - how?  [Solved]

Easy to use, 100% Lua-based event scripting framework.
Post Reply
funnybu
Posts: 12
Joined: Monday 07 May 2018 14:48
Target OS: Raspberry Pi
Domoticz version: 3.9272
Location: Russia, Novosibirsk
Contact:

lastUpdate.minutesAgo for security state - how?

Post by funnybu » Tuesday 09 October 2018 11:41

Hello. I need get lastUpdate.minutesAgo (or hoursAgo) for security state.

For example i need switch boiler to standby mode if my Dz had "Armed Away" state more than 2 days...

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

Re: lastUpdate.minutesAgo for security state - how?

Post by waaren » Tuesday 09 October 2018 23:20

funnybu wrote:
Tuesday 09 October 2018 11:41
Hello. I need get lastUpdate.minutesAgo (or hoursAgo) for security state.

For example i need switch boiler to standby mode if my Dz had "Armed Away" state more than 2 days...
The field lastUpdate does not exist for the Domoticz Security Panel in dzVents. The workaround for this could be a script like

Code: Select all

-- security.lua
return { 
        on =        {   security = { domoticz.SECURITY_ARMEDAWAY, 
                                     domoticz.SECURITY_ARMEDHOME,
                                     domoticz.SECURITY_DISARMED },
                        
                        timer   =  {"every 10 minutes" }, 
                    },
                    
        data = { securityState = { history = true, maxItems = 1 }}, 
        
        logging =   {   level   =   domoticz.LOG_DEBUG,
                        marker  =   "Security" },
         
    
    execute = function(dz, item )
    
        if dz.data.securityState.get(1) == nil then
            dz.data.securityState.add("Initialized")          -- Initialize persistent historical variable (= data + timestamp)
            dz.log(item.trigger,dz.LOG_DEBUG)
        end 
        if item.isSecurity then 
            dz.data.securityState.add(item.trigger)         -- Store state to persistent historical variable (= data + timestamp)
        else
            safedSecurityState = dz.data.securityState.get(1)
            dz.log("Security state set to: " .. safedSecurityState.data .. ", " .. 
                    safedSecurityState.time.secondsAgo .." seconds ago.",dz.LOG_DEBUG)  -- retrieve state and set time secondsAgo 
        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

funnybu
Posts: 12
Joined: Monday 07 May 2018 14:48
Target OS: Raspberry Pi
Domoticz version: 3.9272
Location: Russia, Novosibirsk
Contact:

Re: lastUpdate.minutesAgo for security state - how?

Post by funnybu » Wednesday 10 October 2018 9:03

May be easiest way write user variable? (and read it from another scripts) I think it is possible do with dzvents and more universal. What do you think?

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

Re: lastUpdate.minutesAgo for security state - how?

Post by waaren » Wednesday 10 October 2018 12:35

funnybu wrote:
Wednesday 10 October 2018 9:03
May be easiest way write user variable? (and read it from another scripts) I think it is possible do with dzvents and more universal. What do you think?
Yes, good idea ! Write the uservariable with my script and read it with other (your) scripts. Let me know if you cannot get it working.
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
waaren
Posts: 1358
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: lastUpdate.minutesAgo for security state - how?  [Solved]

Post by waaren » Thursday 11 October 2018 7:48

funnybu wrote:
Wednesday 10 October 2018 9:03
May be easiest way write user variable? (and read it from another scripts) I think it is possible do with dzvents and more universal. What do you think?
Like this ?

Set

Code: Select all

--[[ setSecurityState.lua 
    Before first execution of this script, the vars "SecurityStateModifyTime" and "SecurityState" need to be created as type string.
    values can be left empty
]]--

return { 
        on =        {   security = { domoticz.SECURITY_ARMEDAWAY, 
                                     domoticz.SECURITY_ARMEDHOME,
                                     domoticz.SECURITY_DISARMED },
                    },
     
        logging =   {   level   =   domoticz.LOG_DEBUG,
                        marker  =   "setSecurityState" },
         
    execute = function(dz,item)
        local securityState     = dz.variables("SecurityState")
        local securityStateTime = dz.variables("SecurityStateModifyTime")
        
        local function setVars(str)
            if securityState.value ~= str then
                securityState.set(str)
                securityStateTime.set(dz.time.raw)
            end    
        end
        
        setVars(item.trigger)
                  
    end
}
Get

Code: Select all

--[[ getSecurityState.lua 
    this is a script to demonstrate how to get the states from the security panel and lastupdate time of this state.
    The update of the variables is done  by the setSecurityState script and that script must have run at least once before this one
]]--

return { 
        on =        {   devices = { "securityTrigger" }},  -- Just for test purposes. 
      
    execute = function(dz)
        local securityState     = dz.variables("SecurityState")
        local securityStateTime = dz.variables("SecurityStateModifyTime")
        
        -- Get securityState and time set
        local Time = require('Time')
        local t = Time(securityStateTime.value)
        dz.log("Security state set to: " .. securityState.value .. ", " .. 
                    t.secondsAgo .." seconds ago.")  -- retrieve state and set time secondsAgo                    
    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

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests