Home Assistant

Home Assistant Automation Tutorial For Dummies

If you a new to Home Assistant it can be quite difficult to overcome the initial learning curve, however once you get to grips with the basics it is really quite simple, whilst being a very powerful tool.

The Home Assistant developers have been making it progressively easier and more user friendly, however at the time of writing it is not yet possible to configure everything in the GUI. You will need to use at least a little YAML.

It is now possible to create automations, scenes and scripts using the graphical user interface. However in order to use these tools you will still need an understanding of the various states, entities and services within Home Assistant.

In this tutorial we will take a look at the graphical user interface for creating our automations. Then we will then take a look at the YAML version of the automation.

Bitmoji Image

Prerequisite

You will of course need Home Assistant installed and running. We will also be using the file editor add-on so if you didn’t install it yet go ahead and click the Supervisor button on the side bar, then click the add-on store tab at the bottom of the page.

Install file editor

Scroll down the page and find the file editor plug in, click on it and click install to add the plugin. Once installed you should see the file editor option appear in the sidebar.

Entities

You should also have an understanding of what an entity is in Home Assistant, so I will cover it here briefly.

An entity is the terminology used to describe an object within Home Assistant. Entities are grouped by their type, for example all lights are grouped into the light entity category and switches are grouped into the switch entity category.

Each entity has a name that Home Assistant uses for reference called the entity_id. This name needs to be compatible with the Home Assistant programming, therefore it does not contain spaces or exotic characters that may disrupt the programming. If in doubt just stick to numbers, letters and use an underscore “_” for a space. Do not use brackets, symbols or spaces.

Home Assistant also allows us to assign a friendly name to objects, which is more human readable. The friendly name serves as a reference for us but is not used by the code.

If you wish to edit the entity_id or friendly name you can do so in the integrations menu found on the supervisor menu.

When we want to reference an entity in Home Assistant we use the following syntax.

entity.entity_id

For example, I have a lamp in the corner of my room that looks a little like a tree, I use it a lot for an example in my tutorials. This lamp is of course part of the light entity group and its name is treelamp. Therefore if I wish to reference it in Home Assistant I can using the following.

light.treelamp

The same method would be used to reference a switch. Here is another entity from my Home Assistant setup, a smart plug that I use to control the kettle.

switch.kettle

Now that we understand how to reference entities in Home Assistant we can take a look at creating our first automation!

What is an automation?

Automation is a powerful feature of Home Assistant, which allows us to tie entities together and create events that occur automatically.

An example would be that we can get our lamp to automatically turn on and off when we switch the main light in the room on and off. Another example could be that we get the kettle to switch on when we get out of bed.

An instance of automation consists of three primary elements, the trigger, the conditions and the action.

Trigger

The trigger is the event in Home Assistant that causes the automation to fire. You can see a full list of the different triggers available on the Home Assistant website.

There are many examples of something that could be a trigger, examples include an entity turning on, a person returning home or the sun rising.

Conditions

The conditions are a series of rules that define whether or not an automation should occur when fired. A condition could prevent an automation from running over the weekend or only turn on a light after sunset.

For further information you can read about all of the different conditions on the Home Assistant website.

Action

The action is quite simply the action that will occur when the automation fires. The action could be something like a light turning, a door locking or a switch turning on.

Automation Editor

First we will create an automation using the graphical user interface. All of the automations generated by the GUI will be stored in the automations.yaml file.

You can find this file in the config directory. We will not need to open or edit it when using the automation editor, however we will need to tell Home Assistant that we want to load the automations from it.

In order to do this click on file editor in the sidebar, locate the file configuration.yaml and click it to open it in the file editor.

Add the automation integration

Next we need to to add the automation integration to the configuration.yaml file and tell it to use automations.yaml. Simply add the following line of code to your file.

You can only have one instance of the “automation:” integration within your configuration.yaml file, so if you already have it just replace it with the following code.

automation: !include automations.yaml

Now Home Assistant will load your automations from automations.yaml, so next we need to create some automations!

Open the editor

In order to open the editor, click the configuration button on the side bar and then click on automations.

This will open up the automation editor page and display all of the automations that Home Assistant has recognized.

Clicking on the information icon to the right-hand side of each automation displays information about the automation. It shows when it was last triggered and gives us the option to disable it. We can also execute it, which is useful for testing.

Create a simple automation

In order to create a new automation, click the orange circle with the plus sign in the bottom right-hand corner of the screen.

You will be presented with a screen that prompts you for a description of what you would like the automation to do. Home Assistant will try to convert this to your desired automation.

You can try it if you like, but for this tutorial we will create one manually so go ahead and just click skip.

You will be presented with the new automation screen, where we can enter all of the relevant information for our automation. For this example we will create an automation that will boil the kettle at sunrise.

Bitmoji Image

Add name and description

First let’s give the automation a name and description. This automation will boil the kettle at sunrise, so we will name is accordingly.

Once we have specified the name and description we need to add a trigger. This is the event in Home Assistant that will start the automation.

Add a trigger

Home Assistant has an entity called sun that can be used to link things to sunrise and sunset. Therefore we will set the trigger type to sun and choose sunrise for the event.

Next we will set the action to occur at sunrise, which is of course to boil the kettle. In order to do this we need to set the action type to call service.

Then in the service drop-down menu we can type “switch” to list all of the services available for the switch entity. For this example we will use the service switch.turn_on so that we can turn on the kettle.

Add an action

Finally we need to give the entity_id of the switch that we want to turn on. My kettle is connected to a switch called switch.kettle therefore I will enter this here.

Create an automation with a condition

Now that we have an understanding of a basic automation, let’s create an automation that has a condition.

This automation will fire when the bedroom light is switched on, but only if the temperature is above 23C. So we are introducing a temperature value as our condition.

The action will be tied to a smart plug that controls an AC unit in the living room. When I wake up in the morning and turn the bedroom light on, the AC turns on and starts to cool the living room if the temperature is above 23C.

In order to set the automation to trigger when the bedroom light is switched on we will set the trigger type to state. This allows us to check a particular entity for a change of state, such as from “off” to “on.”

In this example light.lohastwo is the smart bulb in my bedroom light. We want to fire the automation when the state of this entity changes from “off” toon.”

Next we need to add our condition and set the condition type to numeric state. This allows us to detect a change in a numeric value, in this case the temperature.

The entity is set to sensor.temperaturedownstairs which is the temperature sensor in my thermostat downstairs. The number above parameter will tell Home Assistant to fire the action only if the sensor value is above the number specified. The number below parameter works the same but when the sensor value is below the number specified.

Finally we will add the action, which is the same as in our first example only we are specifying the entity switch.ac, which will turn on the AC unit.

Thats it! Now we can save the new automation to our automations.yaml file using the save button in the bottom right-hand corner of the screen.

Automation YAML

Now that we have created an automation with the automation editor, we can take a look at the YAML version.

It should hopefully be fairly easy to understand now that we have walked through the process in the graphical editor.

Open the file editor

First click on file editor on the sidebar and navigate to your automations.yaml file.

Writing the automation manually

If you saved the automation in the graphical editor you will see the code that has been generated. If you wish to manually add an automation, simply write the code beneath the existing code.

One very important point to make is that YAML is very sensitive to tabs and spacing. You must ensure that your tabs and spacing are correct in the file or you will get syntax errors. This is the number one cause of problems when people are editing YAML.

We must also move the “-” operator down to the alias: parameter as we still need it there in order to specify a new item in our list of automations. The “-” operator is always used to denote items in a list, just think of it like a bullet point.

ID number

In order for the automation to work with the graphical editor we will need to specify an ID number.

The graphical editor generates one automatically but if you are writing an automation manually, just specify your own number. It does not matter what number you specify, as long as it is unique.

- id: '1582583923026'

Name and description

Firstly we will give our automation the name MorningAC and description.

- id: '1582583923026'
  alias: MorningAC
  description: Turn on air conditioning at sunrise if temperature is above 23 degrees

If you wish to write all of the your automations in YAML and are not going to use the graphical editor, you can omit the ID attribute.

Add the trigger

Next we specify the trigger and assign the entity_id.

- id: '1582583923026'
  alias: MorningAC
  description: Turn on air conditioning at sunrise if temperature is above 23 degrees
  trigger:
  - entity_id: light.lohastwo

Then we specify the platform as state because we want to trigger the automation when the state of something changes, in this case our light. The state change we are looking for is from off to on.

- id: '1582583923026'
  alias: MorningAC
  description: Turn on air conditioning at sunrise if temperature is above 23 degrees
  trigger:
  - entity_id: light.lohastwo
    platform: state
    from: 'off'
    to: 'on'

Add the condition

Next we will specify the condition as a numeric_state. We would only like our automation to fire when sensor.temperaturedownstairs is above 23.

- id: '1582583923026'
  alias: MorningAC
  description: Turn on air conditioning at sunrise if temperature is above 23 degrees
  trigger:
  - entity_id: light.lohastwo
    platform: state
    from: 'off'
    to: 'on'
  condition:
  - above: '23'
    condition: numeric_state
    entity_id: sensor.temperaturedownstairs

Add the action

Lastly we will specify the action as the switch.turn_on service. The entity we wish to switch on is switch.ac.

- id: '1582583923026'
  alias: MorningAC
  description: Turn on air conditioning at sunrise if temperature is above 23 degrees
  trigger:
  - entity_id: light.lohastwo
    platform: state
    from: 'off'
    to: 'on'
  condition:
  - above: '23'
    condition: numeric_state
    entity_id: sensor.temperaturedownstairs
  action:
  - entity_id: switch.ac
    service: switch.turn_on

YAML tips

As previously mentioned, make sure that your tabs and spacing are correct! This is the number one cause of problems for beginners.

The order of items in a list does not matter. For example you can write the trigger as it is generated by the graphical editor.

  trigger:
  - entity_id: light.lohastwo
    platform: state
    from: 'off'
    to: 'on'

Or you can write it like this, with the items in the list in a different order. Note that items in the list have the same tab spacing.

  trigger:
  - platform: state 
    entity_id: light.lohastwo
    to: 'on'
    from: 'off'

When you want to create a list of something, use the “-” operator and be sure to put a space afterwards. You can create lists of triggers, conditions and actions.

  trigger:
  - platform: state 
    entity_id: light.lohasone
    to: 'on'
    from: 'off'
  - platform: sun 
    event: sunrise

Next steps…

Congratulations, you have successfully made it to the end of this tutorial! I hope that it has given you a better understanding of automations in Home Assistant.

Thanks for visiting and please be sure to leave a comment below! Now click on one of the links below and go put your new found knowledge to some good use!

trophy unlocked

Join the discussion

  1. Maulana Falithurrahman

    Is there any way to control automation from different node?
    For example, I have ESP8266 (node A) with binary GPIO sensor connected to physical switch. I want to control relay attached to another ESP8266 (node B) everytime GPIO sensor in node A is switched on. Do you have any reference where i can find the solution? Thanks

    • Siytek

      Hi Maulana, thanks for visiting! Are you wanting to link these two directly or are you using Home Assistant? If you are running Home Assistant then yes it’s definitely possible. Flash both ESP8266 nodes with Tasmota, set one up as a switch and the other as a relay using Generic(18). Then connect both nodes to Home Assistant using MQTT. Finally, set up an automation that switches the relay when the button is pressed.

  2. Vikram Nadgir

    Hi Siytek,
    Thank you for creating this tutorial. I think you have simplified the creation of automation .
    I have installed Home assistant 10days ago. I have been struggling to understand automation /scripts etc.

    I found it very useful.

    regards,
    Vikram

Leave a Reply

Your email address will not be published. Required fields are marked *

back to top