Page 1 of 1

Yet another AWAY script

Posted: Thursday 14 June 2018 8:53
by Ries
Sharing my away script.
the status 'Away' is determined when all detection devices in a group aren't active for the last 20 minutes.

Code: Select all

return {
	on = {
		timer = {
			'every 15 minutes'
		}
	},
	execute = function(domoticz)
	    minInactive = 20 -- grace period in minutes
		if  domoticz.devices('Status').state == 'Off' then -- selector switch : 10 = away;  20 = asleep; Off = people are present,
		    local away = true
		    domoticz.groups('Afwezig').devices().forEach(function(device) -- all relevant devices are in the Group 'Afwezig', about 14 devices, being PIRs, window/door sensor and pinged devices like television and computers
                if device.lastUpdate.minutesAgo < minInactive or device.active then -- if any device is active or was active in the grace period then people are present
		            away = false
                end
            end)
	        if away then 
	            domoticz.devices('Status').switchSelector(10) -- set Status to away; This triggers another script
            end
	    end
	end
}

Re: Yet another AWAY script

Posted: Thursday 14 June 2018 9:10
by Ries
havent tested it yet, but setting the away boolean can be done a little more efficient:

Code: Select all

local away = domoticz.groups('Afwezig').devices().reduce(
	function (acc, device) 
		if device.lastUpdate.minutesAgo < minInactive or device.active then acc =  false end
		return acc
	end, true)

Re: Yet another AWAY script

Posted: Thursday 14 June 2018 9:43
by waaren
Ries wrote: Thursday 14 June 2018 9:10 havent tested it yet, but setting the away boolean can be done a little more efficient:

Code: Select all

local away = domoticz.groups('Afwezig').devices().reduce(
	function (acc, device) 
		if device.lastUpdate.minutesAgo < minInactive or device.active then acc =  false end
		return acc
	end, true)
@Ries, very nice idea to use the group approach for this. I had something similar but used the comment attribute to identify devices to take into account.

Code: Select all

domoticz.devices().forEach(function(device)
                if device.description and string.find(device.description,"ACTIVITY") then

but your method is much more elegant.

[EDIT] tested with return false and works as expected (skipping further checks)

You could return from the foreach loop once you hit the first device active by adding "return false" after the line "away = false" in your first script. Skipping further checks

Re: Yet another AWAY script

Posted: Thursday 14 June 2018 19:16
by Ries
Thank you waaren,
That would make the foreach more efficient than the reduce approach.
Unless the reduce approach can be interrupted too somehow.

The group approach is indeed more efficient: less devices to loop through and easier to maintain.
Are you one of the developers? I love dzvents

Re: Yet another AWAY script

Posted: Thursday 14 June 2018 20:45
by waaren
Ries wrote: Thursday 14 June 2018 19:16 Are you one of the developers? I love dzvents
@Ries, no I am not one of the developers. Just someone who, like you, see the ease of use and elegance of dzVents in combination with domoticz. I learned a bit of Lua when I started with domoticz but since I discovered the ease of use of dzVents I am kind of hooked to it.
Still learning every day :D