Page 1 of 1

How to use a Trigger ID?

Posted: Monday 23 September 2019 13:38
by Marcjeok
Hi all,

I'm new to dzvents and scripting in general. In other words, I'm a real NOOB in scripting :oops:. But I like to learn to create scripts in dzvents, because is has some real potential ;) . I received some scripts from colleagues and try to recreate a script based on their examples. I'm trying to write a script to turn on the stairs lights when motion is detected and turn them off after a short period. I would like to use multiple triggers in the script and based on the trigger execute a command. In the example I've received, he uses the command 'if (trigger.id == IDSM) then' (this works on his Domoticz :? ). However, it doesn't work in my script. Can somebody help me with this?

The code I made so far (not finished yet)

Code: Select all

--[[
SCRIPT DESCRIPTION:
This script turns the stairs lights on or off when there is motion and it's dark.
The lights will be turned off automatically after a period off time.

DEVICES USED:
Idx 67 = Trap sensor - Burglar
Idx 75 = Trapverlichting - Switch
Idx 22 = Daglicht
]]--

--SET USER INPUT VARIABLES:
IDSM = 67
IDSS = 75
IDDL = 22
SECSTOFF = 60   --turn off the stairs lights switch after xx seconds.

return {
	active = true,
	on = {
		devices = {
		    IDSM,
		    IDSS,
		},
	},
	execute = function(domoticz, triggeredItem, info)

        --SET VARIABLES:
        local STAIRSMOTION = domoticz.devices(IDSM)
        local STAIRSSWITCH = domoticz.devices(IDSS)
        local DAYLIGHTSWITCH = domoticz.devices(IDDL)

        -- EXECUTE SCRIPT
        if (trigger.id == IDSM) then
            domoticz.log('Stairs: There is motion in the dark, let me turn on the lights for you')
            STAIRSSWITCH.switchOn()
        end
	end
}
My log is showing the following:
2019-09-23 13:22:52.995 Status: dzVents: Info: Handling events for: "Trap sensor - Burglar", value: "On"
2019-09-23 13:22:52.995 Status: dzVents: Info: ------ Start internal script: Lights stairs: Device: "Trap sensor - Burglar (zwave)", Index: 67
2019-09-23 13:22:52.998 Status: dzVents: Info: ------ Finished Lights stairs

I'm running the latest version of domoticz:
Version: 4.11333
Build Hash: b2a5d9097
Compile Date: 2019-09-20 16:31:36
dzVents Version: 2.4.29
Python Version: 3.4.2 (default, Aug 18 2019, 00:35:45) [GCC 4.9.2]

Thanks in advance!

Re: How to use a Trigger ID?

Posted: Monday 23 September 2019 23:23
by waaren
Marcjeok wrote: Monday 23 September 2019 13:38 However, it doesn't work in my script. Can somebody help me with this?

Code: Select all

	execute = function(domoticz, triggeredItem, info)
        if (trigger.id == IDSM) then
You named the 2nd parm in the execute function triggeredItem but try to refer to it by using trigger.
You should change it to

Code: Select all

 
	execute = function(domoticz, trigger, info)
        if (trigger.id == IDSM) then
or

Code: Select all

 
	execute = function(domoticz, triggeredItem, info)
        if (triggeredItem.id == IDSM) then

Re: How to use a Trigger ID?  [Solved]

Posted: Tuesday 24 September 2019 18:28
by Marcjeok
So simple, so effective.
Many thanks for your help and explanation.

Re: How to use a Trigger ID?

Posted: Monday 30 September 2019 8:09
by Marcjeok
For me, it always helps to see scripts that works fine. So just for sharing, hereby the script I made to control the lights on my stairs.

Code: Select all

--[[
--[[
SCRIPT DESCRIPTION:
This script turns the stairs lights on or off when there is motion and it's dark (sunset).
The lights will be turned off automatically after a period off time.

DEVICES USED:
Idx 67 = Stairs Motion sensor (Philio Technology Corp PST02-B PIR/Motion 3 in 1 Sensor+)
Idx 75 = Stairs Switch (FIBARO System FGWPE/F Wall Plug Gen5+)
]]--

--SET USER INPUT VARIABLES:
local IDSM = 67
local IDSS = 75
local SECSTOFF = 60   --turn off the stairs lights switch after xx seconds.

return {
	active = true,
	on = {
		devices = {
		    IDSM,
		    IDSS,
		},
	},
	execute = function(domoticz, triggeredItem, info)

        --SET VARIABLES:
        local STAIRSMOTION = domoticz.devices(IDSM)
        local STAIRSSWITCH = domoticz.devices(IDSS)

        -- EXECUTE SCRIPT
        if (triggeredItem.id == IDSM) and (domoticz.time.isNightTime) and (STAIRSSWITCH.state == "Off") then
            domoticz.log('Stairs: There is motion in the dark, let me turn on the lights for you')
            STAIRSSWITCH.switchOn()
        elseif (triggeredItem.id == IDSM) and (domoticz.time.isNightTime) and (STAIRSSWITCH.state == "On") then    
            domoticz.log('Stairs: Reset timer, turn off the lights in '..SECSTOFF..' seconds')
            STAIRSSWITCH.cancelQueuedCommands()
            STAIRSSWITCH.switchOff().afterSec(SECSTOFF)
        elseif (triggeredItem.id == IDSS) and (STAIRSSWITCH.state == "On") then
            domoticz.log('Stairs: Turn off the lights in '..SECSTOFF..' seconds')
            STAIRSSWITCH.switchOff().afterSec(SECSTOFF)
        end
	end
}

07-10-2019: updated script