towel rail if statement

Easy to use, 100% Lua-based event scripting framework.
Post Reply
markjgabb
Posts: 128
Joined: Wednesday 25 January 2017 0:00
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: Australia
Contact:

towel rail if statement

Post by markjgabb » Sunday 06 August 2017 4:06

hi guys

im currently trying to write a dzvents to control my towel rail

the scenario is
i have a wemo switch in the bathroom on the powerpoint for the towel rail
if its switched on manually at the switch (which is the most likely)
it doesnt update in domoticz
so i would like to set a switch that checks every 10 minutes using the phyton command wemo status (ouixmux)
which returns something like ('Switch:', 'Wemo', '\t', 0) off
or ('Switch:', 'Wemo', '\t', 1) on

this is where ive gotten so far...im stuck on the script return part....and the end in the middle.... line 27

Code: Select all

-- Check the wiki at
-- http://www.domoticz.com/wiki/%27dzVents%27:_next_generation_LUA_scripting
return {

	-- 'active' controls if this entire script is considered or not
	active = false, -- set to false to disable this script

	-- trigger
	-- can be a combination:
	on = {
		timer = {
			-- timer triggers.. if one matches with the current time then the script is executed
			'every 10 minutes'
		}
	},
	data = {
	    towelrailontime = {domoticz.device('towel rail').lastUpdate.raw}
	    wemo_state = {initial=off}
	    }

	-- actual event code
	-- in case of a timer event or security event, device == nil
	execute = function(domoticz, device)
		-- check for if switch is manually turned On
		--need somthing to run script and report if switch is on or off and report back to wemo_state
		if  domoticz.data.wemo_state == 'off'then 
		    goto end 
	elseif domoticz.data.wemo_state == 'on' then
		--turn on towel rail without triggering on script
		domoticz.device('towel rail').switchOn(true)
	
		--actual off switch
		if (domoticz.device('towel rail').state == 'On') and domoticz.time.raw - domoticz.data.towelrailontime > 45 then
			domoticz.device('towel rail').switchOff()
		
		end
	end
}
V 3.8153 RPI 3
RFlink 334 mhz
mysensors
broadlink
wemo

dannybloe
Posts: 1461
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: towel rail if statement

Post by dannybloe » Sunday 06 August 2017 9:13

Ok, there are a couple of problems with this script.
First of all, it isn't syntactically correct Lua as it is now. Apart from the logic of your script. The data section is missing comma's and the off needs quotes otherwise it is a variable instead of the string 'off'. Also, you cannot initialize a variable using the domoticz.object.
Also, the data section needs to be terminated with a comma as well, separating data from execute.
Then some other remarks.
You cannot use the silent switching just yet as it is not available in 2.2.0. Until then you can use

Code: Select all

domoticz.device('towel rail').update(0, 'Off')
Also, you cannot compare domoticz.time.raw and do calculations with it as it is a string representation of the time.
And finally, I'm not sure what you try to do with your script as the two data variables (towelrailontime and wemo_state) never seem to get a value in your script. The data section is only for initializing and declaring the variables. In your execute function you manipulate them.
Oh, and your question: you can end your function by using the 'return' statement. But in your script you don't need it if you make your if-statement logic different (just create an if-statement that does something when you need to, then you don't need an else part and no return statements are needed).

So, before you go and change the logic of your script, this version is more what you intended (syntactically):

Code: Select all

return {
	active = false, -- set to false to disable this script
	on = {
		timer = {
			-- timer triggers.. if one matches with the current time then the script is executed
			'every 10 minutes'
		}
	},
	data = {
		towelrailontime = { initial = nil }, -- you cannot use domoticz object here
		wemo_state = { initial = 'off' }, -- off must be a string otherwise it's a variable called off
	},

	execute = function(domoticz, device)
		-- check for if switch is manually turned On
		--need somthing to run script and report if switch is on or off and report back to wemo_state
	
		if domoticz.data.wemo_state == 'off' then
			return
		elseif domoticz.data.wemo_state == 'on' then
			--turn on towel rail without triggering on script
			--domoticz.device('towel rail'). switchOn(true) -- true doesn't work just yet in 2.2.0!!
			domoticz.device('towel rail').update(0, 'Off') -- switch off without triggering scripts
			
			--actual off switch
			if (domoticz.device('towel rail').state == 'On' and domoticz.time.raw - domoticz. data. towelrailontime > 45) then
				domoticz.device('towel rail').switchOff()
			end
		end
	end
}
That said, there is a better way to compare timestamps with dzVents by using a data variable with history=true:

Code: Select all

	data = { 
		onTime = {history = true, maxItems = 1 } 
	}
Then every time you give it a value:

Code: Select all

domoticz.data.onTime.set('On')
It will automatically time-stamp the value. So at a later stage, when you want to see when the switch was switched on you can check when exactly that was:

Code: Select all

local lastValue = domoticz.data.onTime.get()
if (lastValue.value == 'On' or lastValue.time.secondsAgo() > 45) then --just an example!
	domoticz.device('towel rail').switchOff()
end
But again, think about the rest of your logic.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Post Reply

Who is online

Users browsing this forum: OlivierFR and 4 guests