Rule Helper Tutorial

Rule Helper

In past tutorials we have introduced the concept of Rules on the SmartThings Platform. In this tutorial we will be going more in depth about rule creation and providing a demo app that will allow you to browse examples of existing SmartApps re-created as rules. The Rule Helper also has samples of simple and advanced rule structures.

The sample app was built using the create-react-app project, so you will need node.js

If you don’t have yarn

npm install -g yarn

First, clone the repo to your computer.

git clone https://github.com/jodyalbritton/rule-helper.git

Second, install the project

yarn install

Finally, start the project

yarn start 

If the project started, should see a window like this

The sample code has placeholders for the required devices, modes, scenes, etc. that you will need to locate based on your own location. Let’s look at an example

In this example you can see that the IF Block requires at least one Device ID and the Then Block also requires at least one Device ID.

Once you have those, you can follow the directions here for posting your rule to the proper endpoint

8 Likes

Thank you for your great work !

I’ve just installed on my linux server, and reverse proxied with NGINX.
It’s very easy to navigate, and I could edit jsons right on the web browser.

It still just navigates rules examples and templates though like examples below, I hope it would be great to integrate with Smartthings API in future.

SmartThingsCommunity/Rules-API-Hub (github.com)

Integrating the SmartThings API would not be difficult, but our goal with this example to provide an easy way to discover and explore rules. I put a license on the project (Apache 2.0) so any enterprising community member may fork and extend this tool.

The developer newsletter states that 60% of groovy smartapps can be done with the rules api instead, but what about the other 40%?

The percentage is a conservative estimate based on the current capabilities of the Rule API. As more features are added to the Rule API, that percentage will increase. It is also worth noting that in the past we did not have an official API so there are many SmartApps that exist only to provide an API interface for the developer. SmartApps were the solution to every problem in the past.

For more complex applications we have Endpoint SmartApps and the SmartApp SDK. Rules will solve the vast majority of the automation style SmartApps. Our API will be the gateway for building service connector Apps. Cloud Connected devices can use Schema or Endpoint connectors.

1 Like

Hi @jody.albritton,

Checked out the repo and its very useful tool, thanks for sharing!

Just wondered if there was any update on local execution of rules on the hub?

Is there any plans to make rules implement a bit more complex logic such as database communication directly from a local rule running on the hub to a database server?

Currently have a subscription service set up on our Endpoint SmartApp where the SmartThings cloud pings our server with information on sensor updates, our Express JS server then logs this to the database.

Possible SQL statements executed by the hub with no middleman Express Server could be useful.

We don’t have any concrete timelines, but there is heavily development ongoing to make this happen. Local rules will require more local devices.

There are plans to add variables and expressions to the rules grammar, but for scraping a server or accessing information outside of the SmartThing’s platform you would still be better served by an endpoint app.

This could be replaced with a more simple oAuth connector and subscriptions. If you are just looking at a solution to push data out of the platform for logging purposes. PM me if you have a requirement for this because we have a closed alpha ongoing at the moment.

1 Like

Hi @jody.albritton,

Thanks for the reply!

Can I ask what is the criteria for a device to be a local device? As it may affect what devices we are using with our solution in future.

This is great news, will it still remain in the JSON format or an maybe a shift towards more object oriented language to define rules?

Sorry I think I may have mixed up endpoint/OAuth, what I am using at the moment is an OAuth integration as specified here after filing a request to gain access to the developers workspace. User signs in to our login system and then it prompts them to sign in with there Samsung account, my backend then sets up the subscriptions with the Node SDK for SmartThings.

PM sent with more information on what is being worked on!

The rules themselves will continue to be stored as JSON on the platform. Transforming something like javascript into the rules JSON would be more akin to a rule builder. I will also respond to your PM

1 Like

It must use a stock device handler that is specifically marked to execute locally (because its also loaded on the hub itself)

That means no custom DTHs.

Hi there,

I’ve done a few groovy apps, some of the 40% I guess that still can’t be done with rules (To my knowledge). My most popular, and useful app, is one that turns on lights for a period of time when something happens (like movement). “Oh”, I can hear you think, “we can do that” or "That’s a standard app already, but not so fast I say…

My app will keep resetting time to the timer on subsequent events, so if if a switch turns on for a minute and then someone moves again, the timer is reset back to a full minute.

My app will STOP the timer if the device is turned off and on manually, so a user can easily keep a light on and not worry about it turning of later. Very practical say for going in the garage. The lights comes on, you can see the switch, toggle it, and now the lights STAY on and the timer is cancelled.

These two little things makes the house ‘just work’, and you are never annoyed that timers turn of too fast, or you can’t stop them from turning off if you need to… For example.

My question is, when can these two situations be expected to be supported in rules so I don’t have to keep my groovy application around (I do NOT want to re-write it). ?

@orangebucket, Graham I think you’re farther along exploring the Rules API than just about any of us? You’ve done some advanced work with rules if I’m not mistaken?

Not as advanced as I’d like to be. Downright simple in fact. The documented rules are pretty basic and nothing new has appeared for yonks. However we are told that the Automations have been implemented as rules which suggests there must be a lot more out there.

For example, the ‘remains’ condition must surely exist (something like the webCoRE ‘stays’), there must be something to handle button presses (it needs something like the webCoRE ‘gets’ which is only a trigger not a condition as there isn’t a standby state), and there must be commands to send notifications and execute scenes. The weather service is clearly available too.

Going back to the webCoRE proof of concept from late 2019, the ‘changes to’ command is there but doesn’t seem to be live, or isn’t documented.

What I am not aware of yet is any of the bespoke logic exemplified by the Task Cancellation Policy in webCoRE and the behaviour of the Smart Lighting app. The sort of thing that allows existing scheduled actions to be cancelled when conditions change.

I’m not a developer and probably don’t know what I’m talking about, but I do know you can execute scenes via a Post to this address. I do this via Webcore. Probably not the same as doing it from the rules API though.

https://api.smartthings.com/v1/scenes/scene-guid/execute

I guess I’ll wait for that, at least the Groovy apps still work… :slight_smile: Thanks guys.