Logic Machine Forum
Script.create? - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Script.create? (/showthread.php?tid=1829)



Script.create? - FatMax - 12.01.2019

I’m increasingly reusing different scripts in projects and as some may know, it might be a tedious effort to create the different scripts and group addresses on each project. 

I’m just wondering if there is a plan to create a sort of script.create function in the software in the near term?


RE: Script.create? - Daniel - 14.01.2019

Hi
Why would you want this?
BR


RE: Script.create? - Thomas - 14.01.2019

I think the idea is clear. If you make a new visualisation for block of N buildings where all houses are the same but group addresses differs (because you use a numbering scheme) then you want to create only one visualisation and copy it N times. This tool is heavily missing at LM. What I suggested (unsuccessfully) was a set of scripts which parse the whole project to (many) Excel sheets. A system implementator can bulk modify what he wants and re-create the project back by another script.


RE: Script.create? - FatMax - 16.01.2019

Thomas use case makes sense, but for me personally it would allow me to build one install script for repetitive functions that depends on event and resident/scheduled scripts and different group addresses that needs to be created.

It would also allow me to install functions remotely from a server and perhaps update scripts with a script.insert command.

I’m sure many users would benefit from this sort of functionality.


RE: Script.create? - Jayce - 17.01.2019

I like the idea of it, I'm sure it would be very useful.


RE: Script.create? - admin - 17.01.2019

Here's an example of how to create script from another script:
Code:
data = {
   -- either event, resident or scheduled
   type = 'event',
   -- unique script name
   name = 'my script',
   -- 1 = active, 0 = inactive
   active = 1,
   -- event script: group address or tag
   -- resident script: sleep time in seconds (0..60)
   -- scheduled script: execution date/time in CRON format
   params = '1/1/1',
   -- set to 1 to enable "execute on read" for event scripts
   -- not used for resident and scheduled scripts
   subparams = 0,
   -- script Lua code
   script = 'log(123)',
   -- optional script category
   category = '',
   -- optional script description
   description = '',
}

-- check for duplicate script with the same name
exists = script.get(data.name)
if not exists then
   db:insert('scripting', data)

   data.id = db:getlastautoid()

   script.save(data, true)
   script.reloadsingle(data)
end



RE: Script.create? - FatMax - 18.01.2019

Thanks!


RE: Script.create? - epps - 18.04.2020

(17.01.2019, 12:59)admin Wrote: Here's an example of how to create script from another script:
Code:
data = {
   -- either event, resident or scheduled
   type = 'event',
   -- unique script name
   name = 'my script',
   -- 1 = active, 0 = inactive
   active = 1,
   -- event script: group address or tag
   -- resident script: sleep time in seconds (0..60)
   -- scheduled script: execution date/time in CRON format
   params = '1/1/1',
   -- set to 1 to enable "execute on read" for event scripts
   -- not used for resident and scheduled scripts
   subparams = 0,
   -- script Lua code
   script = 'log(123)',
   -- optional script category
   category = '',
   -- optional script description
   description = '',
}

-- check for duplicate script with the same name
exists = script.get(data.name)
if not exists then
   db:insert('scripting', data)

   data.id = db:getlastautoid()

   script.save(data, true)
   script.reloadsingle(data)
end
Hey guys,
how can i insert a three or four line code there ?


RE: Script.create? - admin - 19.04.2020

Like this:
Code:
str = [[multi
line
string]]



RE: Script.create? - epps - 19.04.2020

(19.04.2020, 09:27)admin Wrote: Like this:
Code:
str = [[multi
line
string]]
Thanks Admin.


RE: Script.create? - JohnTH - 17.08.2022

(17.01.2019, 12:59)admin Wrote: Here's an example of how to create script from another script:
Code:
data = {
   -- either event, resident or scheduled
   type = 'event',
   -- unique script name
   name = 'my script',
   -- 1 = active, 0 = inactive
   active = 1,
   -- event script: group address or tag
   -- resident script: sleep time in seconds (0..60)
   -- scheduled script: execution date/time in CRON format
   params = '1/1/1',
   -- set to 1 to enable "execute on read" for event scripts
   -- not used for resident and scheduled scripts
   subparams = 0,
   -- script Lua code
   script = 'log(123)',
   -- optional script category
   category = '',
   -- optional script description
   description = '',
}

-- check for duplicate script with the same name
exists = script.get(data.name)
if not exists then
   db:insert('scripting', data)

   data.id = db:getlastautoid()

   script.save(data, true)
   script.reloadsingle(data)
end

Hi,

I am using this for for 'autosetup' LM with a startup (init) script and it has been very useful. 
Is there any simular way to add functions to 'common functions' (or overwrite the file) ?


RE: Script.create? - admin - 18.08.2022

Use this:
Code:
script.checkuserlib(common_functions_code)
script.reload()



RE: Script.create? - JohnTH - 18.08.2022

(18.08.2022, 06:35)admin Wrote: Use this:
Code:
script.checkuserlib(common_functions_code)
script.reload()

Works great, thanks!


RE: Script.create? - FatMax - 13.09.2023

Onto the next question;

Is it also possible to create user libraries with a script?


RE: Script.create? - admin - 14.09.2023

You can use webrequest library in latest firmware to emulate web UI requests:
Code:
webrequest = require('webrequest')

-- user library name
libname = 'test'

-- create user library
params = {
  data = {
    id = 'user.' .. libname,
    name = libname,
    type = 'user',
    source = true, -- keep source
    load = false, -- autoload
    description = 'test description',
  }
}

res, err = webrequest('scripting', 'save', params)
log(res, err)

-- save user library source
params = {
  data = {
    id = 'user.' .. libname,
    scriptonly = true,
  },
  script = [[
    log('test user library')
  ]]
}

res, err = webrequest('scripting', 'save', params)
log(res, err)



RE: Script.create? - FatMax - 15.09.2023

Thank you again for the great help. Is this web request library documented anywhere? I would like to also create new Modbus-profiles and this would be very helpful.

I guess search is my friend: https://forum.logicmachine.net/showthread.php?tid=4425