How to check multiple state of multiple open/close sensors


(Chrisb) #1

So I’m looking at creating a program that will check some unknown quantity of open/close sensors and if any of those sensors is open, then we’re going to do a specific task… turn on a switch.

The user is allowed to enter any number of sensors that he or she wants to. Now if I’m just checking one I can do that with an If - Then statement, but how do you do it for some unknown multiple sensors?


(Chrisb) #2

So I ended up borrowing this code from another app:

def checkDoors() {
	def open = doors.findAll { it?.latestValue("contact") == "open" }
    if(open) {
    	doorCheck.on()
    }
}

The good news: It works. The bad news… I have no idea how it works. Can anyone explain to me what’s going on in the “def open…” line?


(Todd Wackford) #3

I’d have to see the rest of the code. Is doors a list of doors that were enumerated by the user selecting the devices at install?


(Chrisb) #4

Yeah, doors = the user selected open/close sensors.

doorCheck = a virtual on/off tile (it’s used to signal an actions by Ubi).


(Todd Wackford) #5

Its an iteration through the collection of doors, looking for an instance of “it” (each door) with the attribute of contact which has the state of “open”. Open is the collection of what was found.

Hope This Helps?
Twack


(Chrisb) #6

Okay… I think I’m kinda understanding. Looking at the bit of code again:
def open = doors.findAll { it?.latestValue(“contact”) == “open” }
My set that I’m working with is doors, which is my collection of open/close sensors.

When I do .findall it’s more or less saying: On this set of values (doors), I want to find any that fit the parameter that I’m about to indicate.

Next I’ll specific the parameter in the {}. ‘it’ is a pronoun that takes the place of my set, doors. The ? is uses because doors can contain multiple items, so it’s it1, it2, it3… until I complete looking through the set. Then of course we’re looking for the .latestValue of the parameter (“contact”). I’m asking for a positive returns on those instances where that value is equal to “open”.

Am I good so far? (I hope!)

Now’s the part where I’m not sure if I’m getting it. What does the variable open (from the beginning of the equation) equal now? I think maybe it is now a subset of doors, right? Specifically, it’s the collection of doors who have a latest value of “open” as for contact.

For example, when installing the program the user specifies frontDoor, sideDoor, and backDoor as the doors this program should look at. So the variable ‘doors’ = frontDoor, sideDoor, backDoor. Now the back door and side door are open when we run the bit of code above. So after this it should populate the variable ‘open’ with: “sideDoor, backDoor”.

Does that sound right? If so, then I’m sort stuck on this last part. The conditional I’m using is this:
if(open) { [insert action here] }

So what is this conditional asking? If ??what?? then do the action… What does ‘if(open)’ test? Is it merely asking if open exists? That is… If open equals something… anything… then proceed with action? So in my example above because open is populated with sideDoor and backDoor then we run the action, but if no doors were open, then the variable open = null, and if it equal null, then we don’t run the action?


(Todd Wackford) #7

If no door is chosen or does not meet the criteria, then open = null or false.
If a door is chosen and meets the criteria, then open is not null or is considered to be true because it has value.

Make sense?

Twack


(Chrisb) #8

Yup, that makes sense. I think I got it. Like I said above, it works… I just like to know why it works.

Thanks twack.


(Dave Vander Wekke) #9

I’m guessing the same process could be used for getting temperatures from all of the SmartSense Multi Sensor. I’m looking to write an app to show all of the temperatures throughout the house.