single control script(s) - lua
Posted: Tuesday 07 March 2017 21:12
Hi All,
I thought I would share with you something I have knocked up to help split device,variable scripts into folders and help organise things.
I apologise if this has been done before and I'm sure there are better ways. I find this helps me without the overhead of something super rich as dzVents.
Imagine a simple file with device names and script locations
and so on
you then have one control script in the main lua directory
The local deviceArray=dofile(commonPath..'device_db.lua') loads the scripts into an array which you loop through each time an event is fired. If the device is found in the array it will run the file in the deviceScript field of the array.
The script sets up commandArray as a global for all of the scripts that are called using dofile. This also allows you to nest dofiles.
You can do the same for variables too:
I guess this may not be the correct way of doing things within the lua paradigm, but it works for me and someone may find it useful.
Cheers
I thought I would share with you something I have knocked up to help split device,variable scripts into folders and help organise things.
I apologise if this has been done before and I'm sure there are better ways. I find this helps me without the overhead of something super rich as dzVents.
Imagine a simple file with device names and script locations
Code: Select all
return{
{
deviceName = "Sensor-Front-Door",
deviceType = "door_sensor",
deviceScript = "/home/pi/domoticz/scripts/lua/device/sensor_front_door.lua",
description = [[]]
},
{
deviceName = "Boost Heating",
deviceType = "on_off_switch",
deviceScript = "/home/pi/domoticz/scripts/lua/device/boost_heating.lua",
description = [[]]
},
{
deviceName = "PIR-Hall",
deviceType = "motion_sensor",
deviceScript = "/home/pi/domoticz/scripts/lua/device/pir.lua",
description = [[]]
},
{
deviceName = "PIR-Lounge",
deviceType = "motion_sensor",
deviceScript = "/home/pi/domoticz/scripts/lua/device/pir.lua",
description = [[]]
},
{
deviceName = "PIR-Landing",
deviceType = "motion_sensor",
deviceScript = "/home/pi/domoticz/scripts/lua/device/pir.lua",
description = [[]]
},
{
deviceName = "Sensor-Garage-Door",
deviceType = "door_sensor",
deviceScript = "/home/pi/domoticz/scripts/lua/device/sensor_garage_door.lua",
description = [[]]
},
{
deviceName = "sys-setpoint",
deviceType = "temp_sensor",
deviceScript = "/home/pi/domoticz/scripts/lua/device/sys_setpoint.lua",
description = [[]]
},
}
you then have one control script in the main lua directory
Code: Select all
--[[
---------------------------------------------------------
Master script for all devices
Script Name : script_device_control.lua
Script Path : /home/pi/domoticz/scripts/lua/
---------------------------------------------------------
--]]
local currentPath = debug.getinfo(1).source:match("@?(.*/)")
local deviceFolderPath = currentPath ..'device/'
local commonPath = currentPath ..'common/'
local deviceArray=dofile(commonPath..'device_db.lua')
commandArray = {}
-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
-- print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
for key in pairs(deviceArray) do
if (deviceName == deviceArray[key].deviceName) then
dofile(deviceArray[key].deviceScript)
end
end
end
return commandArray
The script sets up commandArray as a global for all of the scripts that are called using dofile. This also allows you to nest dofiles.
You can do the same for variables too:
Code: Select all
--[[
---------------------------------------------------------
Master script for all variables
Script Name : script_variable_control.lua
Script Path : /home/pi/domoticz/scripts/lua/
---------------------------------------------------------
--]]
local currentPath = debug.getinfo(1).source:match("@?(.*/)")
local commonPath = currentPath ..'common/'
local variableArray=dofile(commonPath..'variable_script_db.lua')
commandArray = {}
-- loop through all the changed variables
for variableName,variableValue in pairs(uservariablechanged) do
for key in pairs(variableArray) do
if (variableName == variableArray[key].variableName) then
dofile(variableArray[key].script)
end
end
end
return commandArray
Cheers