Is now the time to switch my rules to Jython? And what's the best way?

Hi all,

I’ve been tinkering with openHAB for a while now and have quite a few rules running in the existing Xtend based language. I have never quite gotten my rules doing as much as I have planned though, and I often have annoying bugs that I haven’t fixed, due to a lack of time and a lot of unexpected interruptions.

I finally think I have some time coming up where I will be able to work on things, but it seems that openHAB is moving away from Xtend and that Jython will be the preferred way to build rules in the near future (is this correct?).

I have done a little bit of experimenting with Jython, and have the basics working in my setup, but at this stage I don’t really have a good feeling for how to write rules in Jython for openHAB. I’ve looked at lots of examples, but with no Python background, I don’t feel anywhere near as confident writing rules as I do with the existing rules DSL. I have a strong Ruby background which I hoped would translate to Python, but they are less simliar than I had hoped.

There doesn’t appear to be any comprehensive documentation for using Jython to write rules at the moment (I’ll be happy if someone can prove me wrong on this and show me otherwise). So I’m left with a few choices and wondering what people would recommend.

  1. Forget about Jython for now and just write rules the existing way that I know how and make the best use of the time I have now. But will this lead to problems for me down the track when all the cool kids have moved away? Will the old way be supported indefinitely in openHAB or will I eventually have to rewrite them one day anyway?

  2. Tinker away with my existing rules trying to convert them to Jython to learn as I go. Hopefully getting to the point where my understanding reaches some sort of ignition point and it all starts to make sense. This is what I have done so far and, while I have made some progress, I feel like it has been slow going.

  3. Take some time out and learn Python properly. Obviously I won’t achieve anything tangible for a while, but hopefully when I come back to it things will make sense.

Thanks for reading this. I expect it will generate a few differing opinions, but I will be appreciative of any insights people have.

Option 3 transitioning into option 2.

At least a beginners familiarity with Python will go a long way to getting your rules switched over. Understanding what you can do with Python will help you conceptualize how you can adapt and improve your rules as you move them over. From experience, expect to rewrite half your rules at least once. When you start, you’ll inevitably make almost 1:1 copies of your Xtend rules which is a waste in Python. Once you get familiar with the extent of what you can do with Jython rules you’ll want to go back and rewrite the rules you wrote before you knew how much more flexibility you actually had to work with.

Start by getting somewhat comfortable writing Python code so you aren’t trying to learn 2 things at once. Jython is currently still running Python 2.7, keep that in mind as you learn. You can still use Python 3 tutorials, but know that you may have to tweak syntax and work with fewer built-ins than you saw in the tutorials.

Once you have a grasp on the language, start with your simpliest rule and just get it to work. Don’t try to get fancy yet. Read the docs far enough to get a rule made, comment out the DSL rule, recreate it in Jython and get it to just work.

After that, study the docs in more detail, read the advanced stuff, and experiment before you try to convert everything in one night.


All that aside, Jython has not made it into the official openHAB distro yet, but there is an addon for 2.5.x and work is being done (I believe) to bring that into 3.x hopefully soon.

2 Likes

I’d recommend doing a python getting started tutorial so you know the basics.
Since you are already a fluent programmer you can skip over most parts but you’ll have a proper roundup how everything works.

Xtend rules really hold you back and are nothing but frustrating to work with.
I really encourage you to look at the alternatives because once I eliminated them my passion for smarthome really came back.

There is also HABApp, a python3 rule engine for openhab which - since I am the author - will shamelessly recommend. :wink:
It has some nice features and you can run it from another machine than openhab and thus quickly try out if you like it.

I think the procedure suggested by @CrazyIvan359 is the way to learn the new ways and it doesn’t really matter what you use, as long as you make the switch.

2 Likes

For completeness, I should mention I am actually about to get started with HABApp. About 85% of my rules are currently Jython and the remained were never converted from Xtend. I’ve decided to start with a blank slate in OH3 and redesign/rewrite my entire home automation with the knowledge I’ve gained since I started.

1 Like

Hmmm… are you using the helper libraries and have you read through their documentation? https://openhab-scripters.github.io/openhab-helper-libraries

I haven’t put together a full tutorial on writing rules, but everythign you need should be there. I have considered a YouTube tutorial, but that has been delayed along with the merge of the Jython add-on.

I suggest 2, transitioning to 3. Michael has given some good advise, but I suggest diving right in, especially if you have a development background. You will learn a lot with just a few rules under your belt. When you are stuck, it is easy to find solutions using Python on the Internet. And just ask in the forum!

Yes… and you will look back at the rules DSL and wonder how you possibly got by using it!

If you post a DSL rule, I will convert it for you to get you on your way. Once you see an example, the patterns are easy to follow!

You will appreciate the jRuby add-on then :slightly_smiling_face:. I will submit it after the Jython add-on issues have been addressed.

4 Likes

Thanks for all of the feedback here. The openHAB community is great!

You’ve convinced me to abandon my Xtend rules and put the effort into switching to Jython. I’ll probably lose some time initially but it sounds like it will be a worthwhile investment. I’m going over some basic Python now (briefly) to get a very basic background and then I’ll start looking at examples and trying to convert my rules, then try to consolidate my Python with what I’ve learned.

I’ll return here with some of my early efforts if I’m running into trouble. (When I’m running into trouble). Hopefully there will be some massive improvements for you to suggest and for me to learn from.

I’ll have a look at HABapp, and I’m very interested in the JRuby add-on.

1 Like

Alright, not sure if I should post here, or in a new thread, but I’ll go with here.

After a couple of weeks of reading Python docs and experimenting in my spare time, I have written my first rule in Python. I thought I would post it here to get some feedback. I don’t have an original Xtend DSL rule equivalent for this as I wanted different behaviour from what I had previously.

This is a fairly basic time of day mode setting rule that sets the Time_Of_Day String Item to a value of either “Day”, “Evening”, or “Night”. It is night time if it is after 23:30, or before 06:30 on a weekday or 08:00 on a weekend. It is day time if it is after 06:30/08:00 and before sunset. It is evening if it is after sunset or before 23:30.

There is not a great deal of difference between this and how I would have written it in the old Rules DSL. Have I gone about it the right way? Is there a more concise axiomatic approach I could be using? Any feedback will be very welcome. I haven’t used this rule over a 24 hour period yet, so there may be bugs I haven’t yet found.

from core.rules import rule
from core.triggers import when
from org.joda.time import DateTime

@rule("Set Time of Day")
@when("System started") 
@when("Channel astro:sun:home:set#event triggered START")
@when("Time cron 0 30 6 ? * MON,TUE,WED,THU,FRI")
@when("Time cron 0 0 8 ? * SAT,SUN")
@when("Time cron 0 30 23 ? * *")
@when("Item Test_Trigger changed to ON")
def set_time_of_day(event):
  now = DateTime.now()
  day_of_week = now.dayOfWeek().getAsText()
  if day_of_week in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]:
    day_start = now.withTime(6,30,0,0)
  else:
    day_start = now.withTime(8,0,0,0)
  evening_start = DateTime(items["Sunset_Time"].toString())
  night_start = now.withTime(23,30,0,0)
  if now.isBefore(day_start):
  	time_of_day = "Night"
  elif now.isBefore(evening_start):
  	time_of_day = "Day"
  elif now.isBefore(night_start):
  	time_of_day = "Evening"
  else:
  	time_of_day = "Night"
  set_time_of_day.log.info("Setting Time_Of_Day to " + time_of_day)
  events.sendCommand("Time_Of_Day", time_of_day)

Compare with this, which you can find in the community contributions…

I have an update to push which incorporates days of the week.

Sorry for slow interactions.

Yes, I had seen that example in the docs. I’ve spent some time working through it and I don’t really understand all of the decisions that led to it being written that way. It feels unnecessarily complicated to me. A few times of day don’t seem like complicated enough data to warrant putting in their own config file, rather than hard-coding it into the rule, although I know a lot of people would argue otherwise.

Anyway, my rule seems to work after a few days of ‘testing in production’ so I’m pretty happy with my first attempt. I have tried to extend it a little by checking for public holidays on weekends and have failed at the first hurdle of installing the python holidays library. I’ve started a new post about it here if anyone is able to have a look.

Thanks.

Just ask and I will explain whatever you would like to understand about it. It has a lot of functionality and was made so that users could copy it just modify the configuration file without having to touch the rule itself. It is much easier to support it this way, compared to the rule you have made.

Why not use Ephemeris, which is included in openHAB core?

Is it very difficult to learn python if you have zero programming background?
Any pointers to where I can start learning?

There is some suggested reading in the helper library documentation…

The bottom one is a nice beginners video series done by Microsoft.

1 Like

I’ve liked Python less than I thought I would, but as far as languages go it’s one that’s often recommended for learning to program. 5iver’s suggestion is probably as good as any. I’ve been using the official Python docs to learn, but they’re better suited to someone with some programming background.