My Home Assistant automation samples for your own use / feedback

Hello all,

This forum has been tremendously helpful to me when it comes to setting up my automations, so much so that I thought I should share them here. Most of the automations are pretty basic as I am still learning it, but I thought I should put them here in case some of you suggest some improvements to them or simply utilise them in their own automations.

Before I start, I would appreciate your improvement suggestions, such as:

  • Can I combine some of the repetitive automations into one?
  • Is there any other way you would configure the automations below differently - maybe with less rules / characters. I am not sure if the way I set up my automations is the most optimum way.

I will be listing the automation first and the description of that automation second.

Here we go…

#########################################################
#                                                       #
#             TURN THINGS OFF AT MIDNIGHT               #
#                                                       #
#########################################################
- id: midnight_turn_off
  alias: Turn things off at Midnight
  initial_state: True
  trigger:
    platform: time
    at: '00:00:00'
  action:
    - service: light.turn_off
      data:
        entity_id: light.christmas_lights
    - service: notify.ios_jack_iphone
      data:
        title: "Home Assistant"
        message: "Christmas Lights are turned off."

The above automation simply turns one of my lights at midnight and then sends a push notification to my iPhone.

#########################################################
#                                                       #
#        REDUCE BRIGHTNESS OF LIGHTS AT MIDNIGHT        #
#                                                       #
#########################################################
- id: reduce_brightness_of_lights_at_midnight
  alias: Reduce brightness of lights at midnight
  initial_state: True
  trigger:
    platform: time
    at: '00:00:00'
  action:
    - service: light.turn_on
      data:
        entity_id: light.kitchen_led
        rgb_color: [0,0,255]
        transition: 1
        brightness_pct: 25
    - service: light.turn_on
      data:
        entity_id: light.kitchen_table_led
        rgb_color: [0,0,255]
        transition: 1
        brightness_pct: 25
    - service: light.turn_on
      data:
        entity_id: light.balcony_led
        rgb_color: [0,0,255]
        transition: 1
        brightness_pct: 25
    - service: light.turn_on
      data:
        entity_id: light.xiaomi_gateway_office
        rgb_color: [255,0,0]
        transition: 1
        brightness_pct: 25
    - service: light.turn_on
      data:
        entity_id: light.office_yeelight_strip
        rgb_color: [255,0,0]
        transition: 1
        brightness_pct: 25
    - service: light.turn_on
      data:
        entity_id: light.xiaomi_gateway_hallway
        rgb_color: [0,0,255]
        transition: 1
        brightness_pct: 25
    - service: notify.ios_jack_iphone
      data:
        title: "Home Assistant"
        message: "Kitchen LED, Kitchen Table LED, Gateways and Office LED brightness reduced to 25%."

The above automation reduces the brightness on some of my devices at midnight. Previously I tried using only the brightness_pct command without using the rgb_color but I noticed with some of my LED light strips, it was changing the colour.

#########################################################
#                                                       #
#                TURN THINGS OFF AT 2AM                 #
#                                                       #
#########################################################
- id: 2am_turn_off
  alias: Turn things off at 2AM
  initial_state: True
  trigger:
    platform: time
    at: '02:00:00'
  action:
    - service: light.turn_off
      data:
        entity_id:
          - light.table_lights
          - light.spot_lights
          - light.balcony_led
        transition: 1
    - service: notify.ios_jack_iphone
      data:
        title: "Home Assistant"
        message: "Table, Spot and Balcony Lights are off."

This one turns off some other lights at 2AM in the morning and sends a notification to my phone.

#########################################################
#                                                       #
#             TURN THINGS OFF AT SUNRISE                #
#                                                       #
#########################################################
- id: sunrise_turn_off
  alias: Turn things off at Sunrise
  initial_state: True
  trigger:
    platform: sun
    event: sunrise
  action:
    - service: light.turn_off
      data:
        entity_id:
          - light.flower_lights
          - light.kitchen_led
          - light.kitchen_table_led
          - light.xiaomi_gateway_office
          - light.xiaomi_gateway_hallway
          - light.office_yeelight_strip
        transition: 1
    - service: notify.ios_jack_iphone
      data:
        title: "Home Assistant"
        message: "Flower, Kitchen LED, Table LED Office and Hallway Lights are off."

The above automation turns off a bunch of lights in the morning when the sun rises.

#########################################################
#                                                       #
#         TURN THINGS ON 45 MINS BEFORE SUNSET          #
#                                                       #
#########################################################
- id: sunset_minus45_turn_on
  alias: Turn things on 45 minutes before Sunset
  initial_state: True
  trigger:
    platform: sun
    event: sunset
    offset: '-00:45:00'
  condition:
    condition: state
    entity_id: group.residents
    state: home
  action:
    - service: light.turn_on
      data:
        entity_id:
          - light.christmas_lights
          - light.table_lights
          - light.spot_lights
          - light.flower_lights
    - service: notify.ios_jack_iphone
      data:
        title: "Home Assistant"
        message: "Christmas, Table, Spot and Flower Lights are on."

This one turns on some of the lights 45 minutes before the sun sets. The reason I chose 45 minutes here is because by the time the sun is set, it is pretty dark.

#########################################################
#                                                       #
#               TURN THINGS ON AT SUNSET                #
#                                                       #
#########################################################
- id: sunset_turn_on
  alias: Turn things on at Sunset
  initial_state: True
  trigger:
    platform: sun
    event: sunset
  action:
    - service: light.turn_on
      data:
        entity_id: light.kitchen_led
        effect: blue_fade
        transition: 1
    - service: light.turn_on
      data:
        entity_id: light.kitchen_table_led
        effect: blue_fade
        transition: 1
    - service: light.turn_on
      data:
        entity_id: light.balcony_led
        effect: blue_fade
        transition: 1
    - service: light.turn_on
      data:
        entity_id: light.xiaomi_gateway_office
        transition: 1
        rgb_color: [255,0,0]
        brightness_pct: 50
    - service: light.turn_on
      data:
        entity_id: light.office_yeelight_strip
        rgb_color: [255,0,0]
        transition: 1
        brightness_pct: 50
    - service: light.turn_on
      data:
        entity_id: light.xiaomi_gateway_hallway
        rgb_color: [0,0,255]
        transition: 1
        brightness_pct: 50
    - service: notify.ios_jack_iphone
      data:
        title: "Home Assistant"
        message: "Kitchen LED, Table LED, Balcony LED, Office and Hallway Gateway Lights are on."

This one turns on the remaining lights (mostly LED strips) at sunset. They are there for decoration rather than to provide light so I didn’t mind them turning on at sunset rather than 45 minutes before.

#########################################################
#                                                       #
#        WHEN MOTION IN OFFICE, LIGHT GATEWAYS          #
#                                                       #
#########################################################
- id: gateway_lights_daytime_motion_on
  alias: When motion in the Office, turn on Office and Hallway Gateway light daytime
  initial_state: True
  trigger:
    platform: state
    entity_id: binary_sensor.office_motion_sensor
    from: 'off'
    to: 'on'
  condition:
    condition: state
    entity_id: sun.sun
    state: above_horizon
  action:
    - service: light.turn_on
      data:
        entity_id: light.xiaomi_gateway_office
        rgb_color: [255,0,0]
        transition: 1
        brightness_pct: 50
    - service: light.turn_on
      data:
        entity_id: light.xiaomi_gateway_hallway
        rgb_color: [0,0,255]
        transition: 1
        brightness_pct: 50
    - service: notify.ios_jack_iphone
      data:
        title: "Home Assistant"
        message: "Somebody moved in the office."

This one just switches two lights on when somebody moves in the office.

#########################################################
#                                                       #
#    WHEN NO MOTION IN OFFICE FOR 5 MINS GATEWAYS OFF   #
#                                                       #
#########################################################
- id: gateway_lights_daytime_motion_off
  alias: When NO motion in the Office for 5 minutes, turn off Office and Hallway Gateway light daytime
  initial_state: True
  trigger:
    platform: state
    entity_id: binary_sensor.office_motion_sensor
    from: 'on'
    to: 'off'
    for:
      minutes: 5
  condition:
    condition: state
    entity_id: sun.sun
    state: above_horizon
  action:
    - service: light.turn_off
      data:
        entity_id: light.xiaomi_gateway_office
        transition: 1
    - service: light.turn_off
      data:
        entity_id: light.xiaomi_gateway_hallway
        transition: 1
    - service: notify.ios_jack_iphone
      data:
        title: "Home Assistant"
        message: "Nobody moved in the office for 5 mins."

This one switches off the lights if nobody moves in the office for 5 minutes.

#########################################################
#                                                       #
#      WHEN PHILIP IS AT HOME, SEND A NOTIFICATION      #
#                                                       #
#########################################################
- id: philip_home_notify_jack
  alias: When Philip is at home, inform Jack's iPhone
  initial_state: True
  trigger:
    platform: state
    entity_id: device_tracker.philipiphone
    from: 'not_home'
    to: 'home'
  action:
    service: notify.ios_jack_iphone
    data:
      title: "Home Assistant"
      message: "Philip has arrived home."

This one sends me a notification when one of the residents come home.

#########################################################
#                                                       #
#     WHEN PHILIP HAS LEFT HOME, SEND A NOTIFICATION    #
#                                                       #
#########################################################
- id: philip_out_notify_jack
  alias: When Philip has left home, inform Jack's iPhone
  initial_state: True
  trigger:
    platform: state
    entity_id: device_tracker.philipiphone
    from: 'home'
    to: 'not_home'
  action:
    service: notify.ios_jack_iphone
    data:
      title: "Home Assistant"
      message: "Philip has left home."

This one sends me a notification when one of the residents leaves home.

#########################################################
#                                                       #
#        WHEN RESIDENTS ARE OUT, SWITCH OFF KODI        #
#                                                       #
#########################################################
- id: residents_out_switch_off_kodi
  alias: When residents are out, switch off KODI
  initial_state: True
  trigger:
    platform: state
    entity_id: group.residents
    from: home
    to: not_home
  action:
    - service: media_player.media_stop
      data:
        entity_id: media_player.living_room_kodi
    - service: notify.ios_jack_iphone
      data:
        title: "Home Assistant"
        message: "Kodi has been switched off."

When nobody is at home, make sure Kodi is not streaming anything. This is to prevent unnecessary internet usage.

#########################################################
#                                                       #
#     WHEN RESIDENTS LEFT AT NIGHT SWITCH OFF LIGHTS    #
#                                                       #
#########################################################
- id: residents_out_at_night_switch_off_lights
  alias: When residents are out at night, switch off some lights
  initial_state: True
  trigger:
    platform: state
    entity_id: group.residents
    from: home
    to: not_home
  condition:
    condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
    - service: light.turn_off
      data:
        entity_id:
          - light.christmas_lights
          - light.table_lights
          - light.spot_lights
          - light.kitchen_led
          - light.kitchen_table_led
          - light.xiaomi_gateway_office
          - light.xiaomi_gateway_hallway
    - service: notify.ios_jack_iphone
      data:
        title: "Home Assistant"
        message: "Nobody is at home so I switched some lights off."

One of the notifications I had above was switching some lights on when the sun is set. This automation makes sure some of them switch off when there are no residents in the flat, to save electricity.

#########################################################
#                                                       #
#    WHEN RESIDENTS CAME IN AT NIGHT SWITCH ON LIGHTS   #
#                                                       #
#########################################################
- id: residents_in_at_night_switch_on_lights
  alias: When residents came in at night, switch on some lights
  initial_state: True
  trigger:
    platform: state
    entity_id: group.residents
    from: not_home
    to: home
  condition:
    condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
    - service: light.turn_on
      data:
        entity_id:
          - light.christmas_lights
          - light.table_lights
          - light.spot_lights
          - light.kitchen_led
          - light.kitchen_table_led
          - light.flower_lights
          - light.xiaomi_gateway_office
          - light.xiaomi_gateway_hallway
    - service: notify.ios_jack_iphone
      data:
        title: "Home Assistant"
        message: "Welcome back! I switched some lights on."

And this one switches them back on if they come back before the sun rises. I don’t want to turn them on if they come back after sun rises.

6 Likes

Thanks for sharing, it gave me some idea’s what I’d like to do in my own instance. Hopefully more will share, as people can be very creative and make things useful! :slight_smile: