Page 1 of 1

Timer function

Posted: Friday 19 March 2021 12:54
by jacobsentertainment
Hi all,

How can i use seconds in an script, I mean I have an switch that toggles an other switch for 3 seconds.
I'm totally new to this dzvents script so go easy please ;)

Re: Timer function

Posted: Friday 19 March 2021 14:00
by waaren
jacobsentertainment wrote: Friday 19 March 2021 12:54 How can i use seconds in an script, I mean I have an switch that toggles an other switch for 3 seconds.
something like

Code: Select all

return
{
    on =
    {
        devices =
        {
            'activatingSwitch', -- name of the switch that triggers the next one
        },
    },
    execute = function(dz)
        follower = dz.devices('name of follower switch')
        follower.switchOn().checkFirst()
        follower.switchOff().afterSec(3)
    end
}

Re: Timer function

Posted: Friday 19 March 2021 14:21
by jacobsentertainment
waaren wrote: Friday 19 March 2021 14:00 something like

Code: Select all

return
{
    on =
    {
        devices =
        {
            'activatingSwitch', -- name of the switch that triggers the next one
        },
    },
    execute = function(dz)
        follower = dz.devices('name of follower switch')
        follower.switchOn().checkFirst()
        follower.switchOff().afterSec(3)
    end
}
Look good, ill give it a try tonight. Thanks!

Checked and works just fine, slowly getting somewhere :lol:

Re: Timer function

Posted: Sunday 28 March 2021 17:51
by jacobsentertainment
Hi again,

I'm trying to expand this little script with an lux sensor, so the light will only be switched by the motion sensors when the amount of light outside is below a specific value.

What's going wrong here?

Code: Select all

return {
	on = {
		devices = {
			'PIR3 overloop', 'PIR1 gang',
		}
	},
	execute = function(domoticz, device)
	    
	    local Device1 = domoticz.devices(95)
    	    
    	    if (Device1 <= 180 ) then
	        follower = domoticz.devices(262)
            follower.switchOn().checkFirst()
            follower.switchOff().afterSec(185)
        end
	end
}
The log is returning; lua:11: attempt to compare table with number
I'm lost here.

Re: Timer function

Posted: Sunday 28 March 2021 19:58
by waaren
jacobsentertainment wrote: Sunday 28 March 2021 17:51 What's going wrong here?

The log is returning; lua:11: attempt to compare table with number
I'm lost here.
Your script tries to compare a device object (a table) with the value 180.

You probably want to compare the value of the lux attribute of that device with that number.

Please see the wiki for all available attributes in dzVents for all devicetypes.

The script could look like.

Code: Select all

return {
    on = {
        devices = {
            'PIR3 overloop', 'PIR1 gang',
        }
    },
    execute = function(domoticz, device)
        
        local lux = domoticz.devices(95).lux
        local follower = domoticz.devices(262)

        if lux <= 180 and follower.state ~= 'On' then
            follower.switchOn()
            follower.switchOff().afterSec(185)
        end
    end
}

Re: Timer function

Posted: Sunday 28 March 2021 21:58
by jacobsentertainment
Thaks you're a star.

So for my info, I was only calling the device here

Code: Select all

local Device1 = domoticz.devices(95)
Where I should call teh value of the device

Code: Select all

local Device1 = domoticz.devices(95).lux
I should do more reading in the wiki, but all this has to be done in the small moments I do have t home :D Time is precious..

Re: Timer function

Posted: Sunday 28 March 2021 22:16
by waaren
jacobsentertainment wrote: Sunday 28 March 2021 21:58 So for my info, I was only calling the device here

Code: Select all

local Device1 = domoticz.devices(95)
Where I should call the value of the device

Code: Select all

local Device1 = domoticz.devices(95).lux
Almost. A device object has many attributes. Some generic and some specific for the devicetype. You were looking for the value of the attribute lux

to list some of the available attributes for a type lux sensor:

Code: Select all

 hardwareName: Xiaomi
 name: WC Lux
 unit: 43
 lux: 3
 inActive: true
 idx: 2676
 deviceSubType: Lux
 nValue: 0
 icon: lux
 customImage: 0
 deviceId: 000002B
 active: false
 _adapters:
       1: Lux device adapter
 description:
 hardwareType: Xiaomi Gateway
 baseType: device
 protected: false
 hardwareId: 33
 lastUpdate: 2021-03-28 21:41:47
 timedOut: false
 batteryLevel: 81
 deviceType: Lux
 isDevice: true
 signalLevel: 12
 state: 3
 sValue: 3


Re: Timer function  [Solved]

Posted: Sunday 28 March 2021 22:21
by jacobsentertainment
Aha now I'm slowly getting it, Thanks!

Re: Timer function

Posted: Friday 07 May 2021 23:25
by jacobsentertainment
I tried to add an second device and so far the script ain't returning any errors but the second device isn't switching on.
What am I doing wrong here?

Code: Select all

return {

	active = true,
	
	on = {
		timer = {
			'every 1 minutes'
		}
	},

	execute = function(domoticz)
     
		local myDevice1 = domoticz.devices('Lux')
		local myDevice2 = domoticz.devices(219)
		local myDevice3 = domoticz.devices(350)
                local LUX = myDevice1.lux
        domoticz.log(myDevice1.name .. ' ' .. LUX, domoticz.LOG_INFO)
        domoticz.log(myDevice2.name .. ' ' .. myDevice2.state, domoticz.LOG_INFO)
        domoticz.log(myDevice3.name .. ' ' .. myDevice3.state, domoticz.LOG_INFO)
		if (LUX <= 15 ) then
			myDevice2.switchOn()
			domoticz.log('Light info, The light ' .. myDevice2.name .. ' will be switched on due to low LUX', domoticz.LOG_INFO)
		elseif (LUX <= 15 ) then
			myDevice3.switchOn()
			domoticz.log('Light info, The light ' .. myDevice3.name .. ' will be switched on due to low LUX', domoticz.LOG_INFO)
		else 
			myDevices.switchOff()
		end
	end
}

Re: Timer function

Posted: Saturday 08 May 2021 0:27
by waaren
jacobsentertainment wrote: Friday 07 May 2021 23:25 I tried to add an second device and so far the script ain't returning any errors but the second device isn't switching on.
What am I doing wrong here?
The second condition is equal to the first condition so if the first is true the elseif will not be evaluated. If the first condition is false the condition after the elseif also evaluates to false

try

Code: Select all

return {

	active = true,
	
	on = {
		timer = {
			'every 1 minutes'
		}
	},

	execute = function(domoticz)
     
		local myDevice1 = domoticz.devices('Lux')
		local myDevice2 = domoticz.devices(219)
		local myDevice3 = domoticz.devices(350)
		
        local LUX = myDevice1.lux
        domoticz.log(myDevice1.name .. ' ' .. LUX, domoticz.LOG_INFO)
        domoticz.log(myDevice2.name .. ' ' .. myDevice2.state, domoticz.LOG_INFO)
        domoticz.log(myDevice3.name .. ' ' .. myDevice3.state, domoticz.LOG_INFO)
		
		if (LUX <= 15 ) then
			myDevice2.switchOn()
			domoticz.log('Light info, The light ' .. myDevice2.name .. ' will be switched on due to low LUX', domoticz.LOG_INFO)
			myDevice3.switchOn()
			domoticz.log('Light info, The light ' .. myDevice3.name .. ' will be switched on due to low LUX', domoticz.LOG_INFO)
		else 
			myDevices.switchOff()
		end
	end
}

Re: Timer function

Posted: Saturday 08 May 2021 1:06
by jacobsentertainment
Thanks again! :P