Python 3.x in OH3 Rules

Hi,
I’ve been searching for a definitive answer, but only found confusing references to Jython and other stuff.
Is it possible to write Rules files purely in Python 3.x (based on what is installed on device or via OH).
Are there any caveats, supported/unsupported libraries or example scripts I can use?

I’m familiar with Python 3.6-3.8 as I do a lot of AWS Lambda work, so would rather not have to be learning yet more languages.

Thanks,
Richie

You might want to check out HABapp. It’s a python 3 system to build rules and scripts that works though the OH API. Most users who want an up to date python solution seem to gravitate to that. Just search the forums for HABapp and you’ll see all the work spaceman_spiff has put into it.

3 Likes

Hi,
Just had a go installing it on my Windows system.
However, getting this in the log:

[2021-08-15 21:08:28,521] [                   HABApp]    ERROR | Error Invalid value for ContactItem: OFF in on_sse_event:
[2021-08-15 21:08:28,521] [                   HABApp]    ERROR | File "F:\Apps\habapp\lib\site-packages\HABApp\core\wrapper.py", line 130, in f
[2021-08-15 21:08:28,521] [                   HABApp]    ERROR |     return func(*args, **kwargs)
[2021-08-15 21:08:28,521] [                   HABApp]    ERROR | File "F:\Apps\habapp\lib\site-packages\HABApp\openhab\connection_logic\connection.py", line 48, in on_sse_event
[2021-08-15 21:08:28,521] [                   HABApp]    ERROR |     38   def on_sse_event(event_dict: dict):
[2021-08-15 21:08:28,522] [                   HABApp]    ERROR |  (...)
[2021-08-15 21:08:28,522] [                   HABApp]    ERROR |     44       # so the items have the correct state when we process the event in a rule
[2021-08-15 21:08:28,522] [                   HABApp]    ERROR |     45       try:
[2021-08-15 21:08:28,522] [                   HABApp]    ERROR |     46           if isinstance(event, HABApp.core.events.ValueUpdateEvent):
[2021-08-15 21:08:28,522] [                   HABApp]    ERROR |     47               __item = Items.get_item(event.name)  # type: HABApp.core.items.base_item.BaseValueItem
[2021-08-15 21:08:28,522] [                   HABApp]    ERROR | --> 48               __item.set_value(event.value)
[2021-08-15 21:08:28,522] [                   HABApp]    ERROR |     49               HABApp.core.EventBus.post_event(event.name, event)
[2021-08-15 21:08:28,522] [                   HABApp]    ERROR |     ..................................................
[2021-08-15 21:08:28,523] [                   HABApp]    ERROR |      event_dict = {'topic': 'openhab/items/HallEnvironment_HallSmoke/state',
[2021-08-15 21:08:28,523] [                   HABApp]    ERROR |                    'payload': '{"type":"OnOff","value":"OFF"}',
[2021-08-15 21:08:28,523] [                   HABApp]    ERROR |                    'type': 'ItemStateEvent'}
[2021-08-15 21:08:28,523] [                   HABApp]    ERROR |      event = <ItemStateEvent name: HallEnvironment_HallSmoke, value: OFF>
[2021-08-15 21:08:28,523] [                   HABApp]    ERROR |      HABApp.core.events.ValueUpdateEvent = <class 'HABApp.core.events.events.ValueUpdateEvent'>
[2021-08-15 21:08:28,523] [                   HABApp]    ERROR |      __item = <ContactItem name: HallEnvironment_HallSmoke, value: None, last_change: 2021-08-15T20:55:11.064056, last_update: 2021-08-15T21:05:43.893986>
[2021-08-15 21:08:28,523] [                   HABApp]    ERROR |      Items.get_item = <function 'get_item' Items.py:24>
[2021-08-15 21:08:28,523] [                   HABApp]    ERROR |      event.name = 'HallEnvironment_HallSmoke'
[2021-08-15 21:08:28,523] [                   HABApp]    ERROR |      __item.set_value = <method 'ContactItem.set_value' of # error calling str contact_item.py:7>
[2021-08-15 21:08:28,523] [                   HABApp]    ERROR |      event.value = <HABApp.openhab.definitions.values.OnOffValue object at 0x05C8D310>
[2021-08-15 21:08:28,524] [                   HABApp]    ERROR |     ..................................................
[2021-08-15 21:08:28,524] [                   HABApp]    ERROR | 
[2021-08-15 21:08:28,524] [                   HABApp]    ERROR | File "F:\Apps\habapp\lib\site-packages\HABApp\openhab\items\contact_item.py", line 13, in set_value
[2021-08-15 21:08:28,524] [                   HABApp]    ERROR |     7    def set_value(self, new_value) -> bool:
[2021-08-15 21:08:28,524] [                   HABApp]    ERROR |  (...)
[2021-08-15 21:08:28,524] [                   HABApp]    ERROR |     9        if isinstance(new_value, OpenClosedValue):
[2021-08-15 21:08:28,524] [                   HABApp]    ERROR |     10           new_value = new_value.value
[2021-08-15 21:08:28,524] [                   HABApp]    ERROR |     11   
[2021-08-15 21:08:28,525] [                   HABApp]    ERROR |     12       if new_value is not None and new_value != OpenClosedValue.OPEN and new_value != OpenClosedValue.CLOSED:
[2021-08-15 21:08:28,525] [                   HABApp]    ERROR | --> 13           raise ValueError(f'Invalid value for ContactItem: {new_value}')
[2021-08-15 21:08:28,525] [                   HABApp]    ERROR |     14       return super().set_value(new_value)
[2021-08-15 21:08:28,525] [                   HABApp]    ERROR |     ..................................................
[2021-08-15 21:08:28,525] [                   HABApp]    ERROR |      self = <ContactItem name: HallEnvironment_HallSmoke, value: None, last_change: 2021-08-15T20:55:11.064056, last_update: 2021-08-15T21:05:43.893986>
[2021-08-15 21:08:28,525] [                   HABApp]    ERROR |      new_value = <HABApp.openhab.definitions.values.OnOffValue object at 0x05C8D310>
[2021-08-15 21:08:28,525] [                   HABApp]    ERROR |      OpenClosedValue = <class 'HABApp.openhab.definitions.values.OpenClosedValue'>
[2021-08-15 21:08:28,525] [                   HABApp]    ERROR |      new_value.value = 'OFF'
[2021-08-15 21:08:28,525] [                   HABApp]    ERROR |      OpenClosedValue.OPEN = 'OPEN'
[2021-08-15 21:08:28,525] [                   HABApp]    ERROR |      OpenClosedValue.CLOSED = 'CLOSED'
[2021-08-15 21:08:28,526] [                   HABApp]    ERROR |     ..................................................
[2021-08-15 21:08:28,526] [                   HABApp]    ERROR | 
[2021-08-15 21:08:28,526] [                   HABApp]    ERROR | ValueError: Invalid value for ContactItem: OFF
[2021-08-15 21:08:37,091] [HABApp.openhab.connection]  WARNING | Status 404 for PUT http://localhost:1980/rest/items/HABApp_Ping/state/ NULL
[2021-08-15 21:08:47,114] [HABApp.openhab.connection]  WARNING | Status 404 for PUT http://localhost:1980/rest/items/HABApp_Ping/state/ NULL

So not sure if it’s actually working on my system :frowning:
Is there a specific forum/section I can go to to get help on installation and configuration of HabApp?

Thanks,
Richie

HabApp is covered by the following thread. The OP gives the link to a website with documentation on installation and libraries.
There are some limitations that may or may not affect you, as HabAPP is based on the REST API. There are several threads about the different approaches to automation. One that deals with Jython vs Habapp is this one .

1 Like

Hi,
Cheers for that.
I’ve removed some unused things/items and now my HabApp log file is looking much healthier.
Just got this error, but not sure if it’s something I can ignore for the moment:

[2021-08-15 21:42:06,196] [HABApp.openhab.connection]  WARNING | Status 404 for PUT http://localhost:1980/rest/items/HABApp_Ping/state/ NULL

Which I’ve now fixed by creating an OpenHab item “HABApp_Ping”
Time to look at writing my first “Hello World” script to test :smiley:

Thanks,
Richie

You can always look at the documentation.
I really recommend reading it thoroughly because it explains lots of things.
There is a dedicated section that describes the HABApp functionality so you’ll get a glimpse of how everything works and what is possible and what now.

1 Like

Just to add a few definitive answers.

As of this writing there is no native way to write “pure” rules in Python 3. HABApp is your best bet for that.

Jython only has a release for Python 2.7. A release for Python 3 is in doubt.

Perhaps there will be someone who picks up GraalVM Python at some point (there is already a GraalVM JavaScript addon if some intrepid developer wants to take a shot at it and needs an example) we’ll have a Python 3 that runs natively.