Home automation from nothing to done: Part 4


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 start taking advantage of the groundwork we did setting up our automation software. We’ll add some scenes so that we will be able to prepare our home for specific situations and we’ll write some scripts to perform complex tasks in a simple way. In case you didn’t read the first three 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:

  • control multiple lights
  • control multiple thermostats
  • setup our media center

all with a single touch.

Scenes

As explained in the Home Assistant documentation, scenes are a way for you to describe the state of multiple entities (e.g. lights, smart plugs, thermostats, and other appliances), resulting in a new scene entity that you can reuse in scripts or automations.

Our first scenes

For instance, let’s say that when we watch a movie, we want to turn off the living room ceiling light and to dim the sofa light sitting behind us. We might create a scene that looks like this:

- name: Living room playing movie
  entities:
    light.living_room:
      state: off
      transition: 5
    light.sofa:
      state: on
      transition: 5
      brightness: 80
      rgb_color: [ 255, 255, 204 ]

This is giving a name to the scene so that we can reference it later. Then it goes on describing the state of all the entities we’re interested in. The living room light should go off with a transition of 5 seconds, and the sofa light should turn on at a light intensity of 80 out of 255 with a transition of 5 seconds (the color is a yellow-ish white).

On the other hand, when we’re not watching a movie anymore, we want the sofa light to go off and the ceiling light to go back on:

- name: Living room after movie
  entities:
    light.living_room:
      state: on
      transition: 5
      brightness: 150
    light.sofa:
      state: off
      transition: 5

The configuration is very similar so I’ll skip the detailed explanation. You can now save the previous scenes in a scenes.yaml file and add the following to the configuration.yaml file:

scene: !include scenes.yaml

After restarting Home Assistant you will see your scenes and you will be able to activate them one by one. Please try tweaking the values above and watch how the final result changes. You should eventually use what works best for you. This is the best part of implementing your own automation setup: you are in control!

Scripts

Once again, the Home Assistant documentation does a very good job at explaining what scripts are.

With scripts we are now able to write sequences of commands. We can even add conditions so that depending on the state of the house or one of its entities, the script behaves differently. Scripts can of course also call other scripts during their execution, so we can modularize complex sequences by splitting them into multiple simple ones (assuming each small portion has a meaning on its own). It’s a bit like a (quite limited) programming language we can write our functions in!

Our first script

Let’s assume we want to collect the sequence of steps needed to turn on our media center into a single script.

We want to turn on our A/V receiver and set it on the correct HDMI source; then we want to turn on our TV and our HTPC.

My script looks like this:

turn_on_media_center:
  alias: Turn on the media center
  sequence:
    - service: media_player.turn_on
      entity_id: media_player.denon
    - delay: '00:00:05'
    - service: media_player.select_source
      data:
        entity_id: media_player.denon
        source: 'DVD/Blu-ray'
    - service: media_player.volume_set
      data:
        entity_id: media_player.denon
        volume_level: "0.45"
    - service: media_player.turn_on
      entity_id: media_player.tv
    - service: switch.turn_on
      entity_id: switch.media_center

As you can see I’m adding a delay so that the receiver has enough time to start before we ask it to change its input.

A script (like many other entities) has an alias describing what it does, and a sequence of steps.

A step can be a service call, a delay or a condition. A service can be for example light.turn_on, media_player.turn_on or any other one you can call on any of your entities. You can check which services are available in your setup by clicking the Services icon (the first on the left) in the Developer Tools section in the bottom left corner of the Home Assistant web interface (or opening http://[YOUR_HOME_ASSISTANT_IP]:8123/dev-service).

You can then select a Domain (that can be light, media_player, climate and so on) and you will be able to see all the services available for that domain:

available services

Choosing one service will show you a simple JSON payload you can put into Service Data:

service data

Clicking Call service will actually call the service passing the given data so you can try and see how your appliances behave before diving into a script. It’s a great testing playground!

We can save our script into a scripts.yaml file and add the following line to our configuration.yaml:

script: !include scripts.yaml

we will then see the script in the Home section of Home Assistant after restarting it, and turn it on by clicking the Activate button, but I’m quite sure this wouldn’t do the trick since we didn’t configure media players in the first place, and you probably have a different setup anyways!

If you’re eager to configure media players, you can have a look at all the supported ones on this Home Assistant page. I’m quite positive that the one you have is supported! :) We will anyway do some work with media players during Part 7, where we’ll setup a multi-room synchronized audio configuration similar to the one you could achieve through a Sonos setup - only by spending a thousand € less ;)

I strongly recommend playing with scripts until you’re comfortable writing your own. Don’t worry about conditions and the other complexities, a simple sequence of steps you would like to automate works perfectly and you’ll have a great sense of accomplishment once you won’t have to manually do it anymore!

With this post we were able to organize our appliances with scenes and perform complex tasks with scripts. All of this with Home assistant so that we can trigger them through its web interface (also from our mobile phone).

With the next post I’ll write about how to create automations, one of the most powerful bullets that Home Assistant offers us. Keep reading!