Split lua script up in two and call the new one from the first one?
Posted: Tuesday 03 May 2016 22:11
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:
I was thinking to split it op like this
Device specific part:
Main script:
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