Motion activated light only if light is already off?

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
  1. Official SmartThings tech support.

  2. “Solution” SmartApps (aka Dashboard SmartApps?)… Also officially supported by SmartThings Tech Support.

2 Likes

Most importantly - not needing to rewrite bulk of ST functionality from scratch. But if it is not possible to do via code, it is kind of moot.

Hey, i now know what they are called!! :smile:

:frowning: so close…

-M

So what is the answer here? I have a hallway light that I only want to come on in the dark. If the light is already on I don’t want it to run.

There may be a better way these days, but back then I ended up writing a custom app that created two virtual switches - one for motion sensor to control and one for override. The real switch would be on if EITHER of the switches is on, and off when both are off. This way I control it via the override switch if I want it to stay on (normal operation) and while motion sensor controls a switch like normal, it only turns on/off the light if it was not on already.