Page 1 of 1

simple dzVents script

Posted: Friday 28 July 2017 11:19
by dk78
I'm new with dzVents and I'm trying to create my first simple script with it. I think it's far more powerful then the basic lua. But i'm having some issues with some triggers and need some help.

I think it's a very simple script, but I'm not sure that I use the correct timer triggers. I want to turn on my hallway light when the front door is open. But only during nighttime. When the door is closed, the light must go off after 30 seconds. First I had this:

Code: Select all

return {
	active = true,
	on = {
		devices = {
			'front door'
		},
		timer = {
			'at nighttime'
		},
	},

	execute = function(domoticz, door)
		local light = domoticz.devices('Hallway')
		
		if (door.state == 'Open') then
			light.switchOn()
		else
			light.switchOff().afterSec(30)
		end

	end
}
But then the light will stay on during nighttime if i'm correct. Because of the timer in the on section.

So now I thought of this script, but I'm not sure how to use the boolean 'domoticz.time.atNightTime'. So should this do the trick?

Code: Select all

return {
	active = true,
	on = {
		devices = {
			'front door'
		},
	},

	execute = function(domoticz, door)

		local light = domoticz.devices('Hallway')
		
		if (domoticz.time.isNightTime and door.state == 'Open') then
			light.switchOn()
		else
			light.switchOff().afterSec(30)
		end

	end
}
I tried to found some examples, but I haven't found any.

I'm not sure if I'm doing this correct. Should I let the door trigger this script or the timer?

Re: simple dzVents script

Posted: Friday 28 July 2017 11:47
by dannybloe
Ok, your first attempt wasn't correct indeed. What you did was creating two independent triggers: a device trigger and a timer trigger. They have no connection with each other. If the door is opened, the script is triggered but also every minute at nighttime.

So clearly that is not what you wanted.

Your seconds attempt creates only one trigger and causes your script to be executed if the door is opened or when it is closed (change of state). I think you should change it a bit though:

Code: Select all

execute = function(domoticz, door)
	if (not domoticz.time.isNightTime) then return 
	
	local light = domoticz.devices('Hallway')
      
	if (door.state == 'Open') then
		light.switchOn()
	else
		light.switchOff().afterSec(30)
	end
end
But, in your case it can even be simpler by setting a time rule on the device trigger:

Code: Select all

return {
	active = true,
	on = {
		devices = { 
			['front door'] = { 'at nighttime' }
		}
	},
	execute = function(domoticz, door)
		local light = domoticz.devices('Hallway')
      
		if (door.state == 'Open') then
			light.switchOn()
		else
			light.switchOff().afterSec(30)
		end
	end
}

Re: simple dzVents script

Posted: Friday 28 July 2017 12:07
by dk78
Thanks! Your last script is the script what I thought to do with my first.

I'm going to rewrite my scripts to dzVents. See many powerfull things without any dummy switches that I use at the moment.

Re: simple dzVents script

Posted: Friday 28 July 2017 12:08
by dannybloe
Good luck!

Re: simple dzVents script

Posted: Friday 28 July 2017 14:40
by dk78
How does it work if you want to execute the script with 2 device triggers? I thought I copy the syntax, but this won't work:

Code: Select all

   on = {
      devices = {
         ['Status'] = { 'at nighttime' }
         ['Luxsensor'] = { 'at nighttime' }
      }
   },
Tried also to put both devices between the [] but that isn't the solution either.

Re: simple dzVents script

Posted: Friday 28 July 2017 14:42
by dannybloe
there is a comma missing between the two devices items.

Code: Select all

   on = {
      devices = {
         ['Status'] = { 'at nighttime' }, -- comma
         ['Luxsensor'] = { 'at nighttime' }
      }
   },

Re: simple dzVents script

Posted: Friday 28 July 2017 15:01
by dk78
Ah, that makes sense. I was looking at {} and [] ' ' but missed the comma. Thanks again!