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

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").

  1. -- LUA script for Domoticz, used to manage one or more pushbutton switches
  2. -- Written by Creasol, https://creasol.it
  3. --
  4. -- Domoticz passes information to scripts through a number of global tables
  5. --
  6. -- devicechanged contains state and svalues for the device that changed.
  7. -- devicechanged['yourdevicename'] = state
  8. -- devicechanged['svalues'] = svalues string
  9. --
  10. -- otherdevices, otherdevices_lastupdate and otherdevices_svalues are arrays for all devices:
  11. -- otherdevices['yourotherdevicename'] = "On"
  12. -- otherdevices_lastupdate['yourotherdevicename'] = "2015-12-27 14:26:40"
  13. -- otherdevices_svalues['yourotherthermometer'] = string of svalues
  14. --
  15. -- uservariables and uservariables_lastupdate are arrays for all user variables:
  16. -- uservariables['yourvariablename'] = 'Test Value'
  17. -- uservariables_lastupdate['yourvariablename'] = '2015-12-27 11:19:22'
  18. --
  19. -- other useful details are contained in the timeofday table
  20. -- timeofday['Nighttime'] = true or false
  21. -- timeofday['SunriseInMinutes'] = number
  22. -- timeofday['Daytime'] = true or false
  23. -- timeofday['SunsetInMinutes'] = number
  24. -- globalvariables['Security'] = 'Disarmed', 'Armed Home' or 'Armed Away'
  25. --
  26. -- To see examples of commands see: http://www.domoticz.com/wiki/LUA_commands#General
  27. -- To get a list of available values see: http://www.domoticz.com/wiki/LUA_commands#Function_to_dump_all_variables_supplied_to_the_script
  28. --
  29. -- Based on your logic, fill the commandArray with device commands. Device name is case sensitive.
  30. --
  31. debug=1 -- 0 => do not write debug information on log. 1 => write some information to the Domoticz log
  32.  
  33. commandArray = {}
  34.  
  35. timeNow=os.time() -- current time in seconds
  36.  
  37. function timeElapsed(devName)
  38. -- compute number of seconds since last update, for the specified variable name
  39. s=otherdevices_lastupdate[devName]
  40. year = string.sub(s, 1, 4)
  41. month = string.sub(s, 6, 7)
  42. day = string.sub(s, 9, 10)
  43. hour = string.sub(s, 12, 13)
  44. minutes = string.sub(s, 15, 16)
  45. seconds = string.sub(s, 18, 19)
  46. return os.difftime(timeNow, os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds})
  47. end
  48.  
  49. -- loop through all the changed devices
  50. for devName,devValue in pairs(devicechanged) do
  51. if (devName=='PUSHBUTTON_light_external') then
  52. -- Example: press and release pushbutton in less than 1 second => toggle lights ON/OFF
  53. -- press and release pushbutton for more than 1 second => switch lights OFF
  54. if (debug > 0) then print('EVENT: devname="'..devName..'" and value='..devValue) end
  55. -- 1 short pulse => toggles lights ON/OFF
  56. -- 1 long pulse => lights OFF
  57. if (devValue=='Off') then
  58. -- pushbutton released
  59. -- compute pulse length
  60. pulseLen=timeElapsed(devName)
  61. if (debug>0) then print("EVENT: pushbutton released, pulseLen="..tostring(pulseLen).."s") end
  62. if (pulseLen<=1 and otherdevices['LightOut2']=='Off') then
  63. -- short pulse, and commanded device is OFF => ON
  64. commandArray['LightOut2']='On'
  65. commandArray['LightOut3']='On'
  66. else
  67. -- long pulse, or commanded device was ON
  68. commandArray['LightOut2']='Off'
  69. commandArray['LightOut3']='Off'
  70. end
  71. end
  72. end
  73. end
  74.  
  75. return commandArray
  76.  

 

 

Our products for Domoticz: Creasol DomESP1 and DomBus1

 

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.

  1. -- LUA script for Domoticz, used to manage one or more pushbutton switches
  2. -- Written by Creasol, https://creasol.it
  3. --
  4. -- Domoticz passes information to scripts through a number of global tables
  5. --
  6. -- devicechanged contains state and svalues for the device that changed.
  7. --   devicechanged['yourdevicename'] = state
  8. --   devicechanged['svalues'] = svalues string
  9. --
  10. -- otherdevices, otherdevices_lastupdate and otherdevices_svalues are arrays for all devices:
  11. --   otherdevices['yourotherdevicename'] = "On"
  12. --   otherdevices_lastupdate['yourotherdevicename'] = "2015-12-27 14:26:40"
  13. --   otherdevices_svalues['yourotherthermometer'] = string of svalues
  14. --
  15. -- uservariables and uservariables_lastupdate are arrays for all user variables:
  16. --   uservariables['yourvariablename'] = 'Test Value'
  17. --   uservariables_lastupdate['yourvariablename'] = '2015-12-27 11:19:22'
  18. --
  19. -- other useful details are contained in the timeofday table
  20. --   timeofday['Nighttime'] = true or false
  21. --   timeofday['SunriseInMinutes'] = number
  22. --   timeofday['Daytime'] = true or false
  23. --   timeofday['SunsetInMinutes'] = number
  24. --   globalvariables['Security'] = 'Disarmed', 'Armed Home' or 'Armed Away'
  25. --
  26. -- To see examples of commands see: http://www.domoticz.com/wiki/LUA_commands#General
  27. -- To get a list of available values see: http://www.domoticz.com/wiki/LUA_commands#Function_to_dump_all_variables_supplied_to_the_script
  28. --
  29. -- Based on your logic, fill the commandArray with device commands. Device name is case sensitive.
  30. --
  31. debug=1            -- 0 => do not write debug information on log. 1 =>  write some information to the Domoticz log
  32.  
  33. commandArray = {}
  34.  
  35. timeNow=os.time()    -- current time in seconds
  36.  
  37. function timeElapsed(devName)
  38.     -- compute number of seconds since last update, for the specified variable name
  39.     s=otherdevices_lastupdate[devName]
  40.     year = string.sub(s, 1, 4)
  41.     month = string.sub(s, 6, 7)
  42.     day = string.sub(s, 9, 10)
  43.     hour = string.sub(s, 12, 13)
  44.     minutes = string.sub(s, 15, 16)
  45.     seconds = string.sub(s, 18, 19)
  46.      return os.difftime(timeNow, os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds})
  47. end
  48.  
  49. -- loop through all the changed devices
  50. for devName,devValue in pairs(devicechanged) do
  51.     if (devName=='PUSHBUTTON_light_external') then
  52.         -- Example: press and release pushbutton in less than 1 second => toggle lights ON/OFF
  53.     --          press and release pushbutton for more than 1 second => switch lights OFF
  54.         if (debug > 0) then print('EVENT: devname="'..devName..'" and value='..devValue) end
  55.         -- 1 short pulse => toggles lights ON/OFF
  56.         -- 1 long pulse => lights OFF
  57.         if (devValue=='Off') then
  58.             -- pushbutton released
  59.             -- compute pulse length
  60.             pulseLen=timeElapsed(devName)
  61.             if (debug>0) then print("EVENT: pushbutton released, pulseLen="..tostring(pulseLen).."s") end
  62.             if (pulseLen<=1) then
  63.                 -- short pulse
  64.                 commandArray['LightOut1']='On'
  65.             elseif (pulseLen>=2 and pulseLen<=3) then
  66.                 -- medium pulse, 2s
  67.                 commandArray['LightOut2']='On'
  68.             elseif (pulseLen>=4 and pulseLen<=6) then
  69.                 -- long pulse, 4s
  70.                 commandArray['LightOut3']='On'
  71.             end
  72.         end
  73.     end
  74. end
  75.  
  76. return commandArray

 

Pin It