Skip to content

Durable Automations

Leonard Sperry edited this page Feb 29, 2024 · 11 revisions

Creating automations with delayed actions that survive restarts can be difficult. HaKafkaNet makes it easy.

All durable automations implement the ISchedulableAutomation interface. They work by scheduling an automation to occur at a specific time instead of waiting for some time to elapse. There are some things to consider however, and this page covers those topics.

Event Timing

When HaKafkaNet starts, it reads a Kafa data stream from the beginning to ensure it has the latest information about the states of all entities in Home Assistant. As it is reading those state changes, it is aware of when those changes occurred in relation to both start up time and the latest items in the cache. For each reported state, it assigns an EventTiming which it will pass on to the automation manager and eventually your automation. See Event Timing for more details.

Past events (pre-startup)

If your automation is configured to be durable, it will be able to read state changes from before startup and respond appropriately to schedule the action to take. If the scheduled time occurred before start up, your automation can optionally take the action immediately. This is configured by setting the ShouldExecutePastEvents property on the automation. This is set to true by default in most cases, and is likely setting you want. However, there is one edge case to be aware of. If during restart, you also cleared your cache and Kafka topic compaction has not run, and your system has been down for a while, it could end up responding to several occurrences of state change.

Reschedulable

In most cases you do not want to reschedule an automation once it has been scheduled. For example, when the next sunrise is calculated, it does not need to be recalculated until the next sunrise occurs. There are however times where you might want to reschedule. See IScheduleableAutomation for details.

Creating Durable Automations

The simplest way to create a durable automation is to use either the IAutomationFactory or the IAutomationBuilder in your registry. For example, if you want to turn a light off after no motion has been detected for a certain amount of time, you could use the factory's prebuilt method for this:

    _factory.DurableAutoOffOnEntityOff(
        "light.my_light", "binary_sensor.my_motion_sensor", 
        TimeSpan.FromMinutes(10));

The factory also has a method for creating any durable automation called CreateDurable.

The automation builder also has a method for making automations durable:

    _builder.CreateSchedulable(true)
        .MakeDurable()
        // add conditions and execution
        .Build();

Creating a durable automation from scratch

If you wish to create a reusable durable automation, you have two options:

  • Create a class that derives from the SchedulableAutomationBase class
  • Create a class that implements ISchedulableAutomation and set a few properties accordingly.

The example app has examples of both here and here..