Lua generic function library

Must be cleaned all topics go to the right subforum
User avatar
remb0
Posts: 753
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Lua generic function library

Postby remb0 » Wednesday 19 August 2015 10:22

With a friend we made some nice lua scripts.. but when you made some scripts you always need the same coding for: rounding, calculate time differences etc.
So we made the following file: MyFunc.lua

Spoiler: show

Code: Select all

local My ={}

   function My.GetUserVar(UserVar)
      Waarde=uservariables[UserVar]
      if Waarde==nil then
         print(".  User variable not set for : " .. UserVar)
         UserVarErr=UserVarErr+1
      end
      return Waarde
   end

   function My.File_exists(file)
     local f = io.open(file, "rb")
     if f then f:close() end
     return f ~= nil
   end

   function My.Round(num, idp)
      local mult = 10^(idp or 0)
      return math.floor(num * mult + 0.5) / mult
   end

   function My.GetValue(Text, GetNr)
      Part=1
      for match in (Text..';'):gmatch("(.-)"..';') do
         if Part==GetNr then MyValue = tonumber(match) end
         Part=Part+1
      end
      return MyValue
   end
   
   -- replace the last character
   function My.EnumClear(Text)
      a=string.len(Text)
      b=string.sub(Text,a,a)
      if b=="," or b==" " then Text=string.sub(Text,1,a-1) end
      a=string.len(Text)
      b=string.sub(Text,a,a)
      if b=="," or b==" " then Text=string.sub(Text,1,a-1) end
      return Text
   end

   function My.ConvTime(TimeX)
      year = string.sub(TimeX, 1, 4)
      month = string.sub(TimeX, 6, 7)
      day = string.sub(TimeX, 9, 10)
      hour = string.sub(TimeX, 12, 13)
      minutes = string.sub(TimeX, 15, 16)
      seconds = string.sub(TimeX, 18, 19)
      ResTime = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
      return ResTime
   end

   function My.TimeDiff(Time1,Time2)
      if string.len(Time1)>12 then Time1 = My.ConvTime(Time1) end
      if string.len(Time2)>12 then Time2 = My.ConvTime(Time2) end   
      ResTime=os.difftime (Time1,Time2)
      return ResTime
   end
return My


You can use the functions in every lua script after you include the library with this lines:

Code: Select all

package.path = package.path .. ';' .. '/home/pi/domoticz/scripts/lua/?.lua'
My = require('MyFunc')


Example uasage

Check if file exist.

Code: Select all

   if not My.File_exists(LogFile) then


Get UserVar and Print when missing
Get value of a user variable, but when the user variable not exist the function prints print (". User variable not set for : " .. UserVar) to the log.
When you use this function for all the variables users who copy your script gets in logging a message which variables they must make.

Code: Select all

LogFile        = My.GetUserVar("SUN_LogFile")


Get the X value of a device
5 in this example can you change in the number of the value.
my Wind meter gives: 207.00;SSW;9;18;16.4;16.4 so I need fift value for temperature

Code: Select all

Variable      = My.GetValue(otherdevices_svalues[DeviceName],5)   


Round numbers.

Code: Select all

My.Round(ValueThatNeedsToRounded,1)


TimeDifferences
The example below gave to difference between now and the time that a devices is last changed in minutes

Code: Select all

TDiff = My.Round(My.TimeDiff(os.time(),otherdevices_lastupdate[DeviceManual])/60,0)




If you Have also some nice functions, reply with coding, explanation and example

Yann
Posts: 7
Joined: Tuesday 17 May 2016 14:42
Target OS: Windows
Domoticz version: 3.5143
Location: France
Contact:

Re: Lua generic function library

Postby Yann » Tuesday 17 May 2016 19:15

Worked great before but now, with the new Domoticz versions, Lua scripts are stored in the Domoticz database .... what is the package path now ?
Thanks
HP Microserver + Domoticz + Aeon Labs Z-Wave USB +RFX433

SweetPants
Posts: 1146
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V3.6792
Location: The Netherlands
Contact:

Re: Lua generic function library

Postby SweetPants » Tuesday 17 May 2016 20:59

Yann wrote:Lua scripts are stored in the Domoticz database

Only if you use the build in editor. You can still use the 'old' way for LUA scripts

Yann
Posts: 7
Joined: Tuesday 17 May 2016 14:42
Target OS: Windows
Domoticz version: 3.5143
Location: France
Contact:

Re: Lua generic function library

Postby Yann » Thursday 19 May 2016 15:27

Yes, you are right, will try this. Thanks !
HP Microserver + Domoticz + Aeon Labs Z-Wave USB +RFX433

Yann
Posts: 7
Joined: Tuesday 17 May 2016 14:42
Target OS: Windows
Domoticz version: 3.5143
Location: France
Contact:

Re: Lua generic function library

Postby Yann » Saturday 28 May 2016 14:22

Worked perfectly, thank you again !
HP Microserver + Domoticz + Aeon Labs Z-Wave USB +RFX433

User avatar
remb0
Posts: 753
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Lua generic function library

Postby remb0 » Saturday 28 May 2016 19:42

Yann wrote:Worked perfectly, thank you again !


thanks! feel free to post your creations to help other people.

Yann
Posts: 7
Joined: Tuesday 17 May 2016 14:42
Target OS: Windows
Domoticz version: 3.5143
Location: France
Contact:

Re: Lua generic function library

Postby Yann » Wednesday 01 June 2016 12:31

Good point ! Let's do it
I wanted to create a library to send pushbullet messages to specific users. In Domoticz, the notifications settings allow only 1 pushbullet user, but I wanted to send messages either to my wife or myself. In other cases, I wanted to send a message only on Pushbullet (vs all notifications SMS/Email ...).
Hence this function
First you need to install curl in C:\programs\Curl (or other directory but then you have to change the command line bellow)

SEND MESSAGE TO PUSHBULLET
Message should be in this format 'title;message' ... so avoid ';' in your title :D
If you need to insert space in the text, use %20 instead otherwise the command line won't work

Code: Select all

local Lib={};

  function Lib.Pushbullet(Message)
    local pb_token = 'yourpushbullettoken'
    local pb_total = Message
    local val=string.find(pb_total,";")
    local pb_title = string.sub(pb_total,1,val-1)
    local pb_body = string.sub(pb_total,val+1)


    local pb_command = 'c:\\Programs\\Curl\\curl -u ' .. pb_token .. ': "https://api.pushbullet.com/v2/pushes" -d type=note -d title="' .. pb_title .. '" -d body="' .. pb_body ..'"'
 
    -- Run curl command
    exec_success = os.execute(pb_command)

  end

return Lib


Then you can use it anywhere like this

Code: Select all

Lib.Pushbullet('essai;text')


Hope it will help someone !
HP Microserver + Domoticz + Aeon Labs Z-Wave USB +RFX433


Return to “Scripts [cleaning in progress]”

Who is online

Users browsing this forum: No registered users and 1 guest