Yet another AWAY script

Post Reply
Ries
Posts: 33
Joined: Tuesday 01 December 2015 9:47
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Netherlands
Contact:

Yet another AWAY script

Post by Ries » Thursday 14 June 2018 8:53

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
}
Version 3.9639
Platform: Jessie lite
Hardware: Raspberry pi 3B; RFXCOM; Weather Underground; Dummy; OpenZwave USB; Fritzbox Callmonitor via LAN interface; System Alive Checker (Ping); Philips Hue Bridge; Atag One Thermostat

Ries
Posts: 33
Joined: Tuesday 01 December 2015 9:47
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Netherlands
Contact:

Re: Yet another AWAY script

Post by Ries » 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)
Version 3.9639
Platform: Jessie lite
Hardware: Raspberry pi 3B; RFXCOM; Weather Underground; Dummy; OpenZwave USB; Fritzbox Callmonitor via LAN interface; System Alive Checker (Ping); Philips Hue Bridge; Atag One Thermostat

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

Re: Yet another AWAY script

Post by waaren » Thursday 14 June 2018 9:43

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
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

Ries
Posts: 33
Joined: Tuesday 01 December 2015 9:47
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Netherlands
Contact:

Re: Yet another AWAY script

Post by Ries » Thursday 14 June 2018 19:16

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
Version 3.9639
Platform: Jessie lite
Hardware: Raspberry pi 3B; RFXCOM; Weather Underground; Dummy; OpenZwave USB; Fritzbox Callmonitor via LAN interface; System Alive Checker (Ping); Philips Hue Bridge; Atag One Thermostat

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

Re: Yet another AWAY script

Post by waaren » Thursday 14 June 2018 20:45

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
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: No registered users and 0 guests