wildcards in lua

Moderator: leecollings

Post Reply
hmjgriffon
Posts: 8
Joined: Friday 20 January 2017 21:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

wildcards in lua

Post 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?
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: wildcards in lua

Post 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.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest