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 in the org.openhab.restauth settings

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 server 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 will be used. Currently

  • Cron expressions are supported as well as
  • Item change trigger as shown below

If the method defines a (single!) argument, the item_registry object will be injected automatically. The item_registry cann be used to get item state as well as to update item state. By setting the state the data value will be auto converted into the item specific data type

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 * * * *')  # every 1 minute
@when('Item PhoneLisaLastSeen changed')
@when('Item PhoneJoeLastSeen changed')
def update_persence_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)
        if last_seen > last_time_present:
            last_time_present = last_seen
    item_registry.set_state('LastDateTimePresence', last_time_present)

Project details


Release history Release notifications | RSS feed

This version

1.0

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.0.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

openhab_pythonrule_engine-1.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: openhab_pythonrule_engine-1.0.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for openhab_pythonrule_engine-1.0.tar.gz
Algorithm Hash digest
SHA256 1d1e4048fa17701b787ee011621a61cd2f03fe16eca995663267629662988110
MD5 db6a7ce520ceb158db78521956011f0d
BLAKE2b-256 bca77db539395d4a6fced0fbe751d5c003d75cb2f42bfe8d5673b59d565c8426

See more details on using hashes here.

File details

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

File metadata

  • Download URL: openhab_pythonrule_engine-1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for openhab_pythonrule_engine-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9cc63e7481244709f9613408fba3e004ac55d2a2bb2b6db04cb4d62bd0cf1ae
MD5 a94da09bc2b0e89b4d1ae5382d2dc6eb
BLAKE2b-256 9b379662251a43089d6ea3a4296bbf1d72ec64a4cf2a5bbad2e9135a5bc728c3

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