Page 1 of 1

Help needed with garden irrigation program selector

Posted: Wednesday 22 May 2019 21:06
by snellejellep
Hi, i need some help creating a dzvents stript for a selector switch to be used as some kind of irrigation program selector.
this is what i created so far but i can not figure out how to execute functions with a certain delay. i hope someone can help.

Code: Select all

return {
	on = {
		devices = {
			'Sproeier programma'
		}
	},
	execute = function(dz, device)
		local selector          = dz.devices("Sproeier programma")
		local zone1             = dz.devices("Irrigatie zone 1")            
		local zone2             = dz.devices("Irrigatie zone 2") 
		local zone3             = dz.devices("Irrigatie zone 3") 
		local zone4             = dz.devices("Irrigatie zone 4") 
		local zone5             = dz.devices("Irrigatie zone 5") 
		local zone6             = dz.devices("Irrigatie zone 6") 
		local zone7             = dz.devices("Irrigatie zone 7") 
		
		if selector.state == "Alles" then
		    -- first turn on zone 4 and 1 min later zone 5, then after an hour turn off zone 4 and 5 and turn on first zone 1, then a minute later zone 2,3 and 7
		elseif selector.state == "Voortuin" then
		    -- turn on zone 4 and 1 minute later zone 5, then after an hour turn off zone 4 and zone5
		elseif selector.state == "Achtertuin" then
		    -- turn on zone 1, and 1 minute later zone 2, 3 and 7 and turn them off after an hour
		elseif selector.state == "Waterstopcontact" then
		    -- turn on zone 6 and turn it off after an hour
		elseif selector.state == "Off" then
		    -- force everything to turn off
		    zone1.switchOff()
		    zone2.switchOff()
		    zone3.switchOff()
		    zone4.switchOff()
		    zone5.switchOff()
		    zone6.switchOff()
		    zone7.switchOff()
		end
	end
}

Re: Help needed with garden irrigation program selector  [Solved]

Posted: Wednesday 22 May 2019 22:18
by waaren
snellejellep wrote: Wednesday 22 May 2019 21:06 Hi, i need some help creating a dzvents stript for a selector switch to be used as some kind of irrigation program selector.
Could be something like this. (some assumptions, about resetting states when new selector level is chosen, made)

Code: Select all

return {
	on = {
		devices = {
			'Sproeier programma'
		}
	},

	   logging =	{   
						level   =   domoticz.LOG_ERROR,
						-- level   =   domoticz.LOG_DEBUG,
						marker  =   "Sproeier" 
					},	
					
	execute = function(dz, device)

		local selector = dz.devices("Sproeier programma")

		local zone1 = dz.devices("Irrigatie zone 1")
		local zone2 = dz.devices("Irrigatie zone 2")
		local zone3 = dz.devices("Irrigatie zone 3")
		local zone4 = dz.devices("Irrigatie zone 4")
		local zone5 = dz.devices("Irrigatie zone 5")
		local zone6 = dz.devices("Irrigatie zone 6")
		local zone7 = dz.devices("Irrigatie zone 7")

		local zones = { zone1, zone2, zone3, zone4, zone5, zone6, zone7 }

		dz.log(selector.name .. ' switched to ' .. selector.state, dz.LOG_DEBUG)

		for _, zone in ipairs(zones) do
			zone.cancelQueuedCommands() -- remove all 'old' scheduled starts / stops
			zone.switchOff().checkFirst()
		end
		
		if selector.state == "Alles" then
			-- first turn on zone 4 and 1 Min later zone 5, then after an hour turn off zone 4 and 5 and turn on first zone 1, then a Minute later zone 2,3 and 7
			zone4.switchOn().forMin(60)
			zone5.switchOn().afterMin(1).forMin(60)
			zone1.switchOn().afterMin(61).forMin(60)
			zone2.switchOn().afterMin(62).forMin(60)
			zone3.switchOn().afterMin(62).forMin(60)
			zone7.switchOn().afterMin(62).forMin(60)
			
		elseif selector.state == "Voortuin" then
			-- turn on zone 4 and 1 Minute later zone 5, then after an hour turn off zone 4 and zone5
			zone4.switchOn().forMin(60)
			zone5.switchOn().afterMin(1).forMin(60)

		elseif selector.state == "Achtertuin" then
			-- turn on zone 1, and 1 Minute later zone 2, 3 and 7 and turn them off after an hour
			zone1.switchOn().forMin(60)
			zone2.switchOn().afterMin(1).forMin(60)
			zone3.switchOn().afterMin(1).forMin(60)
			zone7.switchOn().afterMin(1).forMin(60)

		elseif selector.state == "Waterstopcontact" then
			-- turn on zone 6 and turn it off after an hour
			zone6.switchOn().forMin(60)
		
		elseif selector.state == "Off" then
			-- force everything to turn off
			-- Already done
		end
	end
}

Re: Help needed with garden irrigation program selector

Posted: Thursday 23 May 2019 16:52
by snellejellep
waaren wrote: Wednesday 22 May 2019 22:18
snellejellep wrote: Wednesday 22 May 2019 21:06 Hi, i need some help creating a dzvents stript for a selector switch to be used as some kind of irrigation program selector.
Could be something like this. (some assumptions, about resetting states when new selector level is chosen, made)

Code: Select all

return {
	on = {
		devices = {
			'Sproeier programma'
		}
	},

	   logging =	{   
						level   =   domoticz.LOG_ERROR,
						-- level   =   domoticz.LOG_DEBUG,
						marker  =   "Sproeier" 
					},	
					
	execute = function(dz, device)

		local selector = dz.devices("Sproeier programma")

		local zone1 = dz.devices("Irrigatie zone 1")
		local zone2 = dz.devices("Irrigatie zone 2")
		local zone3 = dz.devices("Irrigatie zone 3")
		local zone4 = dz.devices("Irrigatie zone 4")
		local zone5 = dz.devices("Irrigatie zone 5")
		local zone6 = dz.devices("Irrigatie zone 6")
		local zone7 = dz.devices("Irrigatie zone 7")

		local zones = { zone1, zone2, zone3, zone4, zone5, zone6, zone7 }

		dz.log(selector.name .. ' switched to ' .. selector.state, dz.LOG_DEBUG)

		for _, zone in ipairs(zones) do
			zone.cancelQueuedCommands() -- remove all 'old' scheduled starts / stops
			zone.switchOff().checkFirst()
		end
		
		if selector.state == "Alles" then
			-- first turn on zone 4 and 1 Min later zone 5, then after an hour turn off zone 4 and 5 and turn on first zone 1, then a Minute later zone 2,3 and 7
			zone4.switchOn().forMin(60)
			zone5.switchOn().afterMin(1).forMin(60)
			zone1.switchOn().afterMin(61).forMin(60)
			zone2.switchOn().afterMin(62).forMin(60)
			zone3.switchOn().afterMin(62).forMin(60)
			zone7.switchOn().afterMin(62).forMin(60)
			
		elseif selector.state == "Voortuin" then
			-- turn on zone 4 and 1 Minute later zone 5, then after an hour turn off zone 4 and zone5
			zone4.switchOn().forMin(60)
			zone5.switchOn().afterMin(1).forMin(60)

		elseif selector.state == "Achtertuin" then
			-- turn on zone 1, and 1 Minute later zone 2, 3 and 7 and turn them off after an hour
			zone1.switchOn().forMin(60)
			zone2.switchOn().afterMin(1).forMin(60)
			zone3.switchOn().afterMin(1).forMin(60)
			zone7.switchOn().afterMin(1).forMin(60)

		elseif selector.state == "Waterstopcontact" then
			-- turn on zone 6 and turn it off after an hour
			zone6.switchOn().forMin(60)
		
		elseif selector.state == "Off" then
			-- force everything to turn off
			-- Already done
		end
	end
}
thank you! it works great!