Page 1 of 1

lua script On FOR 1 minute

Posted: Saturday 09 January 2016 0:16
by whiteduck
Hi all,

I am running Domoticz v2.3598 on Ubuntu and finding the lua scripting 'On FOR n' doesn't appear to work:

if (devicechanged['LWSwitch'] == 'On') then
commandArray['Lamp']='On FOR 1'
end

I wondered if this was seconds and On FOR 60 doesn't work either.
Plus I tried " instead of ' just in case it is fussy.

Any clues?

Thanks a lot
Ian

Re: lua script On FOR 1 minute

Posted: Saturday 09 January 2016 10:26
by jmleglise
Hi,

The wiki http://www.domoticz.com/wiki/LUA_commands said "FOR" is in minutes when the "AFTER" is in seconds.

a workaround may be to use "AFTER" ?

Code: Select all

commandArray[1]={['Relay1']='On'}
commandArray[2]={['Relay1']='Off AFTER 3'}

return commandArray

Re: lua script On FOR 1 minute

Posted: Saturday 09 January 2016 10:51
by jmleglise
On second thought, personally I won't be confident with the "FOR" instruction because I don't know How it was designed in Domoticz , (consider that for working, the "on FOR" must assume that Domoticz knows the inverse state of each type of device (open / close, on/off...) For example if you send 2x ON command. The second last state is ON or OFF ?)

So I will really use AFTER with a little more logic :

Code: Select all

if device changed then
if device value ==ON then send command OFF after XX seconds
(else  nothing to do because the device value == off)

Re: lua script On FOR 1 minute

Posted: Saturday 09 January 2016 23:28
by whiteduck
Thanks a lot - I tried Off AFTER 7 seconds and it ignored it. I haven't put the device current state logic in yet but tried this from a known state.

Code: Select all

if (devicechanged['LWSwitch'] == 'On') then
        commandArray['Lamp']='On'
        commandArray['Lamp']='Off AFTER 7'
end
Perhaps there is something wrong with the lua installation I have since I am running domoticz (from make file) on Ubuntu. The lua release is 5.2.2 as found in $HOME/domoticz/lua

Best regards and thanks again
Ian

Re: lua script On FOR 1 minute

Posted: Saturday 09 January 2016 23:48
by jvdz
It will ignore one as you are overriding the table entry "lamp" when you do it that way.It should look like this:

Code: Select all

if (devicechanged['LWSwitch'] == 'On') then
   commandArray[1]={['Lamp']='On'}
   commandArray[2]={['Lamp']='Off AFTER 7'}
end
Jos

Re: lua script On FOR 1 minute

Posted: Sunday 10 January 2016 22:06
by whiteduck
Thanks Jos.
I tried that and it's still not working - pointing towards an lua issue perhaps...

Re: lua script On FOR 1 minute

Posted: Sunday 10 January 2016 22:18
by nayr
There is more than one way to skin a cat, its more work but you can try a timer script as a work around.. I dont use the FOR and AFTER statements, I use timer scripts to cleanup after me, like so:

script_device_lwswitch.lua

Code: Select all

if (devicechanged['LWSwitch'] == 'On') then
   commandArray['Lamp']='On'
   commandArray['Variable:lamptriggered'] = tostring(1)
end
script_time_lamp.lua

Code: Select all

function timedifference (s)
        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)
        t1 = os.time()
        t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
        difference = os.difftime (t1, t2)
        return difference
end

commandArray = {}
if (otherdevices['Lamp'] == 'On') and (timedifference(otherdevices_lastupdate['Lamp']) >= 420) and (uservariables['lamptriggered'] == 1) then
        commandArray['Lamp']='Off'
        commandArray['Variable:lamptriggered'] = tostring(0)
        print("Lamp was triggered by an event more than 7mins ago, time to turn it off.")
end
return commandArray
All my lighting has scripts like this, but with longer timeouts and without the user-variable logic.. just so people dont leave lights on since I have not got around to installing motion sensors yet. (closets/laundry rooms get like 10mins, bedrooms an hour, kitchen and work areas get several hours)

Re: lua script On FOR 1 minute

Posted: Sunday 10 January 2016 22:43
by jvdz
whiteduck wrote:Thanks Jos.
I tried that and it's still not working - pointing towards an lua issue perhaps...
Any more details on the "not working" bit? :)
Do you see anything in the domoticz log?
Maybe add some print() statements in the lua script to see if it gets fired and inside the "if" to see if it is true.
The output should also show up in the log.

Jos

Re: lua script On FOR 1 minute

Posted: Sunday 10 January 2016 23:35
by whiteduck
Nayr - many thanks - nice alternative solution!

Jos - yeah sorry I was a bit vague - I added a print inside the if then but it doesn't get into the log file however the command is firing as seen in the log and by the fact the light comes on - it just doesn't go off after 30, 7 or any time combination ;)

Code: Select all

2016-01-10 22:32:18.483  EventSystem: Script event triggered: /root/domoticz/scripts/lua/script_device_lwrf2.lua
2016-01-10 22:32:18.478  (RFXCOM) Lighting 5 (LWSwitch2)
2016-01-10 22:32:19.161  OpenZWave: Domoticz has send a Switch command! NodeID: 10 (0x0a)

Code: Select all

if (devicechanged['LWSwitch'] == 'On') then
print('Lamp On')
   commandArray[1]={['Lamp']='On'}
   commandArray[2]={['Lamp']='Off AFTER 30'}
end

Re: lua script On FOR 1 minute

Posted: Monday 11 January 2016 8:56
by jvdz
Did you put the script in the On or Off action field of the switch in stead of storing it in domoticz/scripts/lua and naming it script_device_abc.lua?

Jos

Re: lua script On FOR 1 minute

Posted: Tuesday 12 January 2016 20:52
by gertlind1
I tested this with a motion sensor.

Code: Select all

commandArray = {}
if (devicechanged['Rörelse'] == 'On' and otherdevices['Hörnbord'] == 'Off') then
        commandArray['Hörnbord']='On FOR 1'
end
return commandArray
Works perfectly for me, light goes off after 1 minute.
Im on version V2.4123r

Re: lua script On FOR 1 minute

Posted: Saturday 16 January 2016 0:21
by whiteduck
Thanks Gert

Jos - no I have it in the lua directory… could this be the problem?

Re: lua script On FOR 1 minute

Posted: Saturday 16 January 2016 0:27
by nayr
yes, it has to be in the scripts/lua folder with a specific filename to be called, if its not, ~/domoticz/scripts/lua/script_device_*.lua then its never going to get executed.

the lua folder @ ~/domoticz/lua is a source path, not a script path.

Re: lua script On FOR 1 minute

Posted: Saturday 16 January 2016 0:32
by whiteduck
Thanks Nayr - I was replying to Jos asking:

Did you put the script in the On or Off action field of the switch in stead of storing it in domoticz/scripts/lua and naming it script_device_abc.lua?

It's getting executed - the issue is the FOR command doesn't appear to work

Thanks

Re: lua script On FOR 1 minute

Posted: Saturday 16 January 2016 18:03
by whiteduck
Everyone,

I realised I was being a bit of a muppet - really sorry - I had two very similar lua scripts and two similar LWRF switches - I was changing the wrong script!!! Thanks a lot for your help… now I am off to hide for a while.

Re: lua script On FOR 1 minute

Posted: Saturday 16 January 2016 21:16
by nayr
Plebkac is the number one cause of issues here it seems :D

Glad you got it figured out, editing a file you think is another file can become quite maddening.. I feel for yeh.

Re: lua script On FOR 1 minute

Posted: Saturday 16 January 2016 21:24
by whiteduck
Time to merge the scripts since they control the same lamp!
Sorry to waste peoples time!

Re: lua script On FOR 1 minute

Posted: Sunday 01 August 2021 23:44
by Welsyntoffie
whiteduck wrote: Saturday 16 January 2016 21:24 Time to merge the scripts since they control the same lamp!
Sorry to waste peoples time!
My time was not wasted reading this post. This code snippet helped me to solve my problem.

Code: Select all

   commandArray[1]={['Lamp']='On'}
   commandArray[2]={['Lamp']='Off AFTER 30'}