This is an old revision of the document!
Table of Contents
High Availability
To aid in overall system availability, my Home Assistant deployment operates as an Active/Standby pair. This is predicated on MQTT Clustering to enable states to be consistent between Home Assistant instances. This is coupled with maintaining a consistent configuration between the two instances using git.
Beyond this, a couple of small tricks are required in order to get two Home Assistant instances to place nicely together.
Variables
There are a number of settings which must be unique for each Home Assistant instance in a cluster. This includes:
- name
- hostname
- internal_url (and possibly external_url, depending on your setup)
Different methods exist for keeping these values unique within a common configuration, however a quick hack is to use the secrets file. Ordinarily the secrets.yaml file is used for storing passwords and secret keys in a single file and masking these values from the main configuration. This file can be used as a pseudo-variable system for storing values that must be configured uniquely for each instance.
secrets.yaml
# Tempates for hosts hostname: leonard partner: johnson name: Home google_report_state: true
configuration.yaml
homeassistant: ... name: !secret name ... internal_url: !secret internal_url
Active/Standby State
Only one instance must be active at a time. This requires a method for transferring control between instances. The simplest method for achieving this is a MQTT topic indicating which instance is active.
home/hass/active
This topic is used to populate a sensor that records the currently active instance:
sensor: .. platform: mqtt state_topic: "home/hass/active" name: partner expire_after: 65
The sensor will revert to unknown
after 65 seconds. Coupling the sensor with birth and will messages for Home Assistant, allows an active instance to hand-over control during shutdown.
mqtt: .. birth_message: topic: 'home/hass/active' payload: !secret hostname retain: 'false' will_message: topic: 'home/hass/active' payload: !secret partner retain: 'false' <code> Additionally, to cover the scenario for a sudden loss of service, such as a crash or an ungraceful shutdown a heartbeat automation is used. <code> automation: .. - id: '1583253390883' alias: HASS:Heartbeat description: '' trigger: - platform: time_pattern seconds: '0' condition: [] action: - data: payload: !secret hostname topic: home/hass/active service: mqtt.publish
This will tickle the heartbeat every 60 seconds, ensuring the active
topic does not expire.
Automations
The main purpose of clustering Home Assistant is to allow either instance to take over the execution of Automations. In general however, each automation should only be executed by one instance at a time. There are two methods for achieving this:
- Automation Conditions
- Disabling/Enabling Automations
Automation Conditions
This is arguably the simplest method for controlling automations. Each automation should have a condition set which checks the status of the active
sensor. If it's hostname matches the active sensor then the automation should run.
.. condition: - condition: state entity_id: sensor.active state: !secret hostname ..
This arrangement also allows for automations which should run regardless of whether the instance is active or not, or automations which run specifically when the instance is inactive. It does however require each automation to be modified with this condition, which may be onerous for an established setup.