AND function kills script

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

Moderator: leecollings

Post Reply
R0yk3
Posts: 37
Joined: Sunday 24 July 2016 21:51
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

AND function kills script

Post by R0yk3 »

Hello,
this script does not work like i want it to:

Code: Select all

return {
	on = {
		devices = {
			'Telefoon_Jeanine',
			'Telefoon_Roy',
			'ZonOnder'
		},
	},
	execute = function(domoticz, device)
        if ((device.name == 'Telefoon_Jeanine' and device.state == 'On') or
            (device.name == 'Telefoon_Roy' and device.state == 'On') and
            (device.name == 'ZonOnder' and device.state == 'On')) then
	    domoticz.devices('Tuin').switchOn()
        end
	end
}
What i want is:
The light 'tuin' is turned on when:
'Telefoon_Jeanine', is on
or
'Telefoon_Roy', is on
and
'ZonOnder' is on

Al three are switches. Without the ZonOnder it works.
I replaced ZondOnder also by

Code: Select all

timer = {
            'at nighttime''
        }
But at nighttime does not work, or while testing at daytime.

I'm Using the latest beta Version: 3.8807
Raspberry PI 3, raspbian, ZwaveMe, RFLink
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: AND function kills script

Post by waaren »

Maybe this can help you ?

Code: Select all

return {

active = true,
    
on = { devices = { ['Telefoon*'] = {'at nighttime'} 	}   },
	            
execute = function(domoticz,Telefoon)
	if Telefoon.state == 'On' then
		domoticz.devices('Tuin').switchOn()
        end
end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
R0yk3
Posts: 37
Joined: Sunday 24 July 2016 21:51
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: AND function kills script

Post by R0yk3 »

Thank You!! This works for turning the lights on.

But, i also want the lights to turn off if we leave the house.
Now i added this

Code: Select all

else 
	    domoticz.devices('Tuin').switchOff()
But now the light does turn off if only 1 phone leaves the house?
Raspberry PI 3, raspbian, ZwaveMe, RFLink
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: AND function kills script

Post by waaren »

How about ? [Edit]

Code: Select all

return {
	active = true,
    
    on = { 
		devices = { ['Telefoon*'] = {'at nighttime'}	,
                timer = { 'at sunrise', 'at sunset'}
		} 
    },
	
            
execute = function(domoticz,Telefoon,TriggerInfo)
    if TriggerInfo.trigger == "at sunrise" then
        domoticz.devices('Tuin').switchOff()
    elseif TriggerInfo.trigger == "at sunset" then
        if domoticz.devices("Telefoon_Jeanine").state == "On" or domoticz.devices("Telefoon_Roy").state == "On" then
            domoticz.devices('Tuin').switchOn()
        end    
    else        
        if Telefoon.state == 'On' then
            domoticz.devices('Tuin').switchOn()
        elseif domoticz.devices("Telefoon_Jeanine").state == "Off" and domoticz.devices("Telefoon_Roy").state == "Off" then
            domoticz.devices('Tuin').switchOff()
        end
    end
end
}
Last edited by waaren on Monday 15 January 2018 8:04, edited 4 times in total.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
R0yk3
Posts: 37
Joined: Sunday 24 July 2016 21:51
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: AND function kills script

Post by R0yk3 »

Thankyou, I wil try this tonight.
Just for me to learn as i think i understand the script.

{'at nighttime'} is a device????

and what is the difference between the red parts in the script:
I use spoiler so i can use colors
Spoiler: show
else
if Telefoon.state == 'On' then
domoticz.devices('Tuin').switchOn()
elseif domoticz.devices("Telefoon_Jeanine").state == "Off" and domoticz.devices("Telefoon_Roy").state == "Off" then
domoticz.devices('Tuin').switchOff()
Loads of other questions because just starting with the dzvents.. Until now just copy paste scripts, so i would like to know what i did wrong in my version of the script.
Raspberry PI 3, raspbian, ZwaveMe, RFLink
R0yk3
Posts: 37
Joined: Sunday 24 July 2016 21:51
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: AND function kills script

Post by R0yk3 »

It works perfect!!!! Thank you.
:D :D :D
Raspberry PI 3, raspbian, ZwaveMe, RFLink
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: AND function kills script

Post by waaren »

Maybe you found out already but to clarify:

devices = { ['Telefoon*'] = {'at nighttime'} -- Trigger script on any change to devices with a name starting with Telefoon but only at nighttime

Telefoon.state == 'On' then -- Check if state of (the single) device that triggered the script is 'On'

domoticz.devices("Telefoon_Jeanine").state == "Off" and domoticz.devices("Telefoon_Roy").state == "Off" then
-- Only true if both phone states are "Off"
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
R0yk3
Posts: 37
Joined: Sunday 24 July 2016 21:51
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: AND function kills script

Post by R0yk3 »

Waaren, Thank you!!
Raspberry PI 3, raspbian, ZwaveMe, RFLink
Luuc_a
Posts: 24
Joined: Tuesday 16 July 2013 10:12
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: AND function kills script

Post by Luuc_a »

I do some thing similar to your needs. I have edited the code I used to your needs

Code: Select all

return {
	on = {
		devices = {
			'Telefoon*'
		}
	},
	execute = function(domoticz, device)
		local home = false
		
		--Look in Domoticz for each device started with Telefoon. If a device has the status on, then set local var "Home" to True
		domoticz.devices().forEach(function(device)
            if string.find(device.name,'Telefoon') then
                if (device.state == 'On') then
                     home=true
                end
             end
        end )
        if home==false then
            domoticz.log('Nobody home' .. device.name, domoticz.LOG_INFO)
            domoticz.devices('Tuinverlichting').switchOff().checkfirst()
        else
            domoticz.log('Somebody home' .. device.name, domoticz.LOG_INFO)
            domoticz.devices('Tuinverlichting').switchOn().checkfirst()
        end
		domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
	end
}
Every device had the name "Telefoon....." and the script is triggerd by that device. In the for each loop you check if one of the devices with that name is "On". If there is another device is On the "Tuinverlichting" will stay on.

Maybe you can use this and edit to your needs.

Edit/
The only thing I don't have is the check for nighttime.
R0yk3
Posts: 37
Joined: Sunday 24 July 2016 21:51
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: the Netherlands
Contact:

Re: AND function kills script

Post by R0yk3 »

Thankyou. This helpes me learn the language.
Raspberry PI 3, raspbian, ZwaveMe, RFLink
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest