LUA, include file

Moderator: leecollings

Post Reply
gertlind1
Posts: 29
Joined: Wednesday 25 December 2013 12:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

LUA, include file

Post by gertlind1 »

I have a bunch of functions() that i use in my LUA scripts.
Is it possible to have those in a separate file instead of copy them to all my LUA scripts?

I have recently started to use the the built in LUA editor if that matters.
---
Gert
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

Re: LUA, include file

Post by jmleglise »

I don't know for built in editor. But with file, you can do like this :
json = (loadfile "/home/pi/domoticz/scripts/lua/json.lua")() -- For Linux
json = (loadfile "D:\\Domoticz\\scripts\\lua\\json.lua")() -- For Windows

See example here : https://www.domoticz.com/wiki/Lua_-_json.lua
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
User avatar
nayr
Posts: 354
Joined: Tuesday 11 November 2014 18:42
Target OS: Linux
Domoticz version: github
Location: Denver, CO - USA
Contact:

Re: LUA, include file

Post by nayr »

I dont think the lua editor actually saves files on the filesystem, so your lua include is going to have to be maintained externally..

for me I created a folder /usr/local/share/lua/5.2 and linked a file called libs.lua into that folder.

see: https://github.com/nayrnet/domoticz-scr ... a/libs.lua

then here is an example usage in a script:
https://github.com/nayrnet/domoticz-scr ... ntdoor.lua
Debian Jessie: CuBox-i4 (Primary) w/Static Routed IP and x509 / BeagleBone with OpenSprinkler / BeagleBone Planted Aquarium / 3x Raspbery Pi2b GPIO Slaves
Elemental Theme - node-domoticz-mqtt - Home Theatre Controller - AndroidTV Simple OSD Remote - x509 TLS Auth
gertlind1
Posts: 29
Joined: Wednesday 25 December 2013 12:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA, include file

Post by gertlind1 »

Thanks guys.
i will play around with it,
---
Gert
gertlind1
Posts: 29
Joined: Wednesday 25 December 2013 12:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA, include file

Post by gertlind1 »

:-(

Of course I can't get it to work.

Created the /usr/local/share/lua/lua5.2/
Created a file there named libs.lua

Code: Select all

local libs = {};

function GroupState(sArray)
        -- v1.0. 2016-01-18
        -- Checks the state of a group of swithces.
        -- USAGE   : GroupState({'Switch1','Switch2','Switch3','Switch4','Switch5',more switches})
        -- RETURNS : 'On'       if all switches are on.
        --         : 'Off'      if all switches are off.
        --         : 'Mixed"    if one or more, but not all switches are on.
        --
        local iState = 0
        local iCount = 0
        local sState = ''
        for i,light in pairs(sArray) do
                if (otherdevices[light] == 'On') then
                        iState = iState + 1
                end
        iCount = iCount + 1
        end
        if(iState == 0) then sState = "Off" end
        if(iState > 0) then sState = 'Mixed' end
        if(iState == iCount) then sState = 'On' end
--      print("iCount   : " .. iCount)
--      print("iState   : " .. iState)
--      print("sState   : " .. sState)
        print "LIBS LUA"
        return sState
end

return libs
made the soft link in my /domoticz/scripts/lua/

Code: Select all

libs.lua -> /usr/local/share/lua/5.2/
calling it from here

Code: Select all

commandArray = {}
libs = require("libs")
aLivingroom = {'Hörnbord','Soffbord','Blomma','Fönster','Bokhylla'}
sCounterName = 'Rörelse räknare'

if (devicechanged['Rörelse'] == 'On' and libs.GroupState(aLivingroom) == 'Off') then
        time = os.date("*t")
        if (SunSet() < 60 and time.hour <= 20) then
                commandArray['Group:Alla Vardagsrum']='On'
        end
        if(time.hour >= 21) then
            commandArray['Hörnbord']='On FOR 1'
        end
else
        difference = TimeDifference('Rörelse')
        myTime = SecondsToClock(tostring(difference))
        commandArray['UpdateDevice'] = '10|0|'..myTime..' sedan senaste rörelsen.'
end
return commandArray
Getting this error:

Code: Select all

Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_motion.lua: /home/pi/domoticz/scripts/lua/script_device_motion.lua:75: attempt to call field 'GroupState' (a nil value)
---
Gert
gertlind1
Posts: 29
Joined: Wednesday 25 December 2013 12:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA, include file

Post by gertlind1 »

Forget it.
I cannot read or my glasses are dirty.

missed this
function lGroupState(sArray)

Did not add the "libs."GroupsState(sArray)

Works fine now.
Will move all my functions to libs.lua.

Tanks for the tip and the instruction.
---
Gert
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: LUA, include file

Post by Dnpwwo »

Scripts created through the script editor are stored in the database but the only difference is how they are loaded by Domoticz prior to being handed to the Lua engine. Functionally external file inclusion should work the same way regardless of where the script is stored.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
gertlind1
Posts: 29
Joined: Wednesday 25 December 2013 12:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA, include file

Post by gertlind1 »

Dnpwwo wrote:Scripts created through the script editor are stored in the database but the only difference is how they are loaded by Domoticz prior to being handed to the Lua engine. Functionally external file inclusion should work the same way regardless of where the script is stored.
Yes, works fine.

Added a lua script in the Domoticz internal editor

Code: Select all

libs = require("libs")

commandArray = {}
if (otherdevices['Rörelse'] == 'On') then
    print ('Time to Sunset : ' .. libs.SecondsToClock(tostring(libs.SunSet())))
end
return commandArray
Works as a charm.

Code: Select all

2016-03-12 16:09:23.602  LUA: Time to Sunset : 00:01:33
Would it be possible to have the "functions file" libs.lua in the internal editor also?
That would be great.

Maybe if libs.lua is put in domoticz/scripts/lua
and the soft link that points to lib.lua is in the /usr/local/share/lua/lua5.2/

??
---
Gert
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: LUA, include file

Post by dannybloe »

<shameless-plug>
Or you use this: dzVents :mrgreen:
</shameless-plug>
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest