User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

The following script, written in LUA programming language for Domoticz, was designed to manage 1 or more pushbutton switches in this way.

If the button is pressed for a short time, less than 1 second, toggle output ON/OFF

If the button is pressed for a longer time, switch output OFF

domoticz pushbutton switches with Creasol DomBus1

The script should be placed into the folder domoticz/scripts/lua ; filename is important, and must be script_device_DESCRIPTION.lua , for example script_device_pushbuttons.lua

Pushbutton devices must be On/Off or Pushbutton* type, or any other type that return status "On" and "Off" (e.g. type Contact returns value "Open" and "Close").

[code]-- LUA script for Domoticz, used to manage one or more pushbutton switches
-- Written by Creasol, https://creasol.it
--
-- 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 => do not 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 (devName=='PUSHBUTTON_light_external') then
-- Example: press and release pushbutton in less than 1 second => toggle lights ON/OFF
-- press and release pushbutton for more than 1 second => switch lights OFF
if (debug > 0) then print('EVENT: devname="'..devName..'" and value='..devValue) end
-- 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
end

return commandArray
[/code]

 

 

Our products for Domoticz: Creasol DomESP1 and DomBus1

 

Do you want more? Presentation of the highly reliable DomBus modules for home automation systems

DomBus modules are designed for high reliability and very low energy consumption, and can be used to create small to very complex home automation systems.

 

The following script is very similar to the previous one, but manages more pulse lengths:

in this example,

  if pushbutton pulse is 1s or less => turns light1 ON,

  if pushbutton pulse 2s => turns light2 ON,

  if pushbutton pulse 4s => turns light3 ON

Each output must be configured to be automatically disabled after a certain number of seconds, else the script have to be modified to also turn off the outputs.

[code]-- LUA script for Domoticz, used to manage one or more pushbutton switches
-- Written by Creasol, https://creasol.it
--
-- 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 => do not 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 (devName=='PUSHBUTTON_light_external') then
        -- Example: press and release pushbutton in less than 1 second => toggle lights ON/OFF
    --          press and release pushbutton for more than 1 second => switch lights OFF
        if (debug > 0) then print('EVENT: devname="'..devName..'" and value='..devValue) end
        -- 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) then
                -- short pulse
                commandArray['LightOut1']='On'
            elseif (pulseLen>=2 and pulseLen<=3) then
                -- medium pulse, 2s
                commandArray['LightOut2']='On'
            elseif (pulseLen>=4 and pulseLen<=6) then
                -- long pulse, 4s
                commandArray['LightOut3']='On'
            end
        end
    end
end

return commandArray[/code]

 Need help? join the DomBus channel on Telegram!

If you like this product, please share it on social networks. Thank you! Your satisfaction is our best advertisement.
Pin It

Comments powered by CComment

FaLang translation system by Faboba