Getting started with Home Assistant automations

Home Assistant gives you a handy way of controlling your smart devices from a single interface, but the real power comes from adding automations.

Automations consist of triggers, conditions, and actions -when a trigger occurs and the conditions are met then carry out the actions.

The trigger is the step that initiates the automation, and an automation can have multiple triggers – it will then run if any of them occur.

Available triggers will depend on what devices or services that you have available and can include:

  • Motion detected by a motion sensor.
  • A door sensor detecting that a door has been opened.
  • Temperature sensor crossing a threshold.
  • Time of day, such as sunrise or sunset.
  • Presence detection when you leave or return home.

To create an automation, go to “Configuration” in the side menu, select “Automations” and then “Add automation.”

There are three options – you can describe the automation and let Home Assistant attempt to create the automation using the Almond AI engine, use a blueprint, or start with an empty automation. I always start off with an empty automation.

Let’s say you want to turn a light on when motion is detected but only at night. Pick a name for your automation and start with the trigger:

Note that the trigger is activated when the state of the motion sensor goes to “on” – there’s no need to set a “from” state, because the only other state for a binary sensor is “off” and the trigger will only run when the state changes. For sensors with multiple states, you may find a use for the “from” restriction if you only want to run your automation when the state changes between specific values.

When you look at the entity in the front end, it will show “Detected” when motion is detected and “Clear” when it is not:

Each entity has a type that determines how it is displayed, but for the automation you will need the actual entity state value – another example is the door sensor which shows “Open” and “Closed” for the “on” and “off” state values.

To see the state value, you can go to developer tools in the sidebar menu and search for the entity. This will show you the values that you should use in your automations:

If you had multiple sensors and wanted to turn the light on when any of them are triggered, you’d simply add additional triggers for each sensor.

The next step is the condition. You can have multiple conditions and by default all conditions must be met for the automation to continue. You can also use an “OR” condition if you need to run an automation if you want any one of multiple conditions to be met.

There are two ways to create a condition to restrict your automation to run only at night. The simplest is to use the “sun” entity and check that the state is “below_horizon”:

You can also use the “sun” condition and use both of the sunrise and sunset options:

To run at night, you can’t use both the “after sunset” and “before sunrise” settings at the same time, as that will only be true if the time is both between sunset and midnight and between midnight and sunrise – which is clearly not possible.

You’ll need to start with an “OR” condition and then add two sub-conditions for “after sunset” and “before sunrise”:

The advantage of this method over the simple “below_horizon” state check is that you can use an offset – for example, you might want the valid times to run from half an hour after sunset to half an hour before sunrise.

Finally, actions are the things that will be done when the triggers activate and the conditions are met.  To turn a light on, you’ll need to call the light.turn_on service:

You can then pick your light. If you want to, you can set the brightness and colour for the light but that’s optional – for most lights, the defaults will be the last values used.

Most automations can be created entirely through the web interface, but sometimes you might need to add some YAML code. You can do that at either the level of individual elements – triggers, conditions, or actions – or overall for the entire automation. To switch between the YAML and visual automation editors, click on the three dots next to the item that you want to edit. For example, the YAML code for the sunrise/sunset condition above is:

The code for the entire automation is:

alias: New Automation
description: ''
  - platform: state
    entity_id: binary_sensor.front_door_motion
  - condition: or
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
  - service: light.turn_on
      entity_id: light.entrance
mode: single

Having the YAML option available can also be really handy for sharing automations with other people. If you see an automation in YAML somewhere online, you can always copy and paste it, and then switch to the visual editor to make further changes.

Automation is obviously a huge topic and this was just a quick intro to round out this series of posts on starting from scratch. You can find other examples of automations in some of my other posts, and, of course, the Home Assistant website and forums. These are often in the form of blueprints, which are basically templates for common automations.

Having covered the basics, I’ll probably be returning to my usual fortnightly schedule, but I’ve still got plenty of Home Assistant content to come, as well as a few other things planned.

in Home Automation

Add a Comment

Your email address will not be published. All comments will be reviewed.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts