Setting a master variable / switch

Hi all,

First post, (hopefully of many), so be gentle.

I’ve got openHabian running on a pi3 and so far so good. I’ve spend a few weeks running through the tutorials and forums and have done the usual install, fiddle, brick, re-install, learn a bit more, brick etc and I’m pretty happy I’ve got my items, things, groups, sitemaps all where I want to be and I’m now writing rules and - as a coder in a previous life - I want some steer as to the concept of variables.

Usually I’d do something like ‘If I’m home set a global variable_home to ‘ON’ or something similar’. Then I’d be using that variable elsewhere to trigger stuff or something. I’ve got in my head that I can create a dummy item in the .items file which I can then use in a .rules file and also in the .sitemap file to give me a switch to have a visual representation of it, and also to give me the ability to toggle it manually.

Now, first part…is this the best way to be going about this or do I need to change my code thinking a bit?

Second part…I’m also used to having a ‘Master’ variable that I use in all if / then type statements so that if everything is going wrong I just set one variable to OFF and then no rules run…kind of a big off switch if you will.

If got this working in principle, I have a variables.items file with an item

Switch MasterSwitch “Master Switch”

The I can use it in rules only if Item MasterSwitch = ON

However, I’m thinking I’d have to add it into every rule that I want to be able to master switch OFF should I need to, and that just seems a lot of work to remember to add it in every time…

Again, am I thinking illogically around this? Is there a better way in openHab?

Thanks in advance


I think you are on the right track as far as using ‘dummy’ items to hold variables. OpenHAB also has group items which can hold many individual items and can be of type Switch and be toggled on and off in mass. Perhaps a solution to your master switch idea

Great thanks. I didn’t want to go too far down this road only to be told why are you doing it that way, don’t you know there is a simply in built command for that…

I’ve been playing with groups and haven’t quite got that down yet…maybe time for some more reading!

A topic that may interest you, about groups and rules, created by one of the best.:sunglasses:

Thanks, great minds! I’m actually scratching my head through that very tutorial now!

I’ll add that coders tend to have the biggest problem with the Rules DSL. Unfortunately, the Rules DSL (i.e. .rules files) requires the developer to bend to it’s will instead of bending to the developer’s will. And it doesn’t use and/or poorly supports many concepts experienced coders use as second nature like classes, functions, etc.

Non-coders have a much easier time as they don’t have to bend at all. They can learn it the Rules DSL way the first time and there is a simplicity and lack of complexity in Rules DSL which makes it relatively easy for non-coders to get started with.

If you find yourself struggling I strongly recommend looking at JSR223 where you can code Rules using a more robust programming language like Jython, JavaScript, or Groovy.

If you decide to stick it out, read the Design Pattern posting. Most of the common situations you might encounter are covered.

Sounds reasonable. You might be interested in Generic Presence Detection which takes your concept a couple of steps further to include aggregating multiple sensors for presence and anti-flapping so the house doesn’t go into “away mode” when you leave for just a couple of minutes to get the mail. All in 40 lines of code (including blank lines).

The Next Gen Rules Engine will support something like this. For the Rules DSL you will have to add an if statement to all your Rules to check for the Switch and return; if it’s ON instead of running the rest.

Honestly, I don’t really see that much benefit in having this master switch for all Rules. I’d implement something more targeted, like one for Lighting, one for HVAC, etc. But I gotta say I don’t do anything like this either. I’ve never had a case where I wanted to turn off the Rules where I wasn’t there actively developing them in which case a simple comment/uncomment sufficed.

I actually recommend not starting with that DP. Instead start with something like Associated Items DP or the Generic Presence Detection tutorial I linked to above. It’s easier to understand when you can see how Groups can be used to solve a “real” problem than what amounts to a reference guide which the Working with Groups in Rule DP is. The other important ones I’d point you to is the Time Of Day and Separation of Behaviors DPs.

Awesome thanks, I am having a little trouble ‘letting go’ of my existing code syntax and structure etc but I’m beginning to see the dark / light side, (delete as to your preference), and just surrendering to the openHab way. The more I read and play the more I’m being able to relate a current coding concept into a openHab one, and the more I’m seeing just how amazingly powerful it all is.

The Master Switch idea came from the fact that sometimes we are away but have someone over pet / house sitting, (often my mum), and she’s not particularly tech minded so I just wanted something that I could turn to off so my house reverts to just a non smart house for her to potter around in without worrying if lights come on at dusk, plugs go on to turn on the tv when I get home etc, I think she’d think the house was possessed!

I’ve just gone through the DP tutorials and found this one related most to the dummy variable thoughts - for anyone else with the same problem as me.

I’ll take a look at the Generic Presence Detection tutorial now…it’s very timely as presence detection was my next project, so two birds / one stone and all that!

The hardest part for some seems to be grasping the event-driven openHAB world. If you find yourself building complicated sequencing and timing structures, take a step back.

Good advice. I’ve spent the last day or so reading all the DPs and working the Presence example listed above, and then got really into Groups…

I’ve now decided to pair everything back and start a fresh so I’ve just removed all the bindings, commented out the rules I’ve got and am starting a new to see if I can simplify things…

Also, to answer my own question about the master variable…there’s no need for one as, done correctly, it seems the groups do a better job of controlling things.

I like to recommend an approach that is best covered by a Mitch Hedberg quote. I’ve quoted is so many times on this forum I’ll just paraphrase. “You should never see an out of order sign on an escalator. Instead you would see escalator temporarily stairs, sorry for the convenience.”

When an elevator breaks it becomes unusable. When an escalator breaks, theoretically it can still be used as stairs. In home automation, endeavor to build escalators, not elevators. When your OH goes down or your Rules are otherwise disabled, the house should still be intuitively useful. This approach will drive a lot of choices (e.g. smart bulbs don’t work well with this approach) and can possibly limit what you choose to do in Rules. But then when things break or non-tech guests come over, they can operate the house without extra training.

Many of us have had the experience of teaching a parent or the baby sitter or someone how to use the remote to operate the TV. Let’s avoid expanding that to the rest of the house. :wink:

Good advice, I’m finding it very easy to get carried away with getting very technical home automation set up but keep having to pair myself back to the basics.

Onwards and upwards!

But Why? They should learn how to use a mobile phone to switch the light off before going to sleep and discovering that the Roomba starts cleaning in the middle of the night causing them to trip over the hue sensors…

Ah, “just wanted to go to sleep” you said?


1 Like