Page 1 of 1

convert Blocky to LUA

Posted: Monday 08 May 2017 9:53
by poudenes
Hi Guys,

I have a long list where i change "user variable" to trigger blocky actions. But want to change 1 specific one to make it easier to read.
See attachment what its now and how can i change this into LUA
Screen Shot 2017-05-08 at 09.47.55.jpg
Screen Shot 2017-05-08 at 09.48.06.jpg
Thanks for helping and push me in right direction of learning LUA code :)

Re: convert Blocky to LUA

Posted: Monday 08 May 2017 10:56
by freijn
Instead of Blocky you select Lua
Please select 'userVarialbel' on the next drop down list !!!

Paste the below code and give it a try :-)

Code: Select all

commandArray = {}
if (uservariables["AllOff"] == "1") then
   
	uservariables["ComeHome"] == "0"
	uservariables["Evening"] == "0"
end
return commandArray

Re: convert Blocky to LUA

Posted: Monday 08 May 2017 10:58
by poudenes
freijn wrote:Instead of Blocky you select Lua
Please select 'userVarialbel' on the next drop down list !!!

Paste the below code and give it a try :-)

Code: Select all

commandArray = {}
if (uservariables["AllOff"] == "1") then
   
	uservariables["ComeHome"] == "0"
	uservariables["Evening"] == "0"
end
return commandArray
Thanks !! i give it a shot !!

Re: convert Blocky to LUA

Posted: Monday 08 May 2017 11:23
by poudenes
This is not working:

error: 2017-05-08 11:22:31.899 Error: EventSystem: in MAN SCENE ON 17.05.08 (TEST): [string "commandArray = {}..."]:3: syntax error near '=='

the other user variable must updated to 0

Code: Select all

commandArray = {}
if (uservariables["All Off"] == "1") then
    uservariables["ComeHome"] == "0"
    uservariables["Evening"] == "0"
    uservariables["Goodnight"] == "0"
    uservariables["Leaving"] == "0"
    uservariables["Morning"] == "0"
    uservariables["Movie"] == "0"
    uservariables["Sexy"] == "0"
    uservariables["TV"] == "0"
   
elseif (uservariables["ComeHome"] == "1") then
    uservariables["All Off"] == "0"
    uservariables["Evening"] == "0"
    uservariables["Goodnight"] == "0"
    uservariables["Leaving"] == "0"
    uservariables["Morning"] == "0"
    uservariables["Movie"] == "0"
    uservariables["Sexy"] == "0"
    uservariables["TV"] == "0"
   
elseif (uservariables["Evening"] == "1") then
    uservariables["All Off"] == "0"
    uservariables["ComeHome"] == "0"
    uservariables["Goodnight"] == "0"
    uservariables["Leaving"] == "0"
    uservariables["Morning"] == "0"
    uservariables["Movie"] == "0"
    uservariables["Sexy"] == "0"
    uservariables["TV"] == "0"
   
elseif (uservariables["Goodnight"] == "1") then
    uservariables["All Off"] == "0"
    uservariables["ComeHome"] == "0"
    uservariables["Evening"] == "0"
    uservariables["Leaving"] == "0"
    uservariables["Morning"] == "0"
    uservariables["Movie"] == "0"
    uservariables["Sexy"] == "0"
    uservariables["TV"] == "0"
   
elseif (uservariables["Leaving"] == "1") then
   uservariables["ComeHome"] == "0"
   uservariables["All Off"] == "0"
   uservariables["Evening"] == "0"
   uservariables["Goodnight"] == "0"
   uservariables["Morning"] == "0"
   uservariables["Movie"] == "0"
   uservariables["Sexy"] == "0"
   uservariables["TV"] == "0"
   
elseif (uservariables["Morning"] == "1") then
    uservariables["All Off"] == "0"
    uservariables["ComeHome"] == "0"
    uservariables["Evening"] == "0"
    uservariables["Leaving"] == "0"
    uservariables["Movie"] == "0"
    uservariables["Sexy"] == "0"
    uservariables["TV"] == "0"
    
elseif (uservariables["Movie"] == "1") then
    uservariables["All Off"] == "0"
    uservariables["ComeHome"] == "0"
    uservariables["Evening"] == "0"
    uservariables["Leaving"] == "0"
    uservariables["Morning"] == "0"
    uservariables["Sexy"] == "0"
    uservariables["TV"] == "0"
    
elseif (uservariables["Sexy"] == "1") then
    uservariables["All Off"] == "0"
    uservariables["ComeHome"] == "0"
    uservariables["Evening"] == "0"
    uservariables["Leaving"] == "0"
    uservariables["Morning"] == "0"
    uservariables["Movie"] == "0"
    uservariables["TV"] == "0"
    
elseif (uservariables["TV"] == "1") then
    uservariables["All Off"] == "0"
    uservariables["ComeHome"] == "0"
    uservariables["Evening"] == "0"
    uservariables["Leaving"] == "0"
    uservariables["Morning"] == "0"
    uservariables["Movie"] == "0"
    uservariables["Sexy"] == "0"

end
return commandArray

Re: convert Blocky to LUA

Posted: Monday 08 May 2017 12:03
by manjh
The error is in the "==".

You use "==" to compare, and "="to assign.
for example:

Code: Select all

if (a==b) then
	c = 1
	d = 2
	e = 3
end
Having said this, the way to assign a value to a user variable is not by simply assigning.
I use this to assign a value to variable named "TestVariable":

Code: Select all

commandArray['Variable:TestVariable"] = 3

Re: convert Blocky to LUA

Posted: Monday 08 May 2017 12:24
by poudenes
manjh wrote:The error is in the "==".

You use "==" to compare, and "="to assign.
for example:

Code: Select all

if (a==b) then
	c = 1
	d = 2
	e = 3
end
Having said this, the way to assign a value to a user variable is not by simply assigning.
I use this to assign a value to variable named "TestVariable":

Code: Select all

commandArray['Variable:TestVariable"] = 3

Code: Select all

commandArray = {}
if (uservariables["All Off"] == "1") then
	commandArray['Variable:ComeHome"] = "0"
	commandArray['Variable:Evening"] = "0"
	commandArray['Variable:Goodnight"] = "0"
	commandArray['Variable:Leaving"] = "0"
	commandArray['Variable:Morning"] = "0"
	commandArray['Variable:Movie"] = "0"
	commandArray['Variable:Sexy"] = "0"
	commandArray['Variable:TV"] = "0"
   
elseif (uservariables["ComeHome"] == "1") then
    uservariables["All Off"] = "0"
    uservariables["Evening"] = "0"
    uservariables["Goodnight"] = "0"
    uservariables["Leaving"] = "0"
    uservariables["Morning"] = "0"
    uservariables["Movie"] = "0"
    uservariables["Sexy"] = "0"
    uservariables["TV"] = "0"
    end
return commandArray
Sp the first IF is the correct one to use?

And how can i given that the variable type must stay as "Integer" (type 0) something like "and uservariables type = "0" ?

commandArray['Variable:ComeHome"] = "0" and uservariables type = "0"

sorry for the dumb question... hehe

Re: convert Blocky to LUA

Posted: Monday 08 May 2017 12:31
by manjh
Depending on the definition of the variable, you can use "tonumber" or "tostring" functions.
This is the nice thing about Domoticz: simply try it and see what works.
In my server, I have defined a dummy switch called "Testbutton". When I want to test some code, I put it into a LUA script (type "device") like this:

Code: Select all

-- Test script
commandArray = {}
--if devicechanged['Testbutton']  then
    print ("@@@@@@@@ invoking tester")

    -- put the code to be tested here

    print ("@@@@@@@@ ending  tester")    
--end
return commandArray
This way I can change the code, press the test button, and see what happens.....trial-and-error, but it's fun!

Re: convert Blocky to LUA

Posted: Monday 08 May 2017 14:28
by freijn
apologies yes == was 1 = to much.

quick copy and past error :-(


Thanks Manjh to assist here!

Re: convert Blocky to LUA

Posted: Thursday 11 May 2017 6:02
by poudenes
Grrr :( it worked. But does someone can tell me if i can use Scene/Group into a device changed? Tried it but don't work.
Have some scenes and when the scene is on, i also want update a user variable.

Re: convert Blocky to LUA

Posted: Thursday 11 May 2017 8:24
by freijn
Please post your code , so we can understand what you are looking for?

I have found this :

commandArray['Scene:Livingroom']='On' -- remember, Scene cannot be turned Off!

The comment at the end is a little 'disturbing' :-(

Re: convert Blocky to LUA

Posted: Thursday 11 May 2017 10:30
by poudenes
freijn wrote:Please post your code , so we can understand what you are looking for?

I have found this :

commandArray['Scene:Livingroom']='On' -- remember, Scene cannot be turned Off!

The comment at the end is a little 'disturbing' :-(
Turn on groep All-Off then user variable must goto 1. When turn off then user variable must goto 0

Code: Select all

commandArray = {}
if (Devicechanged["Group:All-Off"] == "On") then
   commandArray['Variable:All-Off"] = "1"
   
elseif (Devicechanged["Group:All-Off"] == "Off") then
    uservariables["All Off"] = "0"

    end
return commandArray
With a switch it is working. But want remove the switch and do some actions directly on the ON/OFF from a group

Re: convert Blocky to LUA

Posted: Thursday 11 May 2017 10:53
by freijn
Then let us know which action please? So we can try to suggest you.

The code you pasted , is from your memory or from your system? As I believe you have some errors in..

Re: convert Blocky to LUA

Posted: Thursday 11 May 2017 14:20
by poudenes
freijn wrote:Then let us know which action please? So we can try to suggest you.

The code you pasted , is from your memory or from your system? As I believe you have some errors in..
That is a LUA script inside Domoticz. And yes its not working. Thats why i ask if i can use a scene or group as trigger ... the IF action

Re: convert Blocky to LUA

Posted: Thursday 11 May 2017 15:34
by freijn
poudenes

I do not recognize the "Devicechaed" in your script.

According to my knowledge that is not a valid statement... or I am mistaken ?

Re: convert Blocky to LUA

Posted: Thursday 11 May 2017 15:51
by poudenes
freijn wrote:poudenes

I do not recognize the "Devicechaed" in your script.

According to my knowledge that is not a valid statement... or I am mistaken ?
sorry was a typo must be devicechanged but then its not working