Skip to main content

Openhab python rule engine

Project description

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

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openhab_pythonrule_engine-1.2.23.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

openhab_pythonrule_engine-1.2.23-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

Details for the file openhab_pythonrule_engine-1.2.23.tar.gz.

File metadata

File hashes

Hashes for openhab_pythonrule_engine-1.2.23.tar.gz
Algorithm Hash digest
SHA256 79d92ce8185dd5ab6182ade355482ebf63c028704759efdccbb5c3025baa2d8e
MD5 2e7b617e1c05ca7a1e81d52593c5f9ea
BLAKE2b-256 189a6176d9e74df5a1b59f17551f0057ef8b4fec3c0a00258ba2649a105c15e7

See more details on using hashes here.

File details

Details for the file openhab_pythonrule_engine-1.2.23-py3-none-any.whl.

File metadata

File hashes

Hashes for openhab_pythonrule_engine-1.2.23-py3-none-any.whl
Algorithm Hash digest
SHA256 fd2447a3e57f620f7b720129a7807c52d135e4119d48abf7bc25caae436e6fc3
MD5 17bf5ad850917876de56939e555f71f4
BLAKE2b-256 a55819f866d6d5f4b1ac758f7e060ab3d7823d16f25ac6ba3ba19486d6a69b77

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page