Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rules Concepts page #3

Merged
merged 4 commits into from
Aug 8, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion rules/concepts.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!---
# Cover generic rules concepts

Pull stuff from the Getting Started Tutorial.
Expand All @@ -22,4 +23,59 @@ formerly implicit variables
How to find and install.
How to write will be on a later page.

### Comprehensive Examples
### Comprehensive Examples
-->

# Rules Concepts

Thus far we've connected openHAB to devices through Things, modeled the devices with Items, discussed persistence and how to build the display to control your home.
But all of that amounts to home control, not automation.
To create home automation we need to define rules.

florian-h05 marked this conversation as resolved.
Show resolved Hide resolved
{::options toc_levels="2..4"/}

- TOC
{:toc}

## What Are Rules

You can think of rules as routines or behaviours for your smart home.

Just like you stand up in the morning and have your breakfast, your smart home can have a routine to open the blinds when the sun rises.
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved
Another routine would be to turn all lights off when you leave home: openHAB can do that for you, no need to open your app and switch of the lights manually.
You want to hear your favorite music when you arrive back at home? No problem, openHAB can do that for you.
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved
But rules can also remind you of things: you opened the window hours ago and forgot that is is open? openHAB can sent a notification to your phone.
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved

To summarize in an universal principle: *When __a__ happens, than do __b__*.
For many cases, it can be practical to extend that to *When __a__ happens, than do __b__ but only if __c__*.
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved

To take some examples from above, you can have openHAB to only turn on your favorite music when you arrive at home if it is not your kids bedtime.
You do not want openHAB to open your blinds on the weekend because you want to sleep a little bit longer? No problem, *but only if* helps.
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved

## Parts of a Rule

To work with the *When __a__ happens, than do __b__ (but only if __c__)* principle, openHAB rules consist of three parts:

florian-h05 marked this conversation as resolved.
Show resolved Hide resolved
| Name | MainUI Section | Purpose |
|-------------|----------------|----------------------------------------------------------------------------------------|
| `Trigger` | When | The *When __a__ happens* part: It causes the rule run when the defined event happens. |
| `Action` | Then | The *than do __b__* part: What should be done when the rule runs? |
| `Condition` | But Only If | The *but only if __c__* part: Which condition has to be met that the rule really runs? |

The __a__, that causes the rule to run, is an so-called event in openHAB.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm torn here. We keep flip flopping between "event" and "trigger". I think we either need to stick to one or the other almost everywhere (I'd choose "trigger") or somewhere in this section make it very clear that:

  • event = something that happened that is detectable by openHAB: time, system event, Item states and commands
  • trigger = identifies an event that will cause a rule to run

The difference is subtle but I fear there might be some confusion if we are not absolutely clear and disciplined in our use of the terms. Events are happing all the time. The trigger is like a filter to identify just those events that we care about to trigger a given rule: a specific time, Item changed to ON, etc.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will use trigger in that section and add a note about the event later in the triggers section.


::: tip A few words about events
openHAB is an event-driven system.
That means, that everything that happens with Items and Things is an event and some parts of openHAB listen to these events.
When your kitchen light (Item) turns off, this is an event. And when openHAB lost connection to your kitchen's lightbulb (Thing) that is an event too.
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved
The UIs will see the Item event that your kitchen light turned off and update their state accordingly.
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved
And a rule can trigger on the Thing event "lost connection to the lightbulb" and perform an action, e.g. sent a notification to your phone.
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved
:::

Any single rule is not required to have all of these (although a rule without an action is not very sensible).
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved

A rule that should always run when triggered will have 0 conditions.
A rule that should respond to several different events can have multiple different triggers.
Even though it might not seem sensible first, a rule with no trigger (that means it would never run, except manually) can still be useful: you can use it as an reusable action to call from other rules.
These rules are called *Script Actions* in openHAB, more about them later.