Skip to main content

OpenHAB python rule engine

A Python 3.x rule engine for OpenHAB. This rule engine allows you to define rules using Python 3.x.

Please note that OpenHAB username/password authentication (basic authentication) must be activated. To do so, open API security settings and enable the advanced setting Allow Basic Authentication.

To run this software, you can use Docker or the PIP package manager, as shown below

Docker approach

sudo docker run -e openhab_uri=http://192.168.1.17:8080 -e user=me -e pwd=secret -v /etc/openhab2/automation/rules/python:/rules grro/pythonrule_engine 

PIP approach

sudo pip install openhab-pythonrule-engine
sudo pyrule --command listen --openhab_uri http://localhost:8080 --python_rule_directory /etc/openhab2/automation/rules/python --user me --pwd secret

In this case, the rules engine connects to the Openhab instance running on the local machine on port 8080. Also, the /etc/openhab2/automation/rules/python directory is used to search for python-based rules

If you are using a systemd-based Linux distribution, you can use the register command to register and start the rule engine as a systemd entity. This automatically starts the rule engine at boot time. Starting the Rule Engine manually with the listen command is no longer necessary.

sudo pyrule --command register --openhab_uri http://localhost:8080 --python_rule_directory /etc/openhab2/automation/rules/python --user me --pwd secret

Rules

To trigger a rule method, the @when decorator must be used. Currently the conditions listed below are supported

condition example description
cron @when('Time cron */1 * * * *') fires based on cron expression
item state change @when('Item PhoneLisaLastSeen changed') fires when the specified Item's State changes
item command @when('Item SelectDoorCam received command ON')
@when('Item SelectDoorCam received command OFF')
fires when the specified Item receives a Command

Example: my_rule.py (located within /etc/openhab2/automation/rules/python)

from openhab_pythonrule_engine.condition import when
from openhab_pythonrule_engine.item_registry import ItemRegistry


@when("Time cron */1 * * * *") # each minute
@when("Item PhoneLisaLastSeen changed")
@when("Item PhoneTimLastSeen changed")
def update_presence_based_on_phone_seen(item_registry: ItemRegistry):
    last_time_present = item_registry.get_state_as_datetime('LastDateTimePresence')
    for phone_name in item_registry.get_group_membernames('Phones'):
        last_seen = item_registry.get_state_as_datetime(phone_name)
        print(last_seen)
        if last_seen > last_time_present:
            last_time_present = last_seen
    item_registry.set_state('LastDateTimePresence', last_time_present)

# uncomment the code below for local debugging
#item_registry = ItemRegistry.new_singleton(openhab_uri="http://192.168.1.27:8080/", user="xxx", pwd="secret")
#update_presence_based_on_phone_seen(item_registry)

If the rule method defines a (single!) argument, the item_registry object is automatically injected. The item_registry object provides methods for getting and setting the item state. When setting the state, the data value is automatically converted to the item-specific data type

Metadata

Release files for openhab-pythonrule-engine 1.2.28

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openhab-pythonrule-engine 1.2.28
File Size Uploaded Provenance
openhab_pythonrule_engine-1.2.28.tar.gz 16.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for openhab-pythonrule-engine 1.2.28
File Interpreter ABI Platform
openhab_pythonrule_engine-1.2.28-py3-none-any.whl Python 3 none any Details

Total release size: 41.6 kB

Release files / openhab_pythonrule_engine-1.2.28.tar.gz

Download URL openhab_pythonrule_engine-1.2.28.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
77ee97ec5406798076896bfdef89df966fd021df3dd9a7912b9e76651f9daaf3
BLAKE2b-256 checksum
How to use checksums
8a1e1743c1fd9a58d37b5d500263b8e191f01aee9983a9869b65503e8f8d933f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / openhab_pythonrule_engine-1.2.28-py3-none-any.whl

Download URL openhab_pythonrule_engine-1.2.28-py3-none-any.whl
Size 25.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1c4fc75a6cc258aeeff86b7b12d0222071a78793d57621d14a5abb9c7426a786
BLAKE2b-256 checksum
How to use checksums
148430dd638a5797651f91917119463724f93233eb013ac302d3e5db7f66b91e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Security for openhab-pythonrule-engine 1.2.28

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.2.28 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page