Problems with motion sensor smart app


#1

(@garyd9, I thought it best to take this out of the Fibaro thread in order not to get too far off topic.)

Here is my issue:

I have several (4) Fibaro motion sensors and one Aeotec motion sensor. I would like to be able to have the motion sensors trigger certain dimmer switches with motion and turn off after a set period of time after no more motion is detected.

Here is the code I am using:

[code]
definition(
name: “Motion Sensor Dimmer On/Off”,
namespace: “”,
author: “”,
description: “Turn on some lights with motion if not already on”,
category: “My Apps”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”,
iconX3Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)

preferences {
section(“Select the motion sensor…”) {
input “motion1”, “capability.motionSensor”, title: “Which?”, required: true, multiple: false
}

section("Select the switches to control...") {
	input "switches", "capability.switchLevel", title: "Which?", required: true, multiple: true
}

section("Select the dimmer level...") {
	input "level", "number", title: "Level?", required: true
}

section("Select the number of minutes after to turn off") {
	input "minutes", "number", title: "Minutes?", required: true
}

}

def installed() {
// log.debug “Installed with settings: ${settings}”

initialize()

}

def updated() {
// log.debug “Updated with settings: ${settings}”

unsubscribe()
initialize()

}

def initialize() {
subscribe(motion1, “motion.active”, motionOnHandler)
subscribe(motion1, “motion.inactive”, motionOffHandler)
}

def motionOnHandler(evt) {
unschedule(switchesOff)
state.switchOff = true
for(mySwitch in switches) state.switchOff = state.switchOff && (mySwitch.currentValue(“switch”) == “off”)
if(state.switchOff) switches.setLevel(level)
}

def motionOffHandler(evt) {
if(state.switchOff) {if(minutes>0) runIn(minutes*60,switchesOff) else switches.off()}
}

def switchesOff() {
switches.off()
} [/code]

Problems:

  1. Continued motion did not appear to reset the timer for the light to turn off. For example, I would be in my kitchen, and as intended, the kitchen lights would turn on. I set the app to turn the kitchen lights off 10 minutes after motion stopped. What would frequently happen is that 10 minutes after the light turned on, the light would turn off, even rgough I was still in the room moving around @garyd9 suggested adding “unschedule(switchesOff)” which was done.

  2. Now however, the lights will not turn off and remain on once triggered.

Possible complicating factors:

  1. Multiple sensors triggering a single switch. For example, I have a sensor above both doors that lead into a hallway. When motion is detected, the light in the hall is to turn on for two minutes, then turn off. Not sure if having two apps that control the same switch makes a difference, but I am just throwing that out there. It’d be nice if I could ID multiple sensors i.e., if motion detected on hallway sensor A or hallway sensor B, turn on hall light at X percent until 2 minutes after no motion on sensor A or sensor B is detected. I’d also LOVE it if the app would allow a bypass of the dimmer setting to 100% if a switch is manually pushed. For example, turn on to 1% if motion, but if switch is pushed, turn on to 100%.

2.???

I have NO programming experience, so I’m am relying on simple logic to troubleshoot, and the helpfulness of forum members. I’m also not beholden to that app I am using but did struggle to find another that would let me set light values to 1% and not just 10%, 20% etc.


(Gary D) #2

For some reason, that line is driving me nuts. The previous line sets state.switchOff to true… and this line just doesn’t do anything. What am I missing?

@wackware, @tgauchat, @Mike_Maxwell - can any of you figure out what this line is supposed to be doing?

argh… and “state.switchOff” is never set to false… anywhere… so why bother checking it? This is insane…

@potus, if you can wait until sometime this weekend, I’ll just re-write this for you.


#3

@garyd9, you rock. Thanks for the offer of help.


(Gary D) #4

yeah, that’ll make things more interesting… heh…


(Aaron) #5

I’ll be watching this. I have been thinking about how to handle multiple motion detectors also. I think something like this could work but I’m not sure how to code it:

subscribe to motion.active - that handler turns on the light, unschedules the turnOff() handler (in case it had been scheduled)

subscribe to motion.inactive - that handler first checks all the motion detectors, if all are inactive, the schedule the turnOff() handler. This insures the last motion detector to send a motion.inactive event triggers the turnOff() handler.

. . …Watching


(Gary D) #6

I don’t think it’d work well unless there was a single smartapp handling everything. Otherwise, different apps are going to be “competing” with each other.

So, the app would have to first prompt for all the motion sensors that matter anywhere. Then (dynamic pages) FOR EACH of those motion sensors, which switches should associate with that sensor.

Then the smartapp would have all the data it needs.

This is going to be a pain to test - I only have a single motion sensor, and the IDE virtual thing doesn’t always work well with dynamic pages… (I’ll just use virtual switches instead of motion sensors.)


(Bruce) #7

I think you may be making this harder than it needs to be. Some ideas for you to think about:

  1. Instead of building the “turn off when no motion” logic into your app, just use a separate Lights & Switches shortcut to turn things off after x minutes. You select the dimmers and motions for that, which can be multiples.

  2. It doesn’t matter that multiple apps refer to the same physical devices so long as they don’t contradict each other in some way.

  3. It’s ok to have multiple motion sensors trigger some “turn on” action, and it’s not necessary to distinguish which one did it in the app. However, expect that the method of the app that is subscribed to the motions will probably run more than once. If you want to distinguish which motion did it in the app for some reason, just have more than one handler and more than one subscription. OR, use more than one instance of the app.

Disclaimer: I haven’t studied your code. I’m offering these ideas simply because they have worked well for me for use cases like yours.


(Aaron) #8

Yea, sorry. I incorrectly made the assumption that the scheme could be arranged so that all the motion detectors that want to drive a given light could be in one SmartApp. Could you have a SmartApp instance for each light or light group? Maybe this is along the lines of what @bravenel is suggesting.


(Gary D) #9

I don’t know about the built-in apps. While they have worked well for others, I usually don’t have such good luck. (Probably because I’m so picky.)

I’m guessing that the OP wants the flexibility to have multiple motions sensors intermingled with multiple lights. So, for sensors A,B, and C - and lights X,Y,Z…

A might control X and Y
B might control Y and Z
C might control X and Z

Or, if thought of from the light’s point of view:

X is controlled by A and C
Y is controlled by A and B
Z is controlled by B and C

Like I said… interesting. :slight_smile:


(Bruce) #10

If that’s it, that’s easy with Lights & Switches. That doesn’t take any custom code at all!

First shortcut controls X and Y, turned on with motion A
Second shortcut controls Y and Z, turned on with motion B
Third shortcut controls X and Z, turned on with motion C


(Bruce) #11

These are all just super simple apps. They do what they do. People get confused and think they are magic or something, like that once you have a shortcut called “Dining Room” that refers to a dimmer in the Dining Room, that you can’t do anything more with the dining room dimmer. Not true. You could have another shortcut called “Dining Room Evening”, that also controls the same dimmer, but does something completely different. Some people say silly things like having multiple shortcuts clutters up the mobile app. ? These are the basic building blocks of ST.

My experience with ST is that less code is better than more code, and more use of built-in apps is better than less. Custom code is for when built-in apps don’t provide the functionality required.


(Gary D) #12

I don’t think that would work… I suspect that the shortcut for motion A will turn off X even if there’s also motion in C.

So, a shortcut called “XY” for lights X and Y, turned on for motion A and off for motion A with 5 min timeout
Another shortcut called “XZ” for lights X and Z, turned on and off by motion C (again, 5 min timeout)

So, a person walks from an area with motion A (A turns on XY lights) to the area with motion C (which turns on XZ lights) Sounds great so far. Then 5 minutes passes and XY realizes it hasn’t had motion for 5 minutes, so it turns off XY. That’s fine for Y, but not for X (as there’s still motion in C)

Disclaimer: I haven’t actually tried this (and don’t have multiple motion sensors.) However, most of the built-in smartapps tend to be somewhat simple like this.


(Bruce) #13

No, you’re mixed up.

You do the three shortcuts like I said, only using “Turn on when there is motion”, and not using the sub-option of “turn off after x minutes” in any of those shortcuts. All they do is turn lights on.

Then there is a fourth shortcut that does “Turn off when there is no motion”, and that references X, Y, and Z, and motions A,B and C.

So any motion sensor turns on its subset of lights. All lights turn off x minutes after there is no motion in the room.

I use this all the time – works great.


(Gary D) #14

… then, again start from my example… and walking from A to C… what will turn off Y? It’ll end up staying on as long as there’s motion in any of A,B, or C…

Actually, I might be discussing a moot point. @potus, will the set up that @bravenel is describing work for you? If so, go for it. :smile:


(Bruce) #15

Back to what I said originally, OP is making this way harder than it needs to be. I can’t tell what the actual use case is from what’s been written. But, I can tell you for certain that these sorts of use cases can usually be handled with shortcuts from Lights & Switches instead of custom apps. Custom apps don’t always work as expected, and things involving time and multiple event sources get hairy. But the Lights & Switches apps have the logic that’s been described, namely multiple motion sensors turning on different lights, and complex or possibly disjoint turn-off logic.

A common thing I do is have a motion sensor turn on a rooms’ main lights, that sensor at the entry to the room. But the shape of the room means I need other sensors to reveal that I’m still in the room. Those other sensors are not involved in turning on the original lights that came on, but some other lights in the room, for example a sink vanity light. Eventually, all of the lights turn off when there is no motion from any of the sensors, but those vanity lights turn off when that particular sensor is quiet. All of this can be done with Lights & Switches shortcuts.


#16

FWIW, it’s very common in a zone-based sensornet that the “Off” condition is processed exactly as @bravenel describes, with a separate rule that does nothing but check for inactivity of all sensors in the zone.

One of the weaknesses of the current mobile app is that the shortcuts functionality is so hidden.

It’s easy to get to once you know it’s there, but if you’re just noodling around trying to set up a typical new customer use case. you may never find it.

I hope an eventual official rules engine will give us a better view of groups and how to use them.


#17

@bravenel: I’m not sure how well the separate turn off after x minutes app would work for me. Seems to me the light would just turn off after x minutes because the app would not reset the timer based on continued motion sensed by the appropriate sensor. For example, in the laundry room, which connects my garage and main house, I have a sensor in the garage above the laundry room door to control the laundry room light. On the other side of the door, I have another sensor (inside the laundry room) to trigger the laundry room light when I come into the laundry room from the main house (through the other door) and also detect motion when I am in the laundry room doing laundry. I want the light in the laundry room to turn off 2 minutes after no motion is detected, which means the timer should reset based on continued motion being detected by one (or more) sensors.


#18

Hmmm. The problem right off the bat with the built in Smartthings app is that I can’t set dimmers to below 10%.

What you are saying, if I understand, is that I should separate the on and off commands into separate apps AND that a single switch can be turned off by polling the multiple sensors?


(Bruce) #19

You’re getting closer. Yes, you can specify multiple motion sensors to control when a light goes off, and by separating the turn-on logic from the turn-off logic, the motions involved can be different for the two. That’s a big part of what you want to do, that I think you missed at first. Let ST do the hard work of knowing when it’s x minutes after the last relevant motion went quiet.

Ah, the nasty 10%. I don’t know why ST doesn’t just fix these apps by replacing the enumeration of multiples of 10% with a simple number to dim to. That’s one of the first things I did, was fix the dimLevel choices.

Once you’ve separated turn-on from turn-off, it’s easy to have your own turn-on app that allows any dimLevel. Select some dimmers, select and subscribe to some motions, select a dimLevel, and there you go.


#20

So, any recommendations on what turn on app to use?