A State Machine Primer with HABlladin, the openHAB Genie

Excellent tutorial. Very entertaining and informative.

Just a couple of comments:

  • The indentation in the code is a little off making it hard to follow.

  • You might have a missing } in your second to last state.

                        if (GLSMtimer != null) {
                GLSMtimer.cancel()
                GLSMtimer=null

Particularly with this kind of code, getting the indentation right really helps one follow the code and see these sorts of errors, if there actually is an error.

This is true for OH 1.x. For OH 2.0 the “Uninitialized” state is NULL (not to be confused with null). Also, this is an OH thing, not an XTend thing.

Perhaps using this. :slight_smile:

Have you considered separating the bodies of each state in the if else machine and moving them to their own rules? In my mind as I’ve been trying to tackle coming up with a good generic example for state machines I have been toying with using the machine part to trigger events that kick off Rules rather than ending up with a several page long rule. Obviously there are positives and minuses. Thoughts?

It’s a challenge and there is a recent lengthy discussion about it on another thread. At the end of the day it is difficult to detect the origin of an event (i.e. rules, binding, sitemap). So probably your best bet is to use a Proxy Item from the sitemap that only the sitemap commands and rules to forward those commands as necessary, perhaps setting a flag or something if necessary to indicate the origin. I do something like this in my Dead Man’s Switch design pattern but boy is it ugly.

Excellent work! Thank you for sharing.

1 Like