library of functions

Moderator: leecollings

Post Reply
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

library of functions

Post by manjh »

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?
Hans
trixwood

Re: library of functions

Post by trixwood »

stlaha2007
Posts: 370
Joined: Monday 05 October 2015 10:16
Target OS: -
Domoticz version:
Contact:

Re: library of functions

Post by stlaha2007 »

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

Post by simonrg »

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:

Code: Select all

dofile("/home/pi/domoticz/scripts/lua/mylibraryfile.lua")
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.
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
stlaha2007
Posts: 370
Joined: Monday 05 October 2015 10:16
Target OS: -
Domoticz version:
Contact:

Re: RE: Re: library of functions

Post by stlaha2007 »

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.
Wasn't aware of this. As i only read briefly my quoted site.

Used to use libraries in perl/python/bash and couldn't imagion it wasn't available in lua.
manjh
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

Post by manjh »

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:

Code: Select all

dofile("\home\pi\domoticz\scripts\lua\mylibraryfile.lua")
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.
I suspect this will not work for me: I use a Windows-10 based server to run Domoticz... :cry:
Hans
simonrg
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

Post by simonrg »

manjh wrote:I suspect this will not work for me: I use a Windows-10 based server to run Domoticz... :cry:
Windows shouldn't make any difference, you just need to put the paths into the right format.

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")
Or this may work

Code: Select all

dofile("c:\\users\\pi\\domoticz\\scripts\\lua\\mylibraryfile.lua")
Or according to stackoverflow this may also work

Code: Select all

dofile([[c:\users\pi\domoticz\scripts\lua\mylibraryfile.lua")
Oops I did all the slashes Windows way round in the first email, for Linux it should be:

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

Who is online

Users browsing this forum: No registered users and 1 guest