LUA: create user variable from within LUA script?

Moderator: leecollings

Post Reply
User avatar
blitzkneisser
Posts: 43
Joined: Tuesday 01 December 2015 8:39
Target OS: Raspberry Pi / ODroid
Domoticz version: stable
Location: Villach, Austria
Contact:

LUA: create user variable from within LUA script?

Post 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?
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

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

Post 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
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de maison de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

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

Post 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
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

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

Post by papoo »

to late for me
User avatar
blitzkneisser
Posts: 43
Joined: Tuesday 01 December 2015 8:39
Target OS: Raspberry Pi / ODroid
Domoticz version: stable
Location: Villach, Austria
Contact:

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

Post by blitzkneisser »

Ah, yes, of course!
Thanks!
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

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

Post by jmleglise »

:-)

Interesting. I didn't know url_encode. I found the code here : http://lua-users.org/wiki/StringRecipes if you need.
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de maison de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

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

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

Who is online

Users browsing this forum: No registered users and 0 guests