Automation


This page will go into more detail about the various options the automation component offers. If you haven’t yet, read the getting started page on automation.

A configuration section of an automation requires a trigger and an action section. condition and condition_type are optional. To keep this page compact, all following sections will not show the full configuration but only the relevant part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Example of a full entry in configuration.yaml
automation:
  alias: Light on in the evening
  trigger:
    - platform: sun
      event: sunset
      offset: "-01:00:00"
    - platform: state
      entity_id: group.all_devices
      state: home
  condition:
    - platform: state
      entity_id: group.all_devices
      state: home
    - platform: time
      after: 16:00:00
      before: 23:00:00
  action:
    service: homeassistant.turn_on
    entity_id: group.living_room

All configuration entries have to be sequential. If you have automation:, automation 2: and automation 4: then the last one will not be processed.

Triggers

Triggers are what starts the processing of an automation rule. It is possible to specify multiple triggers for the same rule. Once a trigger starts, Home Assistant will validate the conditions, if any, and call the action.

Event trigger

Triggers when an event is being processed. Events are the raw building blocks of Home Assistant. You can match events on just the event name or also require specific event data to be present.

1
2
3
4
5
6
7
automation:
  trigger:
    platform: event
    event_type: MY_CUSTOM_EVENT
    # optional
    event_data:
      mood: happy

MQTT trigger

Triggers when a specific message is received on given topic. Optionally can match on the payload being sent over the topic.

1
2
3
4
5
6
automation:
  trigger:
    platform: mqtt
    topic: living_room/switch/ac
    # Optional
    payload: 'on'

Numeric state trigger

On state change of a specified entity, attempts to parse the state as a number and triggers if value is above and/or below a threshold.

1
2
3
4
5
6
7
automation:
  trigger:
    platform: numeric_state
    entity_id: sensor.temperature
    # At least one of the following required
    above: 17
    below: 25

State trigger

Triggers when the state of an entity changes. If only entity_id given will match all state changes.

1
2
3
4
5
6
7
automation:
  trigger:
    platform: state
    entity_id: device_tracker.paulus
    # Optional
    from: not_home
    to: home

Use quotes around your values for state_from and state_to to avoid the YAML parser interpreting some values as booleans.

Sun trigger

Triggers based on sunrise and sunset, both with an optional offset.

1
2
3
4
5
6
7
automation:
  trigger:
    platform: sun
    # Possible values: sunset, sunrise
    event: sunset
    # Optional time offset
    offset: -00:45:00

Time trigger

Time can be triggered in many ways. The most common is to specify after and trigger at a specific point in time each day. Alternatively, you can also match if the hour, minute or second of the current time has a specifc value. For example, by only setting minutes in the config to 5 it will trigger every hour when it is 5 minutes past whole.

1
2
3
4
5
6
7
8
9
10
automation:
  trigger:
    platform: time
    # All following are optional.
    # When 'after' is used, you cannot also match on hour, minute, seconds.
    # Military time format.
    after: 15:32:00
    hours: 10
    minutes: 5
    seconds: 0

Conditions

Conditions are an optional part of an automation rule and be used to prevent an action from happening when triggered. Conditions look very familiar to triggers but are very different. A trigger will look at events happening at the system while a condition only looks at how the system looks right now. A trigger can observe that a switch is being turned on. A condition can only see if a switch is on or off.

An automation rule can have mulitiple triggers. By default the action will only fire if all conditions pass. An optional key condition_type: 'or' can be set on the automation rule to fire action if any condition matches.

1
2
automation:
  condition_type: or

If your triggers and conditions are exactly the same, you can use a shortcut to specify conditions. In this case, triggers that are not valid conditions will be ignored.

1
2
automation:
  condition: use_trigger_values

Numeric state condition

Attempts to parse the state of specified entity as a number and triggers if value is above and/or below a threshold.

1
2
3
4
5
6
7
automation:
  condition:
    platform: numeric_state
    entity_id: sensor.temperature
    # At least one of the following required
    above: 17
    below: 25

State condition

Tests if an entity is a specified state.

1
2
3
4
5
automation:
  condition:
    platform: state
    entity_id: device_tracker.paulus
    state: not_home

Time condition

The time condition can test if it is after a specified time, before a specified time or if it is a certain day of the week.

1
2
3
4
5
6
7
8
9
10
automation:
  condition:
    platform: time
    # At least one of the following is required.
    after: 15:00:00
    before: 23:00:00
    weekday:
      - mon
      - wed
      - fri

Actions

When an automation rule fires, it calls a service. For this service you can specify an entity id it should apply to and optional service parameters (to specify for example the brightness).

1
2
3
4
5
6
7
8
9
10
automation:
  # Change the light in the kitchen and living room to 150 brightness and color red.
  action:
    service: homeassistant.turn_on
    entity_id:
      - light.kitchen
      - light.living_room
    data:
      brightness: 150
      rgb_color: [255, 0, 0]
1
2
3
4
5
6
automation:
  # Notify me on my mobile phone of an event
  action:
    service: notify.notify
    data:
      message: Something just happened, better take a look!

If you want to specify multiple services to be called or include a delay, have a look at the script component. If you want to describe how certain entities should look, check out the scene component.