Lua Scripting - Combining And/Or's

Moderator: leecollings

Post Reply
jimtrout87
Posts: 16
Joined: Tuesday 12 April 2016 17:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Lua Scripting - Combining And/Or's

Post 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
georgesattali
Posts: 84
Joined: Saturday 05 March 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Lua Scripting - Combining And/Or's

Post 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.
jimtrout87
Posts: 16
Joined: Tuesday 12 April 2016 17:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua Scripting - Combining And/Or's

Post 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...
User avatar
robpow
Posts: 38
Joined: Friday 19 July 2013 19:28
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta :)
Location: .se
Contact:

Re: Lua Scripting - Combining And/Or's

Post by robpow »

Try:

if(otherdevices['Kitchen Motion'] =='On' or otherdevices['Hallway Motion'] =='On')
Last edited by robpow on Thursday 26 May 2016 13:44, edited 1 time in total.
markk
Posts: 267
Joined: Tuesday 14 January 2014 14:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua Scripting - Combining And/Or's

Post 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.
Running Domoticz on Pi3 with RFXtrx433e. LWRF power sockets and dimmer switches. Integrated my existing wirefree alarm PIRs and door contacts with domoticz. Geofencing with Pilot. Harmony Hub. Tado for Heating. Now playing with mysensors.
georgesattali
Posts: 84
Joined: Saturday 05 March 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Lua Scripting - Combining And/Or's

Post 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.
jimtrout87
Posts: 16
Joined: Tuesday 12 April 2016 17:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua Scripting - Combining And/Or's

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest