Automated Integrity Maintenance - Home Assistant Declarative Toolkit Custom Component

I want to introduce the community to the Home Assistant Declarative Toolkit. The Declarative Toolkit is a Home Assistant integration that extends Home Assistant with some declarative features that enables symbolic AI and automated reasoning.

Currently implemented is the automated Integrity Maintenance through Answer Set Programming. You give it a Python expression that you want to be true at all times, like states('binary_sensor.it_is_dark') == states('light.light1'), but also more complex invariants.

invariant1

Why is that useful? In this specific case, this is quite easily achieved using the existing automations that are built on the theory of Event-Condition-Action rules (ECA-rules). We can react to the events “sensor goes on” and “sensor goes off” and the actions are quite clear: “turn light on” or “turn light off”. But in more complex situations we need to react to many events. And it is not always clear which actions to take in order to “repair” our situation, especially if there are multiple possible actions. This devolves into very complex and deeply nested conditionals that are difficult to reason about. Also, for a smart home, that’s not very smart.

We use decades old techniques of symbolic AI to bring some additional smarts to the smart home.

How does it work? You give the integrity maintenance module a Python expression using the states/is_state/has_state functions as you’re used to in your templates. The Python expression is parsed, transformed according to some rules, and then evaluated against Home Assistant’s current state. Each invariant will add a sensor, tracking the value of said invariant. Each invariant will also add a switch that allows us to disable enforcement/maintenance of said invariant. If enforcement is enabled and the sensor shows the invariant in need of maintaining, the transformed Python expression is converted to goal clauses of a logic program, specifically an Answer Set Program. Facts for the states and domains for the used entities are added to the program, as are some general rules about how entity states change depending on called services. We feed the Answer Set Program into clingo and get possible services to call in order to achieve the desired goal of restoring the invariant.

Though I have an academic background and the ideas are very much shaped by my research, this is not just a proof of concept. I plan to use and maintain this extension and hope other people find the declarative semantics useful. Declarative descriptions should be much clearer and much easier to reason about than the alternative.