Introducing Opsdroid Home Assistant

A Home Assistant plugin for Opsdroid

Jacob Tomlinson
opsdroid
Published in
3 min readJan 8, 2020

--

Opsdroid is an automation framework for building bots. It is built on an event flow model where events are created by an external service, often by a user saying something in a chat client. The events can then be parsed using various AI services to add additional context and finally user defined Python functions called Skills can be triggered as a result.

Home Assistant is a home automation application which allows you to bring all of your smart home devices together into one application. It is also built on an event loop where device states are updated in real time and can trigger automations. Automations in Home Assistant are defined in YAML and can become unwieldy when trying to set up complex automations.

Today I want to share the Opsdroid Home Assistant plugin, which allows you to bridge the two event loops so that state changes in Home Assistant can trigger Skills in Opsdroid and then Opsdroid can make service calls back to Home Assistant to change states of devices. This enables you to write automations in Python allowing you to define simpler and more readable automations.

Example

Automation to turn on an outside light for 60 seconds when motion is detected

Opsdroid uses a decorator model to define which skills should be called in what situations. The main Opsdroid project focuses on natural language, time and responding to webhooks.

The Home Assistant plugin allows you to trigger skills when entities change state in Home Assistant with the match_hass_state_changed decorator. There are also more specific decorators such as match_sunrise and match_sunset .

Automation to turn on an outside light off and on at sunrise/sunset

For more information on Opsdroid’s other matchers see the Opsdroid documentation.

Helper methods

To create an Opsdroid skill you usually start create a class which is based on opsdroid.skill.Skill . However for Opsdroid Home Assistant we have created an enhanced opsdroid_homeassistant.HassSkill which has many convenience methods to help control your Home Assistant instance.

These include self.turn_on and self.turn_off as seen in the examples above but there are also methods for updating entities, sending notifications, getting sun information, getting presence information and even using Home Assistant’s built in rendering engine to render templates.

For a full list of helper methods see the Opsdroid Home Assistant documentation.

Installation and configuration

Both Opsdroid and the Home Assistant plugin can be installed via pip, Opsdroid is also available as a Docker image.

pip install opsdroid opsdroid-homeassistant

Opsdroid’s configuration is done via a yaml file which can be opened by running opsdroid config edit . To connect Opsdroid to your Home Assistant instance you need to configure the Home Assistant connector with the url of your Home Assistant and a long lived access token. You can then configure your skills by pointing Opsdroid to the path to your Python scripts.

For more information on installing and configuring the Opsdroid Home Assistant plugin see the Getting Started guide.

Alternatives

You may be thinking that this functionality sounds very similar to AppDaemon and you would be right. I’ve been an AppDaemon user for a long time in my personal Home Assistant setup, but I’ve also been working on Opsdroid to help folks automate their DevOps workflows via natural language and chat. Over the holiday break I decided to write this plugin to help unite my Opsdroid world with my Home Assistant setup.

The main differences between Opsdroid and AppDaemon are around the way callbacks are registered and the way concurrency is handled. You can read more about this in the documentation.

If you are already an AppDaemon user and are happy with the experience then that’s great, it’s an awesome tool. However if you’re curious about writing asynchronous automations which are fast and readable, or you’re interested in building natural language pipelines to help automate via chat then I encourage you to give Opsdroid and the Home Assistant plugin a go.

Links

--

--