Thanks for the suggestion! I didn’t know such an app existed. Right now, I’m using the “state” map object provided by ST to store bulb states (on/off status and dim level) before either dimming them or turning them off, then retrieving these states from that map when motion resumes. That’s the only way I can think of to persist such information between executions of a SmartApp, but perhaps there are better thought-out ways to store particular information.
For anyone who wants to know more about why I wrote this: my particular use case is that I have two Hue bulbs in in each of my bedrooms, only one of which I usually want on. They are in the same fixture controlled by the same physical switch. The default “Smart Lighting” app (or whatever it’s called) will turn on/off both of them with motion, but I usually prefer to only have one of them turned on at a time, though occasionally I’ll use both if I want extra light. If I do have both of them on, I want the behavior the stock app provides, but if I have one of them off I don’t want the second one to keep turning back on when motion is detected. I think I have this behavior covered with my SmartApp, which does:
- If no lights are on and motion is detected, then either (1) turn on only those lights that the app knows were on when motion was last inactive for a period of time long enough to dim or turn off the bulbs, or (2) turn on all lights if the app does not know the previous state of any lights (e.g., if you just installed it) or if all lights were off when motion was last inactive for long enough. (This last condition is designed to still turn on lights in the event that the lights were manually turned off by other means before the inactivity timeout was reached–I don’t think it’s desirable to restore all bulbs to "off’ when motion resumes.)
- If lights are on and motion has stayed inactive for long enough, then (save the on/off status of each light and) turn off all lights.
- Optionally, one minute before the above “off” threshold is reached, dim for 1 minute any lights that are on (after saving their previous dim level and on/off state for all lights so they can be restored when motion resumes)–this will attempt to dim them for 30 seconds instead if the user sets the “off” time to 1 minute of inactivity, though ST doesn’t guarantee accuracy here.
The last thing was just something I though would be nice to have–both to serve as a “warning” that the lights are about to turn off, and because Philips has new Hue motion sensors that do something similar, and I didn’t want to feel like I was missing out by not having them. It is optional and won’t happen if you don’t choose to dim those lights in the app. (I really don’t like that the lights have to be selected twice in the app’s preferences, but there doesn’t seem to be a way to ask the user to choose devices that support both “switch” (e.g., lights) and “switch level” (i.e., dimmable) capabilities, so they have to be chosen twice so I can access both of those properties. I guess this does provide the option of only dimming certain lights if you want to apply this feature to some but not all.)