Timer function  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Timer function

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

Re: Timer function

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Timer function

Post 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:
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Timer function

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

Re: Timer function

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Timer function

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

Re: Timer function

Post 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

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Timer function  [Solved]

Post by jacobsentertainment »

Aha now I'm slowly getting it, Thanks!
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Timer function

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

Re: Timer function

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
jacobsentertainment
Posts: 223
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: Timer function

Post by jacobsentertainment »

Thanks again! :P
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest