How to Get Started Creating Complex Rules in SmartThings

(This is a wiki post: feel free to edit it or add to it as needed. Also, I can’t take any screenshots since I can’t use the app at this time due to its incompatibility with voiceover, so feel free to add screenshots where they would be helpful.)

At one point, one of the biggest advantages of using the SmartThings platform was the wide variety of devices that it supported. However, by the end of 2017 several competing platforms had caught up in that regard.

Where SmartThings still holds a significant advantage over some of the other low-cost home automation solutions is in the ability to really build really complex rules in a fairly simple manner. You won’t have to be a programmer, but you may need the help of other community members to get the logic constructed just right.

Also, you don’t need a smartthings hub to do this: the rules engine is built into the platform architecture. So even if all you have is some Wi-Fi switches that work with smartthings you can still take advantage of its sophisticated rules.

So how can you take advantage of this ability? This thread is intended as a basic introduction to building complex rules in SmartThings and was created at the request of @Raeven, who was looking for guidance on getting started in this area. Again, this thread is not intended for programmers, who will probably want to instead go straight into the developer materials for the SmartThings platform. This thread is intended for people who are not programmers who want to use some of the existing tools to create complex rules. :sunglasses: If you add to the thread, please keep this audience in mind.

A Basic Rule: If A, then B

There are many low-cost, automation systems that can perform basic rules of the “if A, then B” format.

if I arrive home, turn on the porch lights

if the closet door opens, turn on the closet light

if it is 15 minutes before sunset, turn on the Living room light

if there has been no motion in the bathroom for 15 minutes, turn off the Bathroom lights

Although there are some variations in the exact devices and options available, this basic “If A, then B” setup is available with everything from HomeKit to echo plus.

And Amazon has been increasing the complexity of the rules offered through its free Alexa Routines.

There is also a subscription service, IFTTT (“If This, Then That”) which will let you tie devices from many different companies together in this kind of basic rule. “If the thermostat drops below 68°, turn my hue lights blue.” (This used to be free to consumers, and was very popular then, but now charges a monthly fee if you have more than three rules.)

In SmartThings, you can do this basic structure with the official Smart Lighting feature, the official Smart Things Home Monitor feature, and the official Routines feature. You may need to check all three of these locations to find the exact options you’re looking for.

It also participates in the IFTTT service.

Level 2: If A and B, then C

The next most complicated type of rule is a compound rule. It lets you combine some additional conditions, usually from a very limited set of possibilities, with your original rule. In low-cost home automation systems, the most typical kind of compound rule lets you also check home/away or time of day.

If I arrive home AND the time is between sunset and sunrise, turn on the porch lights

If the closet door opens AND it is before 9 pm, turn on the closet light ( to keep from waking the baby up :baby:t3:)

If it is 15 minutes before sunset AND I am home, turn on the Living room light

Many Low cost home automation systems can do this, but not all. HomeKit can, but only for time of day and home/away unless you use the more complex Shortcuts feature or one of several thirdparty apps. Alexa routines can do this for some conditions, but it can’t turn on one light switch because another one is turned on.

Also, the free IFTTT service does not have compound rules.

Smartthings has compound rules for both time of day and home/away in the official smartlights feature and in the official automations. The official features also offer the option to use “Mode” which adds some additional compound rule capabilities.

https://support.smartthings.com/hc/en-us/articles/205380404-Smart-Lights
.

https://support.smartthings.com/hc/en-us/articles/115002056383-Custom-Automations-in-the-SmartThings-app

.
https://support.smartthings.com/hc/en-us/articles/200962880-How-do-Modes-work-

Level 3: Stacked Conditionals: Or, While, Unless

The next level is where the rules start to get really complicated, and most of the low-cost home automation systems fall away. This is when you start combining conditions and also checking for the absence of a condition.

If I arrive home AND the time is between sunset and sunrise OR if the motion sensor on the front porch detects motion during the same time, turn on the porch lights UNLESS The date is between Thanksgiving and New Years (keeps the porch lights from spoiling the effect from the outdoor holiday lighting)

If I press this switch AND it is before 11 PM, turn on that outlet UNLESS I have guests, in which case ignore the time restriction

If the closet door opens AND it is before 9 pm OR the mode is “baby sleeping” turn on the closet light ( to keep from waking the baby up :baby:t3:)

If it is 15 minutes before sunset AND I am home, turn on the Living room light UNLESS I am watching a movie

If there has been no motion in the bathroom for 15 minutes AND the humidity levels have not been rising, turn off the bathroom lights (this keeps the lights on if someone is in the shower where the motion sensor might not detect them)

If the indoor temperature is above 78° AND The bedroom window is not open, start the bedroom air conditioner UNLESS I am not home

And a community favorite:

If there has been no motion in the living room AND I am home turn off the Living room lights UNLESS the lights were turned on with the wall switch (manual override of the automatic light turn off)

Stacked conditionals can get really complex. Maybe you want to combine several different devices, so that a rule only gets triggered if a door is open AND there is motion in the room AND you are not home. (so your dog moving around doesn’t trigger your burglar alarm siren)

The point is that most low-cost home automation systems, including HomeKit and Wink and Alexa routines, just can’t handle these kinds of rules. But SmartThings can, and much more complex ones as well. However, it can’t do all of these kinds of stacked conditional rules through the official features.

(Note that the new V3 Version of the SmartThings mobile app has an official rules creator which does offer more stacked conditional options than the classic app did.)

If you can’t find the options you want in the official features, you will have to write your own code, use a third-party app like SharpTools, or learn to use the Rules API.

To Infinity and Beyond: other ways of creating stacked conditional rules

In the early days of SmartThings, if you wanted to set up a stacked conditional rule, you had to write your own code. People did share the code with others in the community, but still. It was a lot of work and hard to follow.

Eventually, one grandmaster coder in the community, Bruce R, created “Rule Machine,” a smartapp that let you create some stacked conditionals. It was immediately very popular with the more technical community members, but eventually the author moved on to other projects and the code was withdrawn. Then for a while, there was another community written option, Webcore, but that doesn’t work on the 2023 architecture.

So in 2020, SmartThings introduced an official feature, “The Rules API,” which is similar to WebCore, although not as yet as fully developed or documented. But it is expected that in the future it will eventually provide full support for these kind of complex rules. Which is important, because when smartthings discontinues its free groovy cloud, Webcore will stop working. But the routines you can build in the smartthings app and the rules API will still work with the new architecture.

Additionally, the third-party SharpTools, has added a lot of features to its rules engine. They have both a free tier and a more advanced “pro” level, which costs $30 per year at the time of this writing.

Neither SharpTools nor the Rules API can address any of the underlying SmartThings architecture issues, like cloud latency or reliability, but they definitely answer the question of “why would I want to use SmartThings instead of HomeKit or Echo Plus?”

The answer is simple: if you want to go above basic “if A then B” rules and start creating automations that include OR, UNLESS, and WHILE, most of the low-cost competition won’t be able to do it and SmartThings will. But you are going to need to either write your own code or use something like SharpTools to accomplish that.

*A Note About Stringify: Now Discontinued

Sometimes referred to as “IFTTT on Steroids,” stringify was a third-party app which let you create visual “flows” which are stacked conditional rules tying together devices from different systems. It was quite popular with people who liked a more visual approach to rules design and management.

Unfortunately it was bought by Comcast, and in April 2019 it was announced that Comcast was adding it to their own home automation system, and would stop supporting the free version. So any references you see to it in the forum refer to the time when it was available to anyone, but no longer apply. :disappointed_relieved:

Stringify shutting down (April 2019)

Getting started creating your own rules for SmartThings

Personally, I’m a great believer in practical applications. So I usually recommend that people start by designing rules that they actually intend to use, rather than just trying to come up with the craziest most complicated rule they can imagine and then implementing that. But it’s your choice. :stuck_out_tongue_winking_eye:

It’s also your choice whether you want to just start with the official routines feature and play around with rules in that until you hit something that you can’t accomplish and then move on to SharpTools or the Rules API, or whether you want to jump right in with the more complex rules engines from the beginning and do even your simple rules with those.

Getting More Help

If you have any questions about setting up a specific rule, please do not add them to this thread.

If it is a question about any of the official features or just how to set up a rule in general, start a new thread in the following section of the forum:

https://community.smartthings.com/c/smartapps/smartapp-ideas

Unless your question is specific to the new rules API. That now has its own section in the forum:

And if you want to write your own code and you need help with that, see the developer section of the forum and again you will get the quickest and most expert responses:

https://community.smartthings.com/c/developers/writing-smartapps

Sharptools also has its own community forum where you can ask questions about it.

Have fun! :sunglasses:

SmartThings is a very powerful, versatile system even for non-programmers. You will have to do a little research to get started, but it definitely has rules capabilities far beyond many of the other low-cost home automation systems, so if you want those kinds of complex rules, it may be a very good match for you.

On the other hand, if you decide all you need are the most basic rules, then you may prefer to choose a system which is simpler, more intuitive, or less dependent on the cloud . Choice is good. :wink:

Even more ideas

And for more ideas and inspiration on what you can do with SmartThings, check out the quick browse lists in the community – created wiki, particularly the project reports section.

There is a “get started“ list there that includes topics like “the top 10 things to do with SmartThings“ and “what device should I get next?” as well as lists for everything from holiday projects to baby nurseries.

Some of those involve complex rules, some are done with very basic rules, but there’s a lot of ideas from the community that are worth checking out. :sunglasses::tada:

FAQ: Using the quick browse lists without the community wiki

52 Likes

Incredible writeup @JDRoberts. This will be highly beneficial for prospective and new members!

5 Likes

Is JD the last JeDi?

7 Likes

Agreed…he’s a god among children. :slight_smile:

1 Like

Wow. On behalf of all users, thank you for the detailed write up, covering a lot of bases and options. Thank you.

3 Likes

Well, if you hadn’t written WebCoRE, there wouldn’t have been so much to write about! So the bases are mostly on you. LOL :stuck_out_tongue_winking_eye:

8 Likes

Thanks for this intro and summary @JDRoberts :+1:

I learned several things that I didn’t know before :slightly_smiling_face:

Hi,

Can you point me to the rules creator in Smartthings new app?

“Note that the new V3 Version of the SmartThings mobile app has an official rules creator which does offer more stacked conditional options”

Regards
S

You access it anytime you “add an automation.“ So just click on the + in the upper right of the home screen and then choose automation.

There is also a new “rules API“ which can do even more complex things, but it’s still in development. :sunglasses:

2 Likes

Thanks @JDRoberts!
I was confused with the wording Rules Creator and thought that Rules api was already implemented.

So within Smartthings,
Automation = Official Rules Creator

There are also features like
Smart Lighting
Scenes
Webcore Rules Engine
Smartthings Home Monitor

All of them have ‘If A then B function’ and some have more complex ones. Are there plans to merge all these functionalities into 1 single smartapp?

Regards
S

1 Like

Webcore is unofficial, community-created, And the official equivalent is the new rules API. So it will remain standalone.

As for the others, there’s no telling what will be combined. They haven’t said anything official one way or the other. :sunglasses:

hello,

is there any way to do :

activate this plug, after X ( let’s say 3 ) detections from a motion sensor ?

thanks for your help

WebCoRE can do it. You can post on the webcore forum and there are users who can help you build the piston

1 Like

thanks a lot for your answer jkp, i’ll have a look to the webCoRE community then .

Thank you. I have learned a lot from this extremely helpful post.

1 Like

Just found this forum and now this Wiki… Exactly what I was needing! Cheers!

2 Likes

So will the new API engine do things that Webcore can? For example, I had to write a piston to allow a button remote to increase Sonos Volume, with automations I can only set it to specific value and not increment it on button press?

Similar, but not identical, and the new Rules API is still,in development. For further discussion of its details, please see:

FAQ: Getting Started with the new Rules API

1 Like

The ‘will’ is perhaps the key word there. At the moment the documented features are relatively sparse, though still incredibly useful. However, given that the Automations have migrated to rules, it seems reasonable to assume there are some undocumented features (not necessarily available to the punters). For example, button presses work and as they don’t return to a standby state you really need an equivalent of the webCoRE ‘gets’ trigger, using ‘is’ or ‘changes’ just isn’t the same thing (the latter isn’t documented either).

I can see the Rules API having a full range of loops and comparisons, but that is really the basic stuff. Where webCoRE comes into its own is in its use of variables and arrays, and arguably even more critically the Task Cancellation, Execution and Scheduling Policies. That is where it gains the power to handle real world situations that would otherwise need bespoke logic. So that is the stuff we really need to see.

4 Likes