Skip to content

Install openHAB2 bindings using Ansible

Recently I started looking into Home Automation, using a Raspberry Pi and the openHAB as platform. The website provides ready-to-go images based on Debian, named openHABian. Me being me, I decided to install the image, let it boot up for the first time, and then take it from there using Ansible as automation tool. That's a bit more effort in the beginning, but ensures that I can always just wipe the SDcard, and start over from the beginning. Especially useful if I screw up at some point during testing.

One of the first steps to do is to decide which bindings, or extensions, are needed. Bindings provide openHAB with information about what kind of hardware and input sources are available out there. The list of available bindings is long, and one can use the UI to click and install bindings, before starting to configure and link them. That process is not idempotent, but luckily openHAB also provides an API which can be used to execute the same steps, just faster and automated.



The following Playbook uses the API to receive a list of available extensions/bindings, and then installs a number of extensions if they are not yet installed. Additionally a new filter "byattr" filter is required, based on this solution here. In my case I did not use "filter_plugins/" but instead placed the plugin in "filter_plugins/".

- name: Install required packages
  apt: name={{ item }} state=present
    - python-jinja2
    - python-jinja2-time
    - jq
    - curl

- name: Get list of available and installed extensions
    url: "http://{{ ansible_host }}:8080/rest/extensions"
  register: oh2_extensions
  changed_when: false

#- name: List extensions
#  debug:
#    msg: "{{ oh2_extensions.json }}"

#- name: List extensions
#  debug:
#    msg: "{{ }}"
#  loop: "{{ oh2_extensions.json }}"

- name: Install extensions
    url: "http://{{ ansible_host }}:8080/rest/extensions/{{ item }}/install"
    method: POST
  when: "not (oh2_extensions.json|byattr('id', item))[0].installed"
    - binding-chromecast
    - binding-logreader
    - binding-network
    - binding-yahooweather
    - binding-ipp
    - binding-feed
    - binding-exec
    - binding-astro
  register: oh2_install_extensions

The first "debug" statement dumps the entire extension list including all the other data. The second "debug" just dumps a list of available extensions, making it somewhat easier to read. Both debug options are commented out, but left there for convenience.

This Playbook installs a few additional packages, mainly jinja2 templates which are not preinstalled on the openHABian image, and a small tool named "jq" which I'm not directly using with Ansible, but in combination with "curl" to debug the API JSON output:

curl -s -X GET --header "Accept: application/json" "" | jq '.' | less

The "curl" line above fetches all available "things" from openHAB, pretty prints the output and lists everything in "less".


Now that all extensions are installed, it's time to rescan and rediscover new inbox items. Again that's possible using the API:

- block:
  - name: Get list of discoveries
      url: "http://{{ ansible_host }}:8080/rest/discovery"
    register: oh2_discoveries
    changed_when: false

#  - name: List discoveries
#    debug:
#      msg: "{{ oh2_discoveries }}"

  - name: Rediscover
      url: "{{ oh2_discoveries.url }}/bindings/{{ item }}/scan"
      method: POST
    loop: "{{ oh2_discoveries.json }}"

  when: oh2_install_extensions.changed

Fetch a list of all available discoveries, and then force openHAB to rediscover all of them - just in case. That's all wrapped in a block, and only run if the list of extensions changed. Otherwise it's not necessary to rediscover items.


ads' corner on : Auto-approve and link certain inbox items in openHAB using Ansible

Show preview
After starting to look into openHAB, and automate the installation with Ansible, I discovered that there is an API, but the main focus of the documentation and examples clearly is on using the UI and don't automate the installation. It takes some time to Comments ()

ads' corner on : Update openHAB Things configuration using Ansible

Show preview
In my earlier openHAB blog posts I automated the installation of extensions and linking of Channels to Items. Another problem I'm facing is the configuration of items. Again, that's very easy in the UI - if you are willing to click through the list of Ite Comments ()


Display comments as Linear | Threaded

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.
Form options