Yet another AWAY script

Moderator: leecollings

Post Reply
Ries
Posts: 40
Joined: Tuesday 01 December 2015 8:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Netherlands
Contact:

Yet another AWAY script

Post 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
}
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: 40
Joined: Tuesday 01 December 2015 8:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Netherlands
Contact:

Re: Yet another AWAY script

Post 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)
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: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Yet another AWAY script

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Ries
Posts: 40
Joined: Tuesday 01 December 2015 8:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Netherlands
Contact:

Re: Yet another AWAY script

Post 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
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: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Yet another AWAY script

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest