Page 1 of 1

single control script(s) - lua

Posted: Tuesday 07 March 2017 21:12
by darlomrh
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

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 = [[]]
      },
  }

and so on

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 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:

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
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

Re: single control script(s) - lua

Posted: Saturday 26 August 2017 20:58
by simon_rb
How well did this work?

Thinking about pass2php however that would take a lot of learning.

I'm looking at consolidating all of my Lua device scripts as its now putting excessive load on the CPU.

Cheers

Re: single control script(s) - lua

Posted: Saturday 26 August 2017 21:57
by zicht
In my experience the load of lua is low, but you need to make sure your scripts are correct : not firing unneeded devices on or off constantly.
Also in my experience lua performs better with one big file than tons of separate script files.
Speed of one single file is below 0.01 sec normally on my system. (calling external websites not included as this is more external than lua related)

To make more/better to follow i have constructed one lua file containing all functionss named functions.lua, include that file in a time or device script and then calling the functions from the normal script files.
It has also benefits as you can reuse the same code in time AND device scripts without writing it twice :)

Code: Select all

package.path = package.path .. ';' .. 'C:/PROGRA~2/domoticz/scripts/lua/functions.lua;'
local my1 = require ("functions")
Everybody has his own system that feels/works good for him/her. The above could work very well and looks really nice .. Thanks for sharing.

Re: single control script(s) - lua

Posted: Monday 04 September 2017 7:20
by darlomrh
Apologies for not replying. No notification of replies. I suppose one thing I didn't explain very well in my original post. "I find this helps me without the overhead of something super rich as dzVents", really should have read "I find this helps me without the learning overhead of something super rich as dzVents".

I created it as I thought scanning all scripts seemed a waste each time a device/variable changed just scan the one and have separate folders that contain all devices and one for variable and one time and so on. I find smaller scripts easier to maintain.