# Common Home Assistant Automation Patterns

## Motion-Activated Lighting

Turn on lights when motion detected, turn off after no motion:

```yaml
automation:
  - alias: "Motion Light - Hallway"
    id: motion_light_hallway
    trigger:
      - trigger: state
        entity_id: binary_sensor.hallway_motion
        to: "on"
    condition:
      - condition: state
        entity_id: sun.sun
        state: "below_horizon"
    action:
      - action: light.turn_on
        target:
          entity_id: light.hallway
      - wait_for_trigger:
          - trigger: state
            entity_id: binary_sensor.hallway_motion
            to: "off"
            for:
              minutes: 5
      - action: light.turn_off
        target:
          entity_id: light.hallway
```

## Time-Based Automation

Execute actions at specific times:

```yaml
automation:
  - alias: "Morning Routine"
    id: morning_routine
    trigger:
      - trigger: time
        at: "07:00:00"
    condition:
      - condition: state
        entity_id: binary_sensor.workday
        state: "on"
    action:
      - action: light.turn_on
        target:
          entity_id: light.bedroom
        data:
          brightness: 50
      - action: cover.open_cover
        target:
          entity_id: cover.bedroom_blinds
      - action: climate.set_temperature
        target:
          entity_id: climate.bedroom
        data:
          temperature: 21
```

## Sunset/Sunrise Automation

React to sun position:

```yaml
automation:
  - alias: "Lights at Sunset"
    id: lights_at_sunset
    trigger:
      - trigger: sun
        event: sunset
        offset: "-00:30:00"  # 30 min before sunset
    action:
      - action: light.turn_on
        target:
          area_id: living_room
        data:
          brightness: 180
```

## Presence Detection

React to people arriving/leaving:

```yaml
automation:
  - alias: "Welcome Home"
    id: welcome_home
    trigger:
      - trigger: state
        entity_id: person.john
        from: "not_home"
        to: "home"
    action:
      - action: light.turn_on
        target:
          area_id: entrance
      - action: climate.set_temperature
        target:
          entity_id: climate.living_room
        data:
          temperature: 22
      - action: notify.mobile_app
        data:
          message: "Welcome home, John!"
```

## Temperature-Based Climate Control

Adjust heating/cooling based on temperature:

```yaml
automation:
  - alias: "Auto Climate Control"
    id: auto_climate_control
    trigger:
      - trigger: numeric_state
        entity_id: sensor.living_room_temperature
        below: 19
    condition:
      - condition: state
        entity_id: person.john
        state: "home"
    action:
      - action: climate.set_hvac_mode
        target:
          entity_id: climate.living_room
        data:
          hvac_mode: heat
      - action: climate.set_temperature
        target:
          entity_id: climate.living_room
        data:
          temperature: 21
```

## Door/Window Alert

Notify when doors/windows left open:

```yaml
automation:
  - alias: "Window Open Alert"
    id: window_open_alert
    trigger:
      - trigger: state
        entity_id: binary_sensor.living_room_window
        to: "on"
        for:
          minutes: 30
    condition:
      - condition: numeric_state
        entity_id: sensor.outdoor_temperature
        below: 10
    action:
      - action: notify.notify
        data:
          message: "Living room window has been open for 30 minutes and it's {{ states('sensor.outdoor_temperature') }}°C outside"
          title: "Window Alert"
```

## Low Battery Notification

Alert when device batteries are low:

```yaml
automation:
  - alias: "Low Battery Alert"
    id: low_battery_alert
    trigger:
      - trigger: numeric_state
        entity_id: sensor.motion_sensor_battery
        below: 20
    action:
      - action: notify.notify
        data:
          message: "{{ trigger.to_state.attributes.friendly_name }} battery is at {{ trigger.to_state.state }}%"
          title: "Low Battery"
```

## Conditional Lighting by Time

Different brightness based on time of day:

```yaml
automation:
  - alias: "Motion Light with Time-Based Brightness"
    id: motion_light_time_based
    trigger:
      - trigger: state
        entity_id: binary_sensor.kitchen_motion
        to: "on"
    action:
      - choose:
          - conditions:
              - condition: time
                after: "22:00:00"
                before: "06:00:00"
            sequence:
              - action: light.turn_on
                target:
                  entity_id: light.kitchen
                data:
                  brightness: 30
          - conditions:
              - condition: time
                after: "06:00:00"
                before: "22:00:00"
            sequence:
              - action: light.turn_on
                target:
                  entity_id: light.kitchen
                data:
                  brightness: 255
```

## Vacation Mode

Randomize lights when away:

```yaml
automation:
  - alias: "Vacation Mode - Random Lights"
    id: vacation_mode_lights
    trigger:
      - trigger: time_pattern
        hours: "/1"  # Every hour
    condition:
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: "on"
      - condition: sun
        after: sunset
        before: sunrise
    action:
      - action: light.turn_on
        target:
          entity_id: "{{ ['light.living_room', 'light.bedroom', 'light.kitchen'] | random }}"
      - delay:
          minutes: "{{ range(30, 120) | random }}"
      - action: light.turn_off
        target:
          entity_id: all
```

## Media Player Automation

Pause media when phone rings:

```yaml
automation:
  - alias: "Pause Media on Phone Call"
    id: pause_media_phone_call
    trigger:
      - trigger: state
        entity_id: sensor.phone_state
        to: "ringing"
    action:
      - action: media_player.media_pause
        target:
          entity_id: all
```

## Garden Watering Schedule

Water garden based on weather and schedule:

```yaml
automation:
  - alias: "Garden Watering"
    id: garden_watering
    trigger:
      - trigger: time
        at: "06:00:00"
    condition:
      - condition: state
        entity_id: sensor.weather_forecast
        state: "sunny"
      - condition: numeric_state
        entity_id: sensor.outdoor_temperature
        above: 20
      - condition: numeric_state
        entity_id: sensor.soil_moisture
        below: 30
    action:
      - action: switch.turn_on
        target:
          entity_id: switch.garden_sprinkler
      - delay:
          minutes: 30
      - action: switch.turn_off
        target:
          entity_id: switch.garden_sprinkler
```

## Reminder Notifications

Remind to perform tasks:

```yaml
automation:
  - alias: "Trash Day Reminder"
    id: trash_day_reminder
    trigger:
      - trigger: time
        at: "20:00:00"
    condition:
      - condition: time
        weekday:
          - sun
    action:
      - action: notify.notify
        data:
          message: "Don't forget to put out the trash tonight!"
          title: "Trash Day Tomorrow"
```

## Smart Doorbell

Announce doorbell press and record:

```yaml
automation:
  - alias: "Doorbell Press"
    id: doorbell_press
    trigger:
      - trigger: state
        entity_id: binary_sensor.doorbell
        to: "on"
    action:
      - action: media_player.play_media
        target:
          entity_id: media_player.home_speaker
        data:
          media_content_id: "local/doorbell.mp3"
          media_content_type: "music"
      - action: camera.snapshot
        target:
          entity_id: camera.front_door
        data:
          filename: "/config/www/snapshots/doorbell_{{ now().strftime('%Y%m%d_%H%M%S') }}.jpg"
      - action: notify.mobile_app
        data:
          message: "Someone is at the door"
          data:
            image: "/local/snapshots/doorbell_{{ now().strftime('%Y%m%d_%H%M%S') }}.jpg"
```

## Energy Saving

Turn off devices when nobody home:

```yaml
automation:
  - alias: "Energy Saving - Away Mode"
    id: energy_saving_away
    trigger:
      - trigger: state
        entity_id: group.all_persons
        to: "not_home"
        for:
          minutes: 15
    action:
      - action: light.turn_off
        target:
          entity_id: all
      - action: climate.set_hvac_mode
        target:
          entity_id: all
        data:
          hvac_mode: "off"
      - action: media_player.turn_off
        target:
          entity_id: all
```
