UserVariables in scrips

Moderator: leecollings

Post Reply
jjdom
Posts: 12
Joined: Tuesday 26 July 2016 13:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: France
Contact:

UserVariables in scrips

Post by jjdom »

Hi all,
Is there any way to create the user variable in domoticz by lua script ?
José
Domoticz raspberry pi
Version 3.4834
georgesattali
Posts: 84
Joined: Saturday 05 March 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: UserVariables in scrips

Post by georgesattali »

yes, here an example :

but beware, that using commandArray, the variable will be created only when the script exits.
otherwise, you may use curl with the same kind of http call.

details here : https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s

Code: Select all

ip = '127.0.0.1:8080'   -- user:pass@ip:port de domoticz

function UrlEncode(str)     -- source : http://lua-users.org/wiki/StringRecipes   
   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

varName = "My Var Name"
if (uservariables[varName] == nil) then
      i=1
      urlVarName = UrlEncode(varName)
      commandArray[i]= { ['OpenURL'] = ip            -- create variable 
	    ..'/json.htm?type=command&param=saveuservariable'
	    ..'&vname='..urlVarName
	    ..'&vtype=2'
	    ..'&vvalue='.."0"
end	    
Bye, GD
jjdom
Posts: 12
Joined: Tuesday 26 July 2016 13:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: France
Contact:

Re: UserVariables in scrips

Post by jjdom »

Hi GD,
Thanks a lot, I will try this asap
José
Domoticz raspberry pi
Version 3.4834
jjdom
Posts: 12
Joined: Tuesday 26 July 2016 13:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: France
Contact:

Re: UserVariables in scrips

Post by jjdom »

Hi all

I have tried this one:

Code: Select all

 urlVarName = UrlEncode('MonTest')
      commandArray[1]= { ['OpenURL'] = ip            -- create variable 
       ..'/json.htm?type=command&param=saveuservariable'
      ..'&vname='..urlVarName
       ..'&vtype=2'
       ..'&vvalue='..'"0"'}
and the result in logs is

Code: Select all

2016-08-02 13:58:44.351 Error: Error opening url: X.X.X.X:Y/json.htm?type=command¶m=saveuservariable&vname=MonTest&vtype=2&vvalue="10"
If I put the url in a local variable, and print it, I get the same result.

is there any issue for this ?
José
Domoticz raspberry pi
Version 3.4834
jjdom
Posts: 12
Joined: Tuesday 26 July 2016 13:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: France
Contact:

Re: UserVariables in scrips

Post by jjdom »

Hi all

My problem is solved.
I'had made a mistake on my IP.

Thanks a lot GD and all others.
José
Domoticz raspberry pi
Version 3.4834
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest