Page 1 of 1

Split lua script up in two and call the new one from the first one?

Posted: Tuesday 03 May 2016 22:11
by SoerenBM
Hi everyone,

I have been piecing a script together by using examples and reading from this forum. I have replaced the hardcoded parts with variables and was thinking that I could split the script up - one main script and one device specific so I can call the main script from all my device scripts using the device specific variables in the main script.

This is my first time writing scripts so please forgive my basic questions. I have googled and searched this forum but not able to figure out how.

How would I go about doing this?

Thanks.

My current scripts looks like this:

Code: Select all

-- script_device_switch_singlepulse.lua
-- Script needs a Domoticz uservariable called 'Domoticz_Devicename' e.g. 'Garage | Loftlampe' of type 'String'
-- See http://domoticz.com/forum/viewtopic.php?f=23&t=7512 for more information

commandArray = {}

openHAB_REST_URL = uservariables['openHAB_REST_URL']
openHAB_Devicename = 'Light_Garage_Loftlampe'
Domoticz_Devicename = 'Garage | Loftlampe'

TimeBetweenPresses = 2 --time that needs to be between presses (in seconds), before os.execute line is ran again

function datetimedifferencenow(s)
   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)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end

if (devicechanged[''..Domoticz_Devicename..''] == 'On') then
   if (datetimedifferencenow(uservariables_lastupdate[''..Domoticz_Devicename..'']) > TimeBetweenPresses ) then
      print('<b style="color:Blue">'..Domoticz_Devicename..', previous time was <b style="color:Green">MORE</b> than '..TimeBetweenPresses..' sec ago, switching ON</b>')
      os.execute('curl --header "Content-Type: text/plain" --request POST --data "ON" '..openHAB_REST_URL..''..openHAB_Devicename..' ')
      commandArray['Variable:'..Domoticz_Devicename..'']=tostring(os.time())
   else
      print('<b style="color:Blue">'..Domoticz_Devicename..', previous time was <b style="color:Red">LESS</b> than '..TimeBetweenPresses..' sec ago, ignore press of button</b>')
   end

elseif (devicechanged[''..Domoticz_Devicename..''] == 'Off') then
   if (datetimedifferencenow(uservariables_lastupdate[''..Domoticz_Devicename..'']) > TimeBetweenPresses ) then
      print('<b style="color:Blue">'..Domoticz_Devicename..', previous time was <b style="color:Green">MORE</b> than '..TimeBetweenPresses..' sec ago, switching OFF</b>')
      os.execute('curl --header "Content-Type: text/plain" --request POST --data "OFF" '..openHAB_REST_URL..''..openHAB_Devicename..' ')
      commandArray['Variable:'..Domoticz_Devicename..'']=tostring(os.time())
   else
      print('<b style="color:Blue">'..Domoticz_Devicename..', previous time was <b style="color:Red">LESS</b> than '..TimeBetweenPresses..' sec ago, ignore press of button</b>')
    end
end



I was thinking to split it op like this
Device specific part:

Code: Select all

commandArray = {}

openHAB_REST_URL = uservariables['openHAB_REST_URL']
openHAB_Devicename = 'Light_Garage_Loftlampe'
Domoticz_Devicename = 'Garage | Loftlampe'

Main script:

Code: Select all

TimeBetweenPresses = 2 --time that needs to be between presses (in seconds), before os.execute line is ran again

function datetimedifferencenow(s)
   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)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end

if (devicechanged[''..Domoticz_Devicename..''] == 'On') then
   if (datetimedifferencenow(uservariables_lastupdate[''..Domoticz_Devicename..'']) > TimeBetweenPresses ) then
      print('<b style="color:Blue">'..Domoticz_Devicename..', previous time was <b style="color:Green">MORE</b> than '..TimeBetweenPresses..' sec ago, switching ON</b>')
      os.execute('curl --header "Content-Type: text/plain" --request POST --data "ON" '..openHAB_REST_URL..''..openHAB_Devicename..' ')
      commandArray['Variable:'..Domoticz_Devicename..'']=tostring(os.time())
   else
      print('<b style="color:Blue">'..Domoticz_Devicename..', previous time was <b style="color:Red">LESS</b> than '..TimeBetweenPresses..' sec ago, ignore press of button</b>')
   end

elseif (devicechanged[''..Domoticz_Devicename..''] == 'Off') then
   if (datetimedifferencenow(uservariables_lastupdate[''..Domoticz_Devicename..'']) > TimeBetweenPresses ) then
      print('<b style="color:Blue">'..Domoticz_Devicename..', previous time was <b style="color:Green">MORE</b> than '..TimeBetweenPresses..' sec ago, switching OFF</b>')
      os.execute('curl --header "Content-Type: text/plain" --request POST --data "OFF" '..openHAB_REST_URL..''..openHAB_Devicename..' ')
      commandArray['Variable:'..Domoticz_Devicename..'']=tostring(os.time())
   else
      print('<b style="color:Blue">'..Domoticz_Devicename..', previous time was <b style="color:Red">LESS</b> than '..TimeBetweenPresses..' sec ago, ignore press of button</b>')
    end
end

Re: Need guidance - how to split a lua script up in two?

Posted: Thursday 12 May 2016 11:43
by SoerenBM
Just bumping this up to see if I can get some help, thanks.

BR Søren

Re: Split lua script up in two and call the new one from the first one?

Posted: Thursday 12 May 2016 14:22
by simonrg
SoerenBM wrote:I have been piecing a script together by using examples and reading from this forum. I have replaced the hardcoded parts with variables and was thinking that I could split the script up - one main script and one device specific so I can call the main script from all my device scripts using the device specific variables in the main script.
This is really about Lua not about Domoticz, so you would best to Google for information about how Lua libraries are created, your main script is effectively a library which the device script calls. For Lua generally have a look at http://www.lua.org and http://lua-users.org.

Sort of working the other way around, then have a look at this forum post:
http://www.domoticz.com/forum/viewtopic ... ork#p51804
Where a master script calls device specific scripts, there is no reason why the device specific script shouldn't then call a library or libraries.

Re: Split lua script up in two and call the new one from the first one?

Posted: Thursday 12 May 2016 14:45
by SweetPants

Re: Split lua script up in two and call the new one from the first one?

Posted: Thursday 12 May 2016 14:53
by PsychoMark
You can include scripts using the require() function, but there are a few things you need to keep in mind with regards to variable scope. commandArray in particular which needs to be global in order for Domoticz to read it back in. The easiest way to enforce this is by prefixing it with "_G."

In my Lua scripts I'm basically doing the same thing, moving common pieces of code to separate files, as described here: http://lua-users.org/wiki/ModulesTutorial. In order for the variables to cross between the modules (files) it's probably easiest to pass them as parameters. For example, your main script could look like:

Code: Select all

main = {}

main.SwitchDevice = function(openHAB_REST_URL, openHAB_Devicename, Domoticz_Devicename)

	...your checks here, and make sure to use:
	_G.commandArray['...']

end

return main

Then in your device script, do:

Code: Select all

local main = require('main')

main.SwitchDevice(uservariables['openHAB_REST_URL'], 'Light_Garage_Loftlampe', 'Garage | Loftlampe')

You can still read the arrays such as "devicechanged" without using _G by the way.

Hope that helps!

Re: Split lua script up in two and call the new one from the first one?

Posted: Saturday 14 May 2016 19:22
by SoerenBM
Thanks all for the guidance. Sometimes with new things you don't even know where to start looking.

Br. Søren


BR Søren

Sent using Tapatalk