Page 1 of 1

Question about LUA and Help to build Lua script

Posted: Thursday 08 February 2018 21:08
by antipiot
Hello everybody!

I Bough an Aeotec Wallmote with 4 buttons and wanted to get a bit more of it than the "Toggle" i can use with the blocky events settings:

Here's the thing:

I use my wallmote to control a RGBW Zipatobulb 2
I actually have a blocky event Toggling on or off the Zbulb2.
The wallmote only send ON commands, so i had to set the command set to toggle so it doesn't use 2 buttons (not that i acually need the four for differents purpose but...)

Things is:
If i set another intensity or color, if i toggle Off and On again, it'll remain on the same color/brighness previously set. Absolutly normal but:

as i've quicky reached the limitations of blocky in this case, i've search a bit about lua with no luck for my questions:

I was asking myself, as i have no clue of what are the possibilities with LUA (or anything script related tho)
if we can have something like:

if RGBW is ON and one one command is received in one second on button 1 set RGBW to OFF
if RGBW is OFF and one on command is received in one second on button 1 set RGBW to white and 100 brightness.
if RGBW is OFF and two on commands are received in one second on button 1 set RGBW to white and 50 brightness

May something like this possible to get working using lua?

Is there another way to have something like this working?

I hope my description is clear enough :-)

Looking forward for your suggestions!
Thanks for reading:-)

Re: Question about LUA and Help to build Lua script

Posted: Sunday 11 February 2018 10:36
by mrf68
Hi,

Let me share my thoughts on this, just to feed yours. ;)

I suppose Domoticz can check if RGBW is On of Off, so doing something when it is On is not that hard to configure. In my opinion the hard part is if the trigger is only triggered once within a specific period of time. So your first IF statement of you questions is not the problem

If you want to start an action after pressing a button more than once, the system needs to know the time of the last update (Domoticz knows that) but also the time of the previous update. I don't know if Domoticz keeps track of that itself. I use a script that stores the time of the last update in a uservariable. Keep in mind that if you need this info to calculate the difference between last and previous update, you need to store the time of the last update into the variable after calculating the difference. Whenever this difference is more than one second, you can assume that the button is pressed once. Also, Domoticz needs to wait for more than one second before initiating the action, because you need the time to be able to press it again and the system needs to check the difference again. I think this way is not that easy to implement.

Another method could be to increase a variable by 1 and have the system check the value after a period of time and based on this value execute the action. You could increase it every time you push the button and set the variable to 0 after the time window you need to update it more than once. In theory it could be something like this (this cannot be copy/pasted, it's just a schematic view of my thoughts):

If button pressed, start action after 5 seconds
set variable = variable + 1

If time difference between last and previous update of button pressed is more than 5 seconds then
read variable
if variable is 1 then do this
if variable is 2 then do that
etc.
set variable to 0

Re: Question about LUA and Help to build Lua script

Posted: Monday 12 February 2018 7:39
by antipiot
Tanks for your advice,

I'll try to do something following your idea, in the meanwhile, if sombody already have something like this, i'd like to see how it looks :-)

Thanks!

Re: Question about LUA and Help to build Lua script

Posted: Monday 12 February 2018 8:11
by ben53252642
I can write the script for you in a few hours.

Please provide:

1) Name of the Wallmote virtual switch in Domoticz for the button you are using
2) Name of the devices to be controlled

I have the same Wallmote and wrote the guide here on how it works: viewtopic.php?f=24&t=16372&start=20#p159812

Hopefully this will help you learn how to use Lua.

Re: Question about LUA and Help to build Lua script

Posted: Monday 12 February 2018 8:57
by antipiot
ben53252642 wrote: Monday 12 February 2018 8:11 I can write the script for you in a few hours.

Please provide:

1) Name of the Wallmote virtual switch in Domoticz for the button you are using
2) Name of the devices to be controlled

I have the same Wallmote and wrote the guide here on how it works: viewtopic.php?f=24&t=16372&start=20#p159812

Hopefully this will help you learn how to use Lua.
Thanks a lot!

Here's the informations you need:
Wallmote name is "$WM1-1"
device name to be controlled: "Lum. Bureau"

I'll take a looke to your guide! Thanks for this :-)

Re: Question about LUA and Help to build Lua script

Posted: Monday 12 February 2018 10:24
by ben53252642
This should do your first two requirements, note that the third "dim brightness if pressed two times" is quite complicated to achieve as it would conflict with your second requirement.

This script will turn on the device if its turned off and the button is pressed and vice versa.

Please change the name of "$WM1-1" to "WM1-1", you should never use a $ sign in scripting where possible because it can be mistaken as a variable.

Code: Select all

-- Device Last Updates
t1 = os.time()
    devices = {
        "WM1-1",
        "Lum. Bureau"
        }
    numdevices = 0 -- Count number of devices in the array
    for index in pairs(devices) do
        numdevices = numdevices + 1
    end
    for i = 1, numdevices do
    s = otherdevices_lastupdate[devices[i]]
    year = string.sub(s, 1, 4)
    month = string.sub(s, 6, 7)
    day = string.sub(s, 9, 10)
    hour = string.sub(s, 12, 13)
    minutes = string.sub(s, 15, 16)
    seconds = string.sub(s, 18, 19)
    t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
    str = (devices[i] .. "LastUpdate")
    str = str:gsub("%s+", "")
    str = string.gsub(str, "%s+", "")
    _G[str] = (os.difftime (t1, t2))
    end

commandArray = {}

-- Turn device on if its turned off
if (devicechanged["WM1-1"] == 'On' and otherdevices['Lum. Bureau'] == 'Off' and Lum.BureauLastUpdate > 2) then
commandArray['Lum. Bureau'] = 'On'
-- Turn device off if its turned on
elseif (devicechanged["WM1-1"] == 'On' and otherdevices['Lum. Bureau'] == 'On' and Lum.BureauLastUpdate > 2) then
commandArray['Lum. Bureau'] = 'Off'
end

return commandArray
Note that the script will only allow one button press per two seconds. This is because the Wallmote can sometimes send multiple on signals very fast when you press the button and obviously this would cause chaos within Domoticz.

It should be added in Domoticz events as a Lua Device script (not "All").

Give it a try and let me know. Cheers 8-)

Re: Question about LUA and Help to build Lua script

Posted: Monday 12 February 2018 10:37
by antipiot
Many thanks for this! 8-)

I'll try and learn from it.

Thing is:
I can already toggle the light using blocky events.

The interesting part for me was to be able to set anything like brighness or color to something :oops:
Is there a way in LUA to set brightness or color for a device instead of "on" or "off"?

Re: Question about LUA and Help to build Lua script

Posted: Monday 12 February 2018 10:43
by ben53252642
Not sure about the color but you can set brightness like this:

Code: Select all

commandArray["Lum. Bureau"]="Set Level 100"
If you look at the script above you would just replace the commandArray on / off sections with the above line and the brightness level you want.

Here is a very very simple complete Lua script to demonstrate:

Code: Select all

commandArray = {}
if (devicechanged['SWITCH NAME'] == 'On') then
    commandArray["Lum. Bureau"]="Set Level 100"
end
return commandArray

Re: Question about LUA and Help to build Lua script

Posted: Monday 12 February 2018 11:16
by antipiot
ben53252642 wrote: Monday 12 February 2018 10:43 Not sure about the color but you can set brightness like this:

Code: Select all

commandArray["Lum. Bureau"]="Set Level 100"
If you look at the script above you would just replace the commandArray on / off sections with the above line and the brightness level you want.

Here is a very very simple complete Lua script to demonstrate:

Code: Select all

commandArray = {}
if (devicechanged['SWITCH NAME'] == 'On') then
    commandArray["Lum. Bureau"]="Set Level 100"
end
return commandArray
great!
Thanks for all thoses informations and all your help!
can't wait to try :-)

Re: Question about LUA and Help to build Lua script

Posted: Monday 12 February 2018 18:20
by antipiot
Hello :-)

Tried the script and i got an error in log:

2018-02-12 18:16:32.283 Error: EventSystem: in Wallmote: [string "-- Device Last Updates..."]:13: bad argument #1 to 'sub' (string expected, got nil)