Lua Activate devices with pushbutton switches

From Domoticz
Jump to navigation Jump to search

Controlling one or more outputs with a pushbutton switch

The following LUA script for Domoticz can be taken as an example to manage one or more pushbutton switches to control one or more outputs.

Two kinds of management are explained in this script:

The first pushbutton toggles outdoor lights ON/OFF with short pulse, and switches lights OFF with long pulse, so with a long pulse it's possible assure that outdoor lights will be off.

The second pushbutton switch (placed in a bathroom, for example) toggles an electric heater ON/OFF with short pulse, activate the hot-water recirculating pump with 2-3s pulse, activate the controlled mechanical ventilation with a 5-6s pulse on pushbutton switch.

These are just example to show how to use the script in real cases... but you can use the pushbutton switches to control everything you need.

Please remember to copy the example below in a file in the directory DOMOTICZ_DIR/scripts/lua and named script_device_ANYNAMEYOUPREFER.lua because filename is important in domoticz and script filename must start with script_device_ and must end with .lua

-- LUA script for Domoticz, used to manage one or more pushbutton switches
-- Two kinds of management: 
-- The first pushbutton toggles outdoor lights ON/OFF with short pulse, and switches lights OFF with long pulse
-- The second pushbutton toggles an electric heater ON/OFF with short pulse, activate a second device with 2-3s pulse, activate a third device with a 5-6s pulse on pushbutton switch
--
-- Written by Creasol, https://www.creasol.it [email protected]
-- 
-- Domoticz passes information to scripts through a number of global tables
--
-- devicechanged contains state and svalues for the device that changed.
--   devicechanged['yourdevicename'] = state 
--   devicechanged['svalues'] = svalues string 
--
-- otherdevices, otherdevices_lastupdate and otherdevices_svalues are arrays for all devices: 
--   otherdevices['yourotherdevicename'] = "On"
--   otherdevices_lastupdate['yourotherdevicename'] = "2015-12-27 14:26:40"
--   otherdevices_svalues['yourotherthermometer'] = string of svalues
--
-- uservariables and uservariables_lastupdate are arrays for all user variables: 
--   uservariables['yourvariablename'] = 'Test Value'
--   uservariables_lastupdate['yourvariablename'] = '2015-12-27 11:19:22'
--
-- other useful details are contained in the timeofday table
--   timeofday['Nighttime'] = true or false
--   timeofday['SunriseInMinutes'] = number
--   timeofday['Daytime'] = true or false
--   timeofday['SunsetInMinutes'] = number
--   globalvariables['Security'] = 'Disarmed', 'Armed Home' or 'Armed Away'
--
-- To see examples of commands see: http://www.domoticz.com/wiki/LUA_commands#General
-- To get a list of available values see: http://www.domoticz.com/wiki/LUA_commands#Function_to_dump_all_variables_supplied_to_the_script
--
-- Based on your logic, fill the commandArray with device commands. Device name is case sensitive. 
--
debug=1			-- 0 => don't write debug information on log. 1 =>  write some information to the Domoticz log

commandArray = {}

timeNow=os.time()	-- current time in seconds

function timeElapsed(devName) 
	-- compute number of seconds since last update, for the specified variable name
	s=otherdevices_lastupdate[devName]
	year = string.sub(s, 1, 4)
	month = string.sub(s, 6, 7)
	day = string.sub(s, 9, 10)
	hour = string.sub(s, 12, 13)
	minutes = string.sub(s, 15, 16)
	seconds = string.sub(s, 18, 19)
 	return os.difftime(timeNow, os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds})
end

-- loop through all the changed devices
for devName,devValue in pairs(devicechanged) do
	if (debug > 0) then print('EVENT: devname="'..devName..'" and value='..devValue) end

	-- pushbutton that toggles lights ON/OFF when push quickly, and turn lights OFF when push for more than 2 seconds
	if (devName=='PULSANTE SUD luci esterne') then -- PULSANTE SUD luci estern = device name for outdoor lights pushbutton switch
		-- 1 short pulse => toggles lights ON/OFF
		-- 1 long pulse => lights OFF
		if (devValue=='Off') then
			-- pushbutton released
			-- compute pulse length
			pulseLen=timeElapsed(devName)
			if (debug>0) then print("EVENT: pushbutton released, pulseLen="..tostring(pulseLen).."s") end
			if (pulseLen<=1 and otherdevices['LightOut2']=='Off') then
				-- short pulse, and commanded device is OFF => ON
				commandArray['LightOut2']='On'
				commandArray['LightOut3']='On'
			else
				-- long pulse, or commanded device was ON
				commandArray['LightOut2']='Off'
				commandArray['LightOut3']='Off'
			end
		end
	end

	-- pushbutton in the bathroom: 
	-- short pulse => toggles ON/OFF the electric heater
	-- 2 seconds pulse => starts hot-water recirculating pump
	-- 5 seconds => starts controlled mechanical ventilation
	if (devName=='PULSANTE_Bagno') then	-- PULSANTE_Bagno = device name for pushbutton switch in the bathroom
		-- short pulse => toggles scaldasalviette ON/OFF
		-- 2s pulse => enable ricircolo acqua calda sanitaria
		-- 4s pulse => enable VMC
		if (devValue=='Off') then
			-- pushbutton released
			-- compute pulse length
			pulseLen=timeElapsed(devName)
			if (debug>0) then print("EVENT: pushbutton released, pulseLen="..tostring(pulseLen).."s") end
			if (pulseLen<=1) then
				-- short pulse, toggle heater ON/OFF
				if (otherdevices['Bagno_Scaldasalviette']~='Off') then -- Bagno_Scaldasalviette = device name for electric heater
					commandArray['Bagno_Scaldasalviette']='Off'
				else
					commandArray['Bagno_Scaldasalviette']='On'
				end
			elseif (pulseLen>=2 and pulseLen<=3) then
				commandArray['Ricircolo ACS']='On FOR 30 seconds'	-- Ricircolo ACS = device name for hot water recirculation pump
			elseif (pulseLen>=5 and pulseLen<=7) then
				if (otherdevices['VMC_Rinnovo']=='Off') then		-- VMC_Rinnovo = device name for controlled mechanical ventilation
					commandArray['VMC_Rinnovo']='On FOR 30 minutes'
				end
			end
		end
	end
end

return commandArray