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 defining rule by using python 3.x.

Please consider that the OpenHAB username/password auhentication (basic authentication) needs to be enabled. To do this, open API security settings and activate advanced setting Allow Basic Authentication.

To run this software you may use Docker or PIP package manager such 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

After this installation you may start the rule engine inside your python code or via command line using

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

Here, the rule engine will connect the openhab instance running on the local machine on port 8080. Furthermore, the directory /etc/openhab2/automation/rules/python will be used to scan for python-based rules

By running a systemd-based Linux distribution you may use the register command to register and start the rule engine as systemd unit. By doing this the rule engine will be started automatically on boot. Starting the rule engine manually using 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 methode the @when decorator has to 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 will be injected automatically. The item_registry provides methods to get and set the item state. By setting the state the data value will be auto converted into 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.1.16.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openhab_pythonrule_engine-1.1.16-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for openhab_pythonrule_engine-1.1.16.tar.gz
Algorithm Hash digest
SHA256 8fd2189a097c0ca3d146deb6ebb7ae42199472e05b57be2cfc259a5fd7e4c2c5
MD5 fef6215fdc94d0fa3937eb22cf7d2620
BLAKE2b-256 44b6fd05ad351ea22ca5fc31ba93e670c70e4bb1dc72fb8a8f730b38fc693d97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openhab_pythonrule_engine-1.1.16-py3-none-any.whl
Algorithm Hash digest
SHA256 c32b782c0003e9a9720ce9c87903b023a2f1cad671c844dd060522ab8d19258a
MD5 a8f287a1f8c801d7a14bc8b7d5340a1a
BLAKE2b-256 efa874608df1dc50ab328539f2e2f2f6a3066b4ddfc36217e42eff6894052ed1

See more details on using hashes here.

Supported by

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