This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Home Assistant

How home assistant configurations are managed.

Declarative home assistant configurations

Declarative ESP Home Firmware

Functional Component State vs Impeditive Event Effects

Traditionally home assistant automation are driven via events pushing changes to devices via automatons. While this is a fine solution for a simple automation such as “when motion detected turn on light” it starts to be very difficult to maintain once you have a wide set of entities that impact the state of another entity. Think of something like: “motion controlled lights that have a switch to manually turn them on/off that automatically turn off when no one is home with brightnesses determined by time of day and a toggle to temporally disable all automation”. That would be a very complex problem to solve and would require a lot of redundant logic in the triggers for every single event type that we want our device to subscribe to.

Our solution to this is state driven automation. This means that every single device state that we want to automatically control gets exactly 1 canonical automation that controls it. Then the triggers for the state update is set to listen to every single phase transition edge in the phase space diagram of the state. Then the body of the automation is a logic tree that implements that phase space diagram.

Managing this manually via the UI can be complex because you need to make sure that you keep the implementation and triggers of your phase diagram in sync. To help with this we use some nix tooling to automatically generate the triggers. Then on activation our nix config will automatically upload the automation and replace the existing canonical automation with the new state.

Here is the light from above. The devices it uses (hallway_light_switch, automation_disabled and pixel_10) are assumed to already be declared in the inventory. The state is declared once, as a tree, and the triggers are derived from every entity the tree reads.

{config, ...}: let
  inv = config.services.home-assistant.inventory.devices;

  sun = inv.sun;
  motion_sensor = inv.motion_sensor;
  room_locked = inv.motion_sensor;
	phone_tracker = inv.phone.entities.tracker
in {
  services.home-assistant.inventory.devices = {
    hallway_light_switch.entities.switch.state.when = {
      cond.state = {
        entity = room_locked;
        is = "on";
      };
      do.value = null;
      otherwise.when = {
        cond.state = {
          entity = phone_tracker;
          is = "not_home";
        };
        do.value = "off";
        otherwise.when = {
          cond.state = {
            entity = motion_sensor;
            is = "on";
          };
          do.when = {
            cond = {
							entity = sun;
							is = "below_horizon";
						};
            do.value = {
              state = "on";
              brightness_pct = 30;
            };
            otherwise.value = {
              state = "on";
              brightness_pct = 100;
            };
          };
          otherwise.value = "off";
        };
      };
    };
  };
}