Best practise regarding rules

Hi all,

Right, I’ve finally got myself where I want to be. All my things are sorted, everything linked etc how I want them and in a relatively neat manner finally.

I’ve got a presence rule set up and working how I want it so now I want to run some rules based on the dummy item that gets set when I’m away.

So the question is do I have one rule ie ‘when I am away do this this and this etc’ (ie turn on lights, stereo, and other looks like I’m home stuff), or do I have multiple separate rules all triggering off the presence dummy item?

What is best practice here in peoples opinion / experience?

My coding background suggests one rule for each thing to keep that separation ideology…is it the same in openhab?

RaspPi / openHabian 2.4.0

I suggest using your presence dummy item as a condition, rather than using it to trigger rules directly. I have:

  • One rule that runs when my Away Flag is turned on, which turns off everything in my house. I trigger this manually via Google Home.
  • A second rule that triggers when my presence is detected (phone or motion sensor), but only carries out actions if the Away Flag is on.

You could create a “Simulate Presence” rule that’s triggered at various times in the day, but only if your Away Flag is on: “if it’s 5pm or 8pm, and I’m not at home, then turn on these items”.

I’m not a good source for best practices, but I think it’s better to have one rule that triggers multiple activities, as opposed to having separate rules for your stereo and lights that use the same triggers and conditions.

I’ve lots of rules as I kept finding new ways to handle things OR I got fed up with having to manually do things.

I setup a rule that would handle disable lights when my spouse or I plug our phones in for the night; setup tasker to update the item for each phone. What I soon discovered is that the spouse never (EVER!) turned off any lights if I was working in the basement, so I threw more logic into the rule. If the outlet that enables my monitors is on and the other phone reports in that its time to sleep, all lights on the main floor turn off; when I turn off the outlet for my monitors the only 1 light turns on and when I plug it in when I go to bed all lights turn off.
Originally the goal was to just turn on a white noise machine in the master bedroom based on whichever phone and then turn it off when both phones are unplugged in the morning.

Personally I look at rules as a learning experience, while I may not set them up in the best practice way; I do find ways to make my life way easier!

Well said.
For me, I write a rule based on how I think it should work. Then I find that even if the rule has proper syntax and the programming logic is sound, there are always cases I haven’t thought of. I’m also beginning to believe that what seems a really simple task never is simple

Oh how true that is, I just recently hooked up a zwave Qubino relay to my garage door (spoiler, going to write up something as I found almost no info to make the installation easy); after 4 nights of being on a ladder I figured out my issue and can close the circuit to cause the garage door to move.
But getting that to work, which then led to creating rules to open when a phone arrives or when the door to the garage opens at certain times in the morning…hopefully my current logic will be flawless for tomorrow morning!

oh boy… I’ve spent several enjoyable weekends writing, testing and perfecting a new rule only to wake up on Monday morning or come home Monday night to find my perfect rule had some flaw. The results were often comical. When home automation works, it’s like magic. When it doesn’t work… it goes haywire!
I live alone so the consequences of my boo boos are my own to bare in lonely shame (and giggle about, especially when it involves lights flashing or alexa saying something) however, it sounds like the consequences your programming logic errors may be more dire so I hope your code is good and the garage door opens on que

I looked at my rules the other day and realized that something was triggering every morning (as it should), but I had no rules in openHAB to make that happen. So for a moment, it really did seem like magic! It took me awhile to realize that the rule was in IFTTT as a holdover from my previous setup. I just hadn’t gotten around to moving it over.

now that made me laugh! I’m just a beginner and already the rules files are getting large enough and numerous enough that sometime tracking down exactly where something is can be a task.

1 Like

Same here! my girlfriend laughs ‘theres an error in your rullleeeeee’ :slight_smile:

Lots of trial and error and lots of things doing things they shouldn’t. But numerous rules do work incredibly well such as motion , lighting, swiping etc.

You have to balance between two competing needs.

  1. Rules should be short, easy to read and easy to understand.

  2. Rule should run as fast as possible and you should have as few running at the same time as possible (see Why have my Rules stopped running? Why Thread::sleep is a bad idea)

:+1 Writing Rules is a journey. It’s always worth revisiting Rules when you learn something new.

It depends on the extent of the changes that take place when you come home or leave. If they are completely different I’d probably use two separate Rules. If they are largely the same with the differences being what commands get sent to the same Items, I’d do it in one Rule using Design Pattern: How to Structure a Rule.

If I have more than a couple of states (more than just home/away) and what is done in each state is pretty different, I’d use A simple scene management: finally a good use of scripts.