Page 1 of 1

LUA: create user variable from within LUA script?

Posted: Sunday 01 May 2016 21:40
by blitzkneisser
Hi!

Is it possible to create a new user variable from within a LUA script, e.g. checking if a variable exists and if not, creating it?

Re: LUA: create user variable from within LUA script?

Posted: Sunday 01 May 2016 22:21
by jmleglise
Yes,

Check with this :

Code: Select all

      if not uservariables['name'] then
          print('User Variable  doesnt exist.')
       end    
and create with the API; look here :
https://www.domoticz.com/wiki/Domoticz_ ... w_variable

Re: LUA: create user variable from within LUA script?

Posted: Sunday 01 May 2016 22:24
by papoo

Code: Select all

if(uservariables['variable_name'] == nil) then 
commandArray['OpenURL']=url..'/json.htm?type=command&param=saveuservariable&vname='..url_encode('variable_name')..'&vtype=2&vvalue=1'
else
...
user variables wiki

Re: LUA: create user variable from within LUA script?

Posted: Sunday 01 May 2016 22:24
by papoo
to late for me

Re: LUA: create user variable from within LUA script?

Posted: Sunday 01 May 2016 22:27
by blitzkneisser
Ah, yes, of course!
Thanks!

Re: LUA: create user variable from within LUA script?

Posted: Sunday 01 May 2016 22:35
by jmleglise
:-)

Interesting. I didn't know url_encode. I found the code here : http://lua-users.org/wiki/StringRecipes if you need.

Re: LUA: create user variable from within LUA script?

Posted: Sunday 01 May 2016 22:59
by papoo

Code: Select all

function url_encode(str)
  if (str) then
    str = string.gsub (str, "\n", "\r\n")
    str = string.gsub (str, "([^%w %-%_%.%~])",
        function (c) return string.format ("%%%02X", string.byte(c)) end)
    str = string.gsub (str, " ", "+")
  end
  return str   
end