Beginner: Check for current switch state

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
barbaar
Posts: 56
Joined: Wednesday 24 December 2014 16:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Beginner: Check for current switch state

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

Re: Beginner: Check for current switch state

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