# Rules Engine

Source: https://autoarm.rhizomatics.org.uk/configuration/advanced/rules_engine/

## Automated Transitions

YAML-only

Transition conditions are configured entirely in YAML.

If nothing else is configured ( occupancy, buttons, calendars ) then arming will still happen by the state of the sun. The rules for this, and how occupancy is used, are all defined as Home Assistant [Conditions] and can be overridden as you need.

| Diurnal State | Occupancy State | Alarm State    |
| ------------- | --------------- | -------------- |
| day           | occupied        | ARMED_HOME(\*) |
| day           | empty           | ARMED_AWAY     |
| night         | occupied        | ARMED_NIGHT    |
| night         | occupied        | ARMED_AWAY     |

(\*) This can be overridden using `state_default` in the `occupancy` configuration, for example if you prefer to have the alarm set to `disarmed` when people are home and its daylight.

Two other states, `armed_vacation` and `disarmed` can be set manually, by buttons, or calendar.

If you need more predictability, especially for high latitudes where sunrise varies wildly through the year, set up a calendar and define exactly when you want disarming or arming to happen.

### Algorithm Conditions

The defaults below will be used if there is no transition defined ( you can override just one of them if you prefer, and the others will remain as default, leave `conditions` empty if you really want to disable the transition).

```yaml
autoarm:
  transitions:
    armed_home:
        - "{{ autoarm.occupied and not autoarm.night }}"
        - "{{ autoarm.computed and autoarm.occupied_daytime_state == 'armed_home'}}"
    armed_away: "{{ not autoarm.occupied and autoarm.computed}}"
    disarmed:
        - "{{ autoarm.occupied and not autoarm.night }}"
        -  "{{ autoarm.computed and autoarm.occupied_daytime_state == 'disarmed'}}"
    armed_night: "{{ autoarm.occupied and autoarm.night and autoarm.computed}}"
    armed_vacation: "{{ autoarm.vacation }}"
```

Conditions have an `autoarm` field added to the context, with these values. The examples above are all in the [shortcut template style](https://www.home-assistant.io/docs/scripts/conditions/#template-condition-shorthand-notation), though any other style of `condition` can be used, along with other Jinja2 features and Home Assistant extras, including AND/OR/NOT logic.

| Field                        | Type            | Usage                                                                           |
| ---------------------------- | --------------- | ------------------------------------------------------------------------------- |
| daytime                      | bool            | The `sun` integration thinks it is daytime                                      |
| night                        | bool            | The `sun` integration thinks it is nighttime                                    |
| bedtime                      | bool            | A bedtime sensor or Schedule is on, or an `armed_night` calendar event is live  |
| after_bedtime                | bool            | Bedtime has ended since the sun last set                                        |
| programmed_bedtime           | bool            | There is a bedtime sensor or Schedule, or an `armed_night` calendar event today |
| occupied                     | bool            | If any of the `person` entities have state `home`                               |
| at_home                      | list[str]       | List of occupancy entities at home                                              |
| not_home                     | list[str]       | List of occupancy entities not at home                                          |
| manual                       | bool            | Alarm Panel is in vacation or 'custom bypass' mode                              |
| computed                     | bool            | State is being computed by the algorithm                                        |
| vacation                     | bool            | Shortcut for Alarm Panel state being ARMED_VACATION                             |
| bypass                       | bool            | Shortcut for Alarm Panel state being ARMED_CUSTOM_BYPASS                        |
| disarmed                     | bool            | Shortcut for Alarm Panel state being DISARMED                                   |
| state                        | str             | Current alarm control panel state                                               |
| calendar_event               | [CalendarEvent] | Most recent active Calendar Event                                               |
| calendar_event.start         | datetime        | Event start date/time                                                           |
| calendar_event.end           | datetime        | Event end date/time                                                             |
| calendar_event.summary       | str             | Event summary                                                                   |
| calendar_event.description   | str             | Event description                                                               |
| calendar_event.location      | str             | Event location                                                                  |
| occupied_daytime_state       | str             | Default state for occupied in day time                                          |
| occupied_nighttime_state     | str             | Default state for occupied between evening and bedtime                          |
| occupied_after_bedtime_state | str             | Default state for occupied between bedtime and morning                          |

The default conditions use `programmed_bedtime` and `after_bedtime` to pick the occupied state for the night: `armed_night` if no bedtime is programmed, `occupied_after_bedtime_state` once bedtime has ended, otherwise `occupied_nighttime_state`. Conditions of your own that replace the defaults need to do the same if they want that behaviour, as `occupied_nighttime_state` is now always the configured value.
