Home automation from nothing to done: Part 5

This is one of a serie of 7 posts about home automation from nothing to done. Each of them marks a milestone in the project and none of them should be too long to comfortably read in the metro when commuting.

In this post we’ll learn how to configure automations so that our system won’t necessarily need user input in order to take actions anymore! We’ll see how to set a cinema atmosphere when watching movies, and how to coordinate multiple lights in the same room. In case you didn’t read the first four parts, please take into account that I’m using Home assistant as my home automation software. I have it running on a Raspberry Pi 3, but if you just want to try it out and don’t want to commit buying a device you can install it on your desktop or laptop. I strongly recommend using their docker image in this case since it’s the fastest and less intrusive way to try it out.

At the end of this post we’ll be able to:

  • Set a cinema atmosphere when watching movies
  • Coordinate multiple groups of lights
  • Get a weather forecast for the day every morning

all without a single touch.


Automations in Home Assistant are just what you think they could be: a way to take a sequence of steps that you previously performed manually (or already semi-automatically through scripts) and configure them to be completely automated.

Home Assistant automations provide you with 3 powerful instruments to achieve this:

  • Triggers
  • Conditions
  • Actions

Triggers are the events that start the automation. For instance, a light turning on could be a trigger; the sun setting or rising could be another one; the weather getting rainy outside, the tv turning on or just about any action that Home Assistant can sense in some way.

Conditions are the things that need to be satisfied in order for the automation to happen. For instance, you may want to turn on the lights when you arrive home. A geofence or a device tracker set to recognize when you get home is the trigger, but this automation wouldn’t be very useful if it turned on the lights during the day too, would it? The sun being set is in this scenario a good condition to determine whether to start the automation or not.

Actions are the steps you want to perform in the automation. For instance, turning on the lights would be an action. Setting a scene or executing a script are also valid actions.

A good resource to get started on the automation concepts is this link on the Home Assistant Getting started section.

If you want to dive deep in all the possibilities and powerful mechanisms of automations, you’d be better off directly reading the docs or having a look at some of the examples listed on the website.

Our first automation

For our first automation, let’s implement something that I setup in my bedroom. I have 3 Hue bulbs, two for the bedlights and one for the ceiling light. What I want to achieve is that if I turn on the bedlights, the ceiling light turns off, and viceversa.

Let’s start by turning off the ceiling light when turning on the bedlights:

- alias: Turn off ceiling bedroom light when turning on bedlights
    - platform: state
      entity_id: light.sabrina_bedlight
      to: 'on'
    - service: light.turn_off
      entity_id: light.bedroom_ceiling

As you can see there is no condition needed for this automation. Another peculiar thing for the trigger is that I’m watching the status of only one of the 2 bedlights. This is because I grouped them together with a Hue dimmer, so you can’t turn one on without the other.

Let’s now write the automation for the inverse case, when we want to turn off the bedlights when the ceiling light is turned on:

- alias: Turn off bedlights when turning on ceiling light
    - platform: state
      entity_id: light.bedroom_ceiling
      to: 'on'
    service: light.turn_off
      - light.sabrina_bedlight
      - light.vittorio_bedlight

This is very similar to the previous one, except that I’m turning off both bedlights, of course.

To let Home assistant know about our automations, we can add

automation: !include automations.yaml

to our configuration.yaml.

For the available triggers (e.g. the state platform), this link is a great resource.

An automation with conditions

We just saw how to write a very simple automation. It just had a trigger (required for automations to exist) but no condition.

We’ll now write a second automation that also includes conditions.

One of my favorite automations (and the one I always show off when friends are visiting) is the one that sets a cinema atmosphere when watching movies.

In my living room I have 2 Hue bulbs, one for the ceiling and (a colored) one in the corner, next to the sofa. When I watch movies, I want the ceiling light to turn off and the sofa light to be very slightly dim, just to avoid a complete dark setting. When I pause or stop them, I want the opposite to happen. All of this only if there is no sunlight outside, of course! :)

Let’s start with the first step, and to make it more interesting, let’s write a scene that we can activate in our action section:

In our scenes.yaml:

- name: Movie playing in living room
      state: off
      transition: 5
      state: on
      transition: 5
      brightness: 80
      rgb_color: [ 255, 255, 204 ]

In our automations.yaml:

- alias: Toggle lights when playing movies
    - platform: state
      entity_id: media_player.osmc
      to: 'playing'
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    service: scene.turn_on
    entity_id: scene.movie_playing_in_living_room

In this automation the trigger is our OSMC media player changing its state to playing.

The condition is that the sun is below the horizon (i.e. there is no sunlight). The time at which the sun rises and sets is updated every day depending on the coordinates of your home you can set in the configuration.yaml and it works pretty well.

The action is that the scene we previously wrote is set.

Let’s write a scene and an automation for the inverse case:

In scenes.yaml:

- name: Movie stopped in living room
      state: on
      transition: 5
      brightness: 150
      state: off
      transition: 5

In automations.yaml:

- alias: Toggle lights when pausing/stopping movies
    - platform: state
      entity_id: media_player.osmc
      from: 'playing'
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    service: scene.turn_on
    entity_id: scene.movie_stopped_in_living_room

As you can see both the scene and the automation are very similar. One thing that could be noted is that the trigger doesn’t watch for transitions to a state, but from. This way the same single automation is triggered if we pause or stop a movie.

That’s it! I assure you this is something that’s going to impress your friends :)

One last automation example

As a last automation example (but feel free to skip this paragraph if you’re not interested), we are going to implement a morning notification informing us of the weather for the day. This way we’re ready to go to work with the right clothing on, and decide whether to bring the umbrella or not.

For this automation we need 2 things:

  • A Pushbullet account. This is needed to send notifications to all our subscribed devices (whether it’s a smartphone or a computer)
  • A Dark Sky developer account. With this we’re going to have 1000 API calls a day, that will be enough to get not only a morning notification but an up-to-date forecast during the day and for the days to come

After creating the two accounts, we can add both the notify section to the configuration.yaml file:

  platform: pushbullet
  api_key: [YOUR_API_KEY]
  name: pushbullet

and the darksky sensor:

  - platform: darksky
    api_key: [YOUR_API_KEY]
    scan_interval: 120
      - summary
      - precip_intensity
      - precip_probability
      - temperature
      - wind_speed
      - cloud_cover
      - humidity
      - visibility 
      - apparent_temperature
      - hourly_summary
      - daily_summary
      - temperature_max
      - temperature_min
      - precip_intensity_max

It’s important to have a scan_interval of around 120 to avoid doing more than a 1000 API calls a day. For the monitored_conditions, feel free to prune them according to your needs.

Now to the automation:

- alias: Daily weather in the morning (weekdays)
    platform: time
    after: '07:15:00'
    - condition: time
        - mon
        - tue
        - wed
        - thu
        - fri
    service: notify.pushbullet
      target: 'channel/[YOUR_CHANNEL]'
      title: "{{ states.sensor.dark_sky_hourly_summary.state }}"
      message: "The temperatures will range from a minimum of {{ states.sensor.dark_sky_daily_low_temperature.state }} to a maximum of {{ states.sensor.dark_sky_daily_high_temperature.state }}"

You can replace the [YOUR_CHANNEL] string with the channel you created and subscribed your devices too. You should then receive a notification every morning at 7:15 AM (but not during the weekend!) telling you the forecast and the min/max temperatures. Try tweaking the parameters of this automation to understand how it works and to make it perfect for your needs!

You can see an example of the sensors the darksky platform makes available for you with the configuration above in the following screenshot:

darksky sensors

With this post we were able to create new automations using the appliances, scenes and scripts we learned about in the previous posts. I recommend you to play with automations until you feel you master them and are confident you can write just the right one for you!

With the next post I’ll write about how to take it even a step further, and trigger every action we configured until now, with your voice. You’ll be able to speak to your flat, just like Robert Downey Jr. in Iron Man :) Keep reading!