Python module for easily accessing the openHAB REST API

It’s been a couple years, but it’s still called Experimental Rule Engine. Just need more volunteers to use/test it so that it bcomes mainstream.

Hmm that is exactly what has been holding me off. In OH1 I never got around to using it, in OH2 I never bothered as its well “hidden” an marked as experimental :slightly_smiling_face:
Glad to here otherwise !

My rules are all migrated to Jython and they’ve been running great. The ESH guys are pretty quick to resolve any issues too. I also have a nice update for the rule decorators that really simplifies rule creation. Just want to get one more thing fixed up before posting in the forum.

Cool! Looking forward to that then!
If that is an existing thread here, could you please share the link? :wink:

It sounds like everything your doing could be done in the rule engine.

I quickly browsed your repo on github. Not clear on the point if I can implement items/bindings in python as well?
That would definitely solve a lot…

No need to stop OH to make changes to a script, and there are a lot of scheduling options. With your Python experience, I would think you’d love to get away from the Rules DSL and into Jython.

omg I’ve been dying to get away from it for a long time… so much head banging and horrible debugging (and that often on a super fast rpi2 :wink: …though I must admit that with vcode etc it has become easier/better)… so yes definitely.
I will give this a try then thanks for the info!

It will probably not solve all my use-cases as I use it for forwarding states from custom IoT devices, small scheduled jobs which are only used sporadically etc…

I guess if I would be moving to jython though I would have to move away from my rpi2 sooner than anticipated; we’ll see.

Not yet! I am wrapping some things up in the PR before I put it into the forum for feedback. Definitely by the end of the weekend.

JSR223 can be used for much more than rules/automation. You can create/remove/modify items, monitor folders, there’s even the potential to write an entire binding. BTW, the repo is not mine… it’s shared and open to anyone wanting to move JSR223-Jython further in OH. All credit goes to our benevolent benefactor @steve1, who put together pretty much everything you see in that repo, which lucid is based on.

You can run both at the same time too. Helps with transitioning.

I bet you will find that it does!

Why? There shouldn’t be any need to upgrade hardware. If anything, I see better performance and less load than with the Rules DSL.

Ok sry, did only a quick browsing.
Hmm is there any code already for creating bindings or is this just a theoretical thing for now ?

If I was to create a binding, sure. Otherwise I don’t see how?
In one case I need to open a serial device and keep polling it.
Not sure how that would be doable?

Really? I would not have expected that quite frankly. I have a bunch of extensions running and quite some rules. So the system is nicely responsive but I see nearly 100% CPU all the time… RAM is also used a lot.
But OK that motivates me even more now :stuck_out_tongue:

Unfortunately, there is still a lot that needs to be be tested and updated with the API changes. This is definitely on my to-do list… I’d love to see a full blown Jython binding. I haven’t tried to use it, but here’s something…

Whatever you are doing now in Python, you could do in Jython. Possibly with the same code, and the OH integration is already taken care of for you.

I need to qualify this… a migration from DSL Rules to Jython should be at least the same performance. I don’t have any stats, but my perception is that my system is performing better with less load. If you’re adding in a bunch of load from some other Python scripts on top of OH, you very well may need a hardware upgrade! :wink:

@sim0nx
I have quesion about script, becuase I receive an error. Previsuly I used your lib on OH1 without any problem. After migration I can not make it work. I based on python 2.7, so this my script:

import openhab
import sys
import os
import energymeter
import math
import struct

DEVICE_0="/dev/ttyUSB0"
ADDR=1

base_url = 'localhost:8080/rest'
#openhab = openHAB(base_url)


def mem2float(reg1, reg2):
    raw = struct.pack('>HH', reg1, reg2)
    return struct.unpack('>f', raw)[0]

#print "testing @ ",DEVICE_0
testEmeter=energymeter.testPort(DEVICE_0, ADDR)
if testEmeter:
	mbcli = energymeter.startClient(DEVICE_0, ADDR)
	regs=energymeter.readRegs(mbcli, 0xE,6)

Vol1 = openhab.get_item(base_url, 'Voltage_L1')
Vol1.state = round(mem2float(regs[0], regs[1]),1)

where in items:
Number Voltage_L1 "Voltage L1 [%.1f V]" <energy> (Energy)

Under 2.2v of your lib I got:

Traceback (most recent call last):
  File "VL1.py", line 60, in <module>
    Vol1 = openhab.get_item(base_url, 'Voltage_L1')
AttributeError: 'module' object has no attribute 'get_item'

I tried with 2.8 and 2.1 but no luck.
Any idea what I need to change after migration from OH1->OH2 ?

@sim0nx

i also used your plugin with openhab 1.

now trying to convert to openhab 2 using openhabian raspberry pi build.

i followed the instructions for install from your github page and built a small test program coming up with the same error as my main.

[12:14:46] openhabian@openHABianPi:~$ sudo python test.py
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from openhab import openHAB
  File "build/bdist.linux-armv7l/egg/openhab/__init__.py", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/python_openhab-2.8-py2.7.egg/openhab/client.py", line 39
    def __init__(self, base_url: str,
                               ^
SyntaxError: invalid syntax

my little program is this

from openhab import openHAB

base_url = 'http://localhost:8080/rest'
openhab = openHAB(base_url)

# fetch all items
items = openhab.fetch_all_items()

Hi sherif,

You need to fix your import and class initialisation, which has changed:

from openhab import openHAB

base_url = 'localhost:8080/rest'
openhab = openHAB(base_url)

That should do it.
Please report back (preferably on github → GitHub - sim0nx/python-openhab: python library for accessing the openHAB REST API) if you still have issues.

Georges

Hi Matt_D,

Your code looks find, but I no longer support Python2. I started using some features which are no available in older version of Python, especially Python2.

Hence your script should run fine if you just run it with python3 (apt-get install python3).

Please let me know if you still have issues.

So
I had mistake in url (I lost http://) and I have removed first argument from get.item() + import as you suggested.
Now it works !

Thank You !

Great! :slight_smile:

Is there a last version of the library that do support python 2.7 available somehow?
Python3 works great with it but I’m limited by the E-ink display driver and have to use python2.

The last version to support PY2 is v2.4. You can find it on pypi:

@sim0nx Do you know if your library will work with OpenHab 3 as well?

@Markus1 I do use it on OH3, so yes it does work. Make sure to use the latest version though as I had to make a small fix for OH3.

Thank you very much for the quik response. I will try it!