Motion activated light only if light is already off?

I am starting out with SmartThings and trying to get the motion activated lights to only turn on/off if the lights are off to begin with. I.e. if I turned them on manually, I do not want them to auto-turn off when no-motion timeout hits. Is there a simple way of doing this?

It seems that the underlying thing I am trying to understand is if there are something less global than “modes” - something that can allow multiple modes to co-exist at same time? Alternatively, Apps can be conditional on modes, but can they be conditional on state of other switches/devices?

Thanks,

-M

Welcome! The short answer is it’s frequently asked for and, although not in the stock app, fortunately can be accomplished with a small amount of custom code. Bravenl, for example, has written 4 or 5 variations on this for different use cases to help different people. See, for example:

Or this:

If you have any questions on either of those, or need something a little different, just ask in those topics and someone will help you.

2 Likes

JD gives some great references; so take a look for sure.

Meanwhile, though, you have the right idea: We’ve mentioned to SmartThings that just one location.mode is rather limiting or cumbersome (i.e., you’d have to create a lot of duplicate modes, like “Home-Day”, “Home-Night”, “Away-Day”, “Away-Night”, etc., etc., etc. in order to cover all the possible scenarios). On top of that, most SmartApps are set to run in one and only one mode (or in all modes), so not much flexibility there.

The most common solution that is “mode-like” is to use Virtual Switch Devices. These are just like real switch “Things” in that they can be on or off, and they can be referenced by multiple SmartApps and Hello Home Actions; but since they are not real switches, you can create as many as needed to coordinate activity between SmartApps.

One SmartApp, for example, can get a Virtual Switch to “on” in order to indicate that another SmartApp should ignore a motion sensor for on/off until that Virtual Switch is set back to “off”.

It’s less complicated than it sounds … but knowing about the “concept” will help make it less mysterious if you discover Community solutions here that use Virtual Switches (or any type of Virtual Device type and instance).

Feel free to chat with me (or the individual SmartApp authors) about this.

Appreciate the answers. I thought of using some form of virtual switches, but without built-in support for conditions on other switches, this means custom code for everything involved. However the links above showed me another way, using virtual switches as proxy wrappers. Interesting concept… I need to learn more about how to develop apps and what is and is not possible…

BTW, one solution to much of this is what Tasker (automation for android) does - they add a registry of global variables and every built-in has support for generic "only if <eq/new/contains/etc> ". If course Tasker is an actual rules engine where ST is not yet there, but I wonder if there is a way to implement something like that… Can one app read variables or run functions on another app or device? We could create a virtual object of some sort per room or for a house and keep all sorts of state there…

Like I said, I have got a lot to learn about how custom smart apps work and what they are capable of…

Thanks,

-M

1 Like

I agree, the absence of a rules engine/scheduler is one of the biggest weaknesses of smartthings. A couple of community members have built their own rules engines and those are interesting. Obycode has one that works only on iOS and JoeC has one which runs in a browser. You might take a look at both of those, because they have some of the features that you’re looking for. Both are still adding more features but they’re an interesting beginning, especially if you just want to use that functionality.

Look for the discussion of "rules engine "in various forum topics.

Also, the following category has community member created custom smart apps which might give you some more ideas.

http://community.smartthings.com/c/smartapps/created-smart-apps
1 Like

You are asking the right questions, but once you understand the few main objects in the SmartThings architecture, the answers will make more sense.

Here’s a very rough outline; (the Developer Documentation has official overviews, and may use terminology more consistently than I.)

A SmartApp cannot directly access Properties (variables and methods) of another SmartApp. Nor can Devices access directly access each other’s Attributes and Commands (or variables and methods).

There are hardly any globals worth mentioning, the most common being the single: location.mode.

Each SmartApp can be authorized to access any number of specific Device instances (“Things”) and can thus access Attributes and call Commands on these, which is the main way to make Things interact.

Devices send Events (generally when updating one of their Attributes, but also when running one of their own Commands), and any number of the same Account’s SmartApps can subscribe to these Events, which essentially gives us inter-SmartApp communication and data sharing… ie, devices can be a data storage and device events can be an internal channel of sorts.

All that without even mentioning external (physical) device or LAN or Web Service communication!

I dealt with a lot of the things you are asking about. I ended up creating custom device types to keep state data – so that different smartapps can share that data. Check out FLEXi Lighting.

2 Likes

Loving this community. Love the combination of quick responses, links and simple explanations. :smiley:

Thank you all for your responses, looks like I got some learning and playing to do.

-M

1 Like

Ok, so I managed to play with it a bit the other night. The good news is that I got something working - wrote a whole SmartApp that includes creating virtual devices and connecting to actual switch and to motion sensor and runs timers for auto-off, etc. (I realize much of this is duplicate of existing functionality, but I did it mostly to understand how this works) Overall its pretty cool to be able to write things in Groovy - but I got a number of questions due to somewhat lacking documentation. Specifically things like:

I found the way to create devices from app using the semi-undocumented addChildDevice() method (it is mentioned in documentation examples, but not in the reference page for methods available to SmartApps) - I was wondering if there was more documentation on this and if there is a way to create other components programmatically - specifically if there is a way to create “switch” apps (not sure if they are apps or devices or neither)? There is nothing in documentation (but then again, addChildDevice is missing too) and simple attempts to check what methods are available via reflection seem to have been disabled for security reasons. (So, is there a functional way to dump all the available methods?)

Also, it seems that there is some weird bug in the apps I am hitting that does not allow me to delete my custom smart app instances. I get errors. I found that if I delete the section of the code in update() that re-subscribe to the events and then update the app before deleting it, it works. It seems the issue is some dependency loop in creating child devices and subscribing to their events and then order of deletion of said subscriptions and child devices. (and yes, I tried an extra unsubscribe right before the code that deletes the child device, but delete still fails) Again, more documentation on lifecycle of SmartApp and child device creation would be ideal.

Next, I am trying to understand if there is a way to change state of a “switch” device without triggering events. More specifically I am trying to figure out a way to both control an actual (non-virtual) switch device AND react to that switch being controlled via means outside this smartapp. I realize the frequent way of using this is via a proxy virtual switch, however this is a problem if the light in question is controlled by the physical switch itself or a secondary controller that bypasses the ST hub altogether. Is there information on which smartapp triggered an even inside the event?

Lastly, and this is something I thought would be obvious but i find no way to do this - is there a way to provide default value for input fields in preferences?

Thanks,

-M

I expect a lot of people will be interested in the coding questions, so to make that discussion easier to find in future I started a new topic for you:

1 Like

Check out BETA call for superState scene app (all better now!)

Yes, you can add custom attributes and methods to existing device types. Use the sendEvent() command to change the state of a switch device or a custom attribute of a switch device. Check out SendEvent and currentValue - #28 by tgauchat

Sort of. I know that you can see some information within the IDE. Again, if you want to have a SmartApp send any custom data to a device – and then have that device store that custom data, you need to edit the device type of the device and add custom attributes and either add new custom methods or modify existing methods so that the device can store this custom data.

Yes. include:

       defaultValue: ______ 

in the input section that you want a defaultValue. For example,

input “defLevel”, title: “Select the level you want the lights to be.”, multiple: false, required: false, defaultValue: 99

-t

I will keep looking at that code, but unless I am missing something but I do not see this creating any “switches”, only “switch devices” (things) and “groups” . Language here is unfortunately confusing, but those are not same. The “switch” I am referring to is a virtual construct controlling one or more “Things” that appears in the “Lights & Switches” section of the app

I actually just want to see if the event that is generated by the device contains information as to what app triggered the event. Theoretically it is very easy to do in generic fashion without forcing us to replace all the built-in devices with our own modified copies :-/ I will keep looking at the event, but without complete documentation, way to do reflection, or better yet, a full on debugger - it is hard to figure out what information is actually in the event.

Works like a charm. Thank you very much!!

-M

I think you’re missing something.

Welcome to the joy of ST.

Check. Will look closer :smiley:

I looked at it again, and I still do not see it doing anything with the Lights & Switches - it creates a virtual device for controlling a group of other devices, which is nice, but not at all what I was looking for :frowning:

Well color me confused. Why is the way that program creates “virtual devices” (which are switches) not helpful to your search for a program that creates virtual switches?

I think he’s saying he wants to use code to create a Shortcut Group that would appear in the official mobile app so he could use the scheduler options that apply to Shortcut Groups.

Which as far as I know can’t be done by independent developers.

Although you can create your own shortcut group through the official app, of course.

But maybe I’m misreading it. Or maybe you can do it with code, I don’t really know that either. :smile:

1 Like

Don’t blame you for being confused. ST has switch “things” both real and virtual (like ones this program creates). But ST also has something also called “switch” that is NOT a device, but rather some UI construct that is neither a “thing” nor smartapp. These appear under “Lights & Switches” part of the UI rather than “Things”.

I want to figure out how to create the latter (if that is even possible)

Those are Shortcut Groups. See my post #17 right above yours. :smile:

2 Likes

OK, but I still fail to understand what advantage those shortcut groups provide that a smartapp-created virtual devices controlling a group of lights would not provide.

1 Like