Page 1 of 1

Lua Scripting - Combining And/Or's

Posted: Wednesday 25 May 2016 12:41
by jimtrout87
I'm quite new to Domoticz and have just started using Lua. I'm not sure if possible but is there a simple way to combine and statements.

Below is my script for my alarm sequence but I seem to be saying and this and this and this.... is this the only way or could this be cleaned up. I have tried a few options to no avail so far. The below does work ok but im sure It could be compacted. As im just starting it would be good to get into the correct habits now!

Code: Select all

-- script_Alarm.lua

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['Kitchen Motion'] == 'On' or otherdevices['Hallway Motion']  == 'On') and otherdevices['Jims Home'] == 'Off' and otherdevices['Kaths Home'] == 'Off' and otherdevices['Alarm'] == 'Off' and timedifference(otherdevices_lastupdate['Jims Home']) > 600 and timedifference(otherdevices_lastupdate['Kaths Home']) > 600) then
	print("Nobody Home and Motion dector Active, Rasing Alarm!")
    commandArray['Alarm'] = 'On FOR 10'
end

Re: Lua Scripting - Combining And/Or's

Posted: Wednesday 25 May 2016 20:15
by georgesattali
As for myself, there is only one optimisation you may do :
In

Code: Select all

    print("Nobody Home and Motion dector Active, Rasing Alarm!")
you could write "Raising Alarm" :lol:

the rest seems perfect to me.

See you,
GD.

Re: Lua Scripting - Combining And/Or's

Posted: Thursday 26 May 2016 11:27
by jimtrout87
haha thanks - that's one way of dropping some text.

Thanks for clearing that up.I don't know enough about Lua but assumed you would be able to combine For example:

if(otherdevices(['Kitchen Motion'] or ['Hallway Motion']) =='On')

To me that seems logical but maybe not...

Re: Lua Scripting - Combining And/Or's

Posted: Thursday 26 May 2016 11:42
by robpow
Try:

if(otherdevices['Kitchen Motion'] =='On' or otherdevices['Hallway Motion'] =='On')

Re: Lua Scripting - Combining And/Or's

Posted: Thursday 26 May 2016 13:42
by markk
jimtrout87 wrote:I'm quite new to Domoticz and have just started using Lua. I'm not sure if possible but is there a simple way to combine and statements.

Below is my script for my alarm sequence but I seem to be saying and this and this and this.... is this the only way or could this be cleaned up. I have tried a few options to no avail so far. The below does work ok but im sure It could be compacted. As im just starting it would be good to get into the correct habits now!

Code: Select all

-- script_Alarm.lua

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['Kitchen Motion'] == 'On' or otherdevices['Hallway Motion']  == 'On') and otherdevices['Jims Home'] == 'Off' and otherdevices['Kaths Home'] == 'Off' and otherdevices['Alarm'] == 'Off' and timedifference(otherdevices_lastupdate['Jims Home']) > 600 and timedifference(otherdevices_lastupdate['Kaths Home']) > 600) then
	print("Nobody Home and Motion dector Active, Rasing Alarm!")
    commandArray['Alarm'] = 'On FOR 10'
end
I use a seperate dummy switch called "someone home" operated by a blockly like this:
Blocky event: IF "me home" AND "wife home" = OFF, THEN turn "someone home" = Off, ELSE IF "me home" OR wife home" = ON, THEN turn "someone home" = On.

This would reduce your script a little bit by just using "someone home" and also reduce any others you use in the future based on last person to leave/first person home. Lights off, appliances off etc etc.

Re: Lua Scripting - Combining And/Or's

Posted: Thursday 26 May 2016 21:15
by georgesattali
good idea :
you could write :

Code: Select all

motionDetected = (otherdevices['Kitchen Motion'] == 'On' or otherdevices['Hallway Motion']  == 'On') 
nobodyshome    = (otherdevices['Jims Home'] == 'Off' and otherdevices['Kaths Home'] == 'Off')
if ( motionDetected and  nobodyshome 
     and otherdevices['Alarm'] == 'Off'
     and timedifference(otherdevices_lastupdate['Jims Home']) > 600
     and timedifference(otherdevices_lastupdate['Kaths Home']) > 600) 
then
...
end
the combination if(otherdevices(['Kitchen Motion'] or ['Hallway Motion']) =='On') would be - if accepted - VERY oddly interpreted by lua -- well, it is not.

Re: Lua Scripting - Combining And/Or's

Posted: Tuesday 26 July 2016 17:53
by jimtrout87
Thanks Both, i do now have a Somonehome dummy switch and is done via blocky. I use it across many scripts so its easiest to leave where it is.