Page 1 of 1

Beginner: Check for current switch state

Posted: Sunday 19 August 2018 17:12
by barbaar
I want to create (another) presence detection script. All smartphones use geofencing to notify Domoticz if they ware at home using a virtual switch.

If one smartphone is at home, the virtual switch SomeoneHome needs to be turned on. But, if all Smartphones are away, the SomeoneHome switch needs to be turned off.

I have an extra virtual switch that can be manually set, for if we have a guest and none of the smartphones is at home (babysitter for example)

The first part works, the second not.

Code: Select all

return {
	on = {
		devices = {
			'Geofence A',
			'Geofence B',
			'Guest at Home'
		}
	},
	execute = function(domoticz, device)
	    if ((device.name == 'Geofence A' and device.state == 'On') or
	        (device.name == 'Geofence B' and device.state == 'On') or
	        (device.name == 'Guest at Home' and device.state == 'On')) then
	            domoticz.devices('SomeoneHome').switchOn()
		domoticz.log('Device ' .. device.name .. ' was turned On', domoticz.LOG_INFO)
	end
	
	 if ((device.name == 'Geofence A' and device.state == 'Off') and
	     (device.name == 'Geofence B' and device.state == 'Off') and
	     (device.name == 'Guest at Home' and device.state == 'Off')) then
	            domoticz.devices('SomeoneHome').switchOff()
		domoticz.log('Device ' .. device.name .. ' was turned Off', domoticz.LOG_INFO)
		end
	end
I guess the last part of the script is only triggered when the state of all devices change.. so how do I check for the current state of 3 devices of one of them is changed..

Re: Beginner: Check for current switch state

Posted: Sunday 19 August 2018 23:21
by waaren
barbaar wrote: Sunday 19 August 2018 17:12 I want to create (another) presence detection script. All smartphones use geofencing to notify Domoticz if they ware at home using a virtual switch.
If one smartphone is at home, the virtual switch SomeoneHome needs to be turned on. But, if all Smartphones are away, the SomeoneHome switch needs to be turned off.
I have an extra virtual switch that can be manually set, for if we have a guest and none of the smartphones is at home (babysitter for example)
What about this ?

Code: Select all

return 
{
    on = { devices = {  'Geofence A', 'Geofence B', 'Guest at Home' }},

    execute = function(dz, triggerObject)
        if dz.devices('Geofence A').state == 'On' or dz.devices('Geofence B').state == 'On' or dz.devices('Guest at Home').state == 'On' then
            dz.devices('SomeoneHome').switchOn().checkFirst()
        else 
            dz.devices('SomeoneHome').switchOff().checkFirst()
        end
        dz.log('Device ' .. triggerObject.name .. ' state: ' .. triggerObject.state, dz.LOG_INFO)
    end
}