After a very short period of toying with Blockly, I have been learning and playing with LUA scripts, and found they are very flexible.
But now that the number of scripts is growing, I find that I would like to put some frequently used sequences of LUA lines into a library of functions or macro's, whichever is the better word for it. I would want to be able to call these functions from any LUA script.
Is this possible, and if so: how?
library of functions
Moderator: leecollings
-
- Posts: 749
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
library of functions
Hans
-
- Posts: 370
- Joined: Monday 05 October 2015 10:16
- Target OS: -
- Domoticz version:
- Contact:
Re: library of functions
For python there's import,
Also LUA has function libraries. Take a look at http://www.lua.org/manual
Section 5.2 talks about how to call them.
Downside is you need to program these in C etcetera.
Also LUA has function libraries. Take a look at http://www.lua.org/manual
Section 5.2 talks about how to call them.
Downside is you need to program these in C etcetera.
-
- Posts: 329
- Joined: Tuesday 16 July 2013 22:54
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 3.8807
- Location: North East England
- Contact:
Re: library of functions
You don't need to program in C or use Python, Lua has really clever ways of incorporating common code using modules with a function called require. This can become quite sophisiticated allowing you to have flexible ways of keeping specific functions together, have a look at https://www.tutorialspoint.com/lua/lua_modules.htm.
You can equally make it really simple by just reading in a file using dofile - http://luatut.com/dofile.html
Just put your code in a file and then:
If you learn how to use Lua in this way then this will be transferable to any other uses of Lua.
The alterantive is to use a framework, which you will find several mentioned in the forum, but they may be too complicated for what you need and any learning is only relevant to that framework with Domoticz.
You can equally make it really simple by just reading in a file using dofile - http://luatut.com/dofile.html
Just put your code in a file and then:
Code: Select all
dofile("/home/pi/domoticz/scripts/lua/mylibraryfile.lua")
The alterantive is to use a framework, which you will find several mentioned in the forum, but they may be too complicated for what you need and any learning is only relevant to that framework with Domoticz.
Last edited by simonrg on Saturday 26 November 2016 18:15, edited 1 time in total.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
-
- Posts: 370
- Joined: Monday 05 October 2015 10:16
- Target OS: -
- Domoticz version:
- Contact:
Re: RE: Re: library of functions
Wasn't aware of this. As i only read briefly my quoted site.simonrg wrote:You don't need to program in C or use Python, Lua has really clever ways of incorporating common code using modules with a function called require. This can become quite sophisiticated allowing you to have flexible ways of keeping specific functions together, have a look at https://www.tutorialspoint.com/lua/lua_modules.htm.
Used to use libraries in perl/python/bash and couldn't imagion it wasn't available in lua.
-
- Posts: 749
- Joined: Saturday 27 February 2016 12:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2020.2
- Location: NL
- Contact:
Re: library of functions
I suspect this will not work for me: I use a Windows-10 based server to run Domoticz...simonrg wrote:You don't need to program in C or use Python, Lua has really clever ways of incorporating common code using modules with a function called require. This can become quite sophisiticated allowing you to have flexible ways of keeping specific functions together, have a look at https://www.tutorialspoint.com/lua/lua_modules.htm.
You can equally make it really simple by just reading in a file using dofile - http://luatut.com/dofile.html
Just put your code in a file and then:If you learn how to use Lua in this way then this will be transferable to any other uses of Lua.Code: Select all
dofile("\home\pi\domoticz\scripts\lua\mylibraryfile.lua")
The alterantive is to use a framework, which you will find several mentioned in the forum, but they may be too complicated for what you need and any learning is only relevant to that framework with Domoticz.

Hans
-
- Posts: 329
- Joined: Tuesday 16 July 2013 22:54
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 3.8807
- Location: North East England
- Contact:
Re: library of functions
Windows shouldn't make any difference, you just need to put the paths into the right format.manjh wrote:I suspect this will not work for me: I use a Windows-10 based server to run Domoticz...
As I don't use Windows for Domoticz, I just Googled lua dofile windows (other search engines are available), found things like http://stackoverflow.com/questions/2234 ... in-windows.
So either put the file in the same directory as Domoticz is executing Lua (which could be the directory you put the script_device_something.lua files)
Code: Select all
dofile("mylibraryfile.lua")
Code: Select all
dofile("c:\\users\\pi\\domoticz\\scripts\\lua\\mylibraryfile.lua")
Code: Select all
dofile([[c:\users\pi\domoticz\scripts\lua\mylibraryfile.lua")
Code: Select all
dofile("/home/pi/domoticz/scripts/lua/mylibraryfile.lua")
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Who is online
Users browsing this forum: No registered users and 1 guest