Page 1 of 1

Need help to understand swtiching on device and timer

Posted: Sunday 12 November 2017 12:25
by Jumper3126
Hi,
Just started my first dzVents script. I have a small greenhouse which I would like to heat during the day and during the night, but at different temperatures. I can get the script below to work for a min and max temp over the whole day, but if I define 4 if conditions (min and max temp during the day, and min and max temp during the night), the script doesnt run.

Any pointers in the right direction would be great :)

Code: Select all

return {
	active = true, -- set to false to disable this script
	on = {
		devices = {
			'Erker kasje rechts', -- device name
		}
	},

	execute = function(domoticz, device)
	    local heater = domoticz.devices('Xiaomi Smart Plug (temp kasje)')
	    
		if  (timer == {'between 10:01 and 22:00'}) and
		    (device.name == 'Erker kasje rechts' and device.temperature <= 25) and
		    (heater.state == 'Off') then
		    heater.switchOn()
		    domoticz.log('dzVentz: Kasje dag heater on')
		end

		if (timer == {'between 10:01 and 22:00'}) and
		   (device.name == 'Erker kasje rechts' and device.temperature >= 28) and
		   (heater.state == 'On') then
		    heater.switchOff()
  		    domoticz.log('dzVentz: Kasje dag heater off')
		end
		
		if  (timer == {'between 22:01 and 10:00'}) and
		    (device.name == 'Erker kasje rechts' and device.temperature <= 18) and
		    (heater.state == 'Off') then
		    heater.switchOn()
  		    domoticz.log('dzVentz: Kasje nacht heater on')
		end

		if (timer == {'between 22:01 and 10:00'}) and
		   (device.name == 'Erker kasje rechts' and device.temperature >= 21) and
		   (heater.state == 'On') then
		    heater.switchOff()
  		    domoticz.log('dzVentz: Kasje nacht heater off')
		end
	end
}

Re: Need help to understand swtiching on device and timer

Posted: Sunday 12 November 2017 13:09
by mosjonathan
i think it needs to be like this:

Code: Select all

return {
	active = true, -- set to false to disable this script
	on = {
		devices = {
			'Erker kasje rechts', -- device name
		},
	},

	execute = function(domoticz, device)
	    local heater = domoticz.devices('Xiaomi Smart Plug (temp kasje)')
	    
		if  (timer == {'between 10:01 and 22:00'}) and
		    (device.name == 'Erker kasje rechts' and device.temperature <= 25) and
		    (heater.state == 'Off') then
		    heater.switchOn()
		    domoticz.log('dzVentz: Kasje dag heater on')

		elseif (timer == {'between 10:01 and 22:00'}) and
		   (device.name == 'Erker kasje rechts' and device.temperature >= 28) and
		   (heater.state == 'On') then
		    heater.switchOff()
  		    domoticz.log('dzVentz: Kasje dag heater off')
		
		elseif (timer == {'between 22:01 and 10:00'}) and
		    (device.name == 'Erker kasje rechts' and device.temperature <= 18) and
		    (heater.state == 'Off') then
		    heater.switchOn()
  		    domoticz.log('dzVentz: Kasje nacht heater on')

		elseif (timer == {'between 22:01 and 10:00'}) and
		   (device.name == 'Erker kasje rechts' and device.temperature >= 21) and
		   (heater.state == 'On') then
		    heater.switchOff()
  		    domoticz.log('dzVentz: Kasje nacht heater off')
		end
	end
}

Re: Need help to understand swtiching on device and timer

Posted: Sunday 12 November 2017 13:46
by Jumper3126
Thanks for your reply. The elseif is an improvement. It didnt solve the issue though.
I think it doesnt interpretate the timer condition as I imagine. Perhaps I am defining it not correctly. Or perhaps I should also include a timer function in the On section. This would seem logical to me though

Re: Need help to understand swtiching on device and timer

Posted: Sunday 12 November 2017 14:18
by mosjonathan
then i think you need to split into two scripts and set the timer to the top off the script.
so you get a day script and a night script.

and maybe someone else with more knowledge of dzvents can give you some pointers

Re: Need help to understand swtiching on device and timer

Posted: Sunday 12 November 2017 19:36
by dannybloe
Please check the documentation as it has examples using device events and timer events in one script.

Re: Need help to understand swtiching on device and timer

Posted: Sunday 12 November 2017 21:57
by Lokonli
Do you also want to switch on the heater? Because now it only switches off.

The part 'device.temperature =' in the two if-statements need to be 'device.temperature ==' (otherwise you try to assign the value)

Probably it's easier to start the script every minute, and then depending on the time and temperature decide what to do.


Verstuurd vanaf mijn SM-G800F met Tapatalk


Re: Need help to understand swtiching on device and timer

Posted: Sunday 12 November 2017 22:49
by Jumper3126
@mosjonathan. For now that would be the only solution I can think of as well. But it would be nicer if I can keep everything in one script.

It is clear to me that you can define a "act when it is between 12:00 and 15:00" in the ON section. But, is it also possible to define such a condition in a IF/THEN function underneath EXECUTE? Like it try to do. Perhaps this is not possible at all, or perhaps I am just using the wrong code. @dannybloe, I scanned the wiki but couldnt find an answer to this.

@Lokonli. not sure if I understand what you mean with the 2 if-statements. I'm not using a single =, but >= and <=.
Starting the script every minute doesnt help unfortunatly. I still can get passed :

Code: Select all

if  (timer == {'between 10:01 and 22:00'}) and

Re: Need help to understand swtiching on device and timer

Posted: Monday 13 November 2017 9:08
by BakSeeDaa
What is timer? A global variable?

Re: Need help to understand swtiching on device and timer

Posted: Monday 13 November 2017 10:50
by Freemann
Maybe this helps;
The script triggers on the device "IemandThuis" and based on the time in "domoticz.time.matchesRule('at XX:XX-XX:XX')" it does various things;

Code: Select all

return {
	on = {
		devices = {'IemandThuis'}
	},
	execute = function(domoticz, device)
		if(device.name=='IemandThuis')then
			if (domoticz.time.matchesRule('at 06:00-19:30') and domoticz.devices('IemandThuis').state == 'On') then
				domoticz.devices('Versterker').switchOn().checkFirst()
				
			elseif (domoticz.time.matchesRule('at 19:31-06:00') and domoticz.devices('IemandThuis').state == 'On') then
				domoticz.devices('Versterker').switchOn().checkFirst()
				
			elseif (domoticz.devices('IemandThuis').state == 'Off') then
				domoticz.devices('Versterker').switchOff().afterSec(8).checkFirst()
				
			end
		end
	end
}

Re: Need help to understand swtiching on device and timer

Posted: Wednesday 15 November 2017 12:50
by Jumper3126
@Freemann, that is what I was looking for. I got it to work the way I wanted.
Thanks!