Page 1 of 1

wildcards in lua

Posted: Sunday 05 March 2017 18:08
by hmjgriffon
I've got a script for door sensors, I'd like to have one block handle all of the door sensors using wildcards since the naming convention is the same, example:

commandArray = {}
if (devicechanged['BackDoorSwitch (Sensor)'] == 'Open' and otherdevices['Security Switch'] == 'Armed') then
commandArray['SendNotification']='Security Alert! Back Door has been opened!#2'

elseif (devicechanged['BackDoorSwitch (Sensor)'] == 'Open' and otherdevices['Security Switch'] == 'Armed Home') then
commandArray['SendNotification']='Security Alert! Back Door has been opened!#2'


end
return commandArray

can I modify this to look for all sensors named *DoorSwitch and then send an alert with the name of whatever door or is tripped?

Re: wildcards in lua

Posted: Sunday 05 March 2017 20:53
by simonrg
hmjgriffon wrote:I've got a script for door sensors, I'd like to have one block handle all of the door sensors using wildcards since the naming convention is the same, example:

Code: Select all

commandArray = {}
if (devicechanged['BackDoorSwitch (Sensor)'] == 'Open' and otherdevices['Security Switch'] == 'Armed') then
        commandArray['SendNotification']='Security Alert! Back Door has been opened!#2'
elseif (devicechanged['BackDoorSwitch (Sensor)'] == 'Open' and otherdevices['Security Switch'] == 'Armed Home') then
        commandArray['SendNotification']='Security Alert! Back Door has been opened!#2'
end
return commandArray
can I modify this to look for all sensors named *DoorSwitch and then send an alert with the name of whatever door or is tripped?
If your DoorSwitch are called, BackDoorSwitch, FrontDoorSwitch, ... etc. then this will check each device changed and if the last 10 letters are DoorSwitch (same spelling same cases) then it will do the standard logic:

Code: Select all

commandArray = {}
tc=next(devicechanged)
v=tostring(tc)
if v:sub(-10) == 'DoorSwitch' then
   doorname = v:sub(1,-11)
   if (devicechanged[v] == 'Open' and otherdevices['Security Switch'] == 'Armed') then
       commandArray['SendNotification']='Security Alert! '..doorname..' Door has been opened!#2'
   elseif (devicechanged[v] == 'Open' and otherdevices['Security Switch'] == 'Armed Home') then
      commandArray['SendNotification']='Security Alert! '..doorname..' Door has been opened!#2'
   end
end
return commandArray
N.B. In your code example the names seem to be ...DoorSwitch (Sensor), if this is really the case then you will need to change the code to take account of this.