Step 11: Automation

So, you finally have all the building blocks for automation: a sensor to activate a switch or set of switches.

The original initial goal of this blog was to setup a cloud free home automation system, this got extended with the ability to control the lights a house without ever manually having to switch them on or off.

This is starting to be achievable! If you only have the one sensor then you will only be able to control one room, but the principles will be exactly the same.

There are example automation scripts that can do this, but lets build our own simple one. Please bear in mind that the setup described will only work with one actor i.e. one person within the home.

First you’ll need to start a new automation script via the big plus button within the automation configuration section.

Configuration -> Automations -> +

Give it a suitable name like “lounge lights” or whatever is appropriate, and set the motion sensor as a trigger event and then add a condition on the sensors luminance to ensure it is dark enough to warrant triggering the lights.

Then add all the light switches/devices that you want to turn off – such as all the ones outside of the room where the motion sensor for the trigger is – and then also add the lights switches/devices that you want to turn on as actions at the bottom of the automation screen.

Another option here is to call the scene.turn_on service and have a scene setup with the lights you want turning on. Unfortunately with this setup you do not want to simply create an all lights off scene and add that too as every-time motion is detected all the lights will turn off then the ones you want on will turn back on, so you want to set it as described to stop potential light flickering or make a collection of scenes – one for each room to control.

There are tons more automations capable and there will be a couple more covered in this stride, but first you will need to be able to access your system via the mobile app out and about for some real fun…

Next: Step 12: External

Step 5: Sun & Moon

Now it is time for some simple automation. Lets say something like changing the theme based on whether the sun is above or below the horizon.

Home Assistant makes use of Devices and Entities. For example, a device might be a Multi Sensor, so it will have multiple entities, one for each sensor. Most motion sensors include a temperature and luminescence or humidity etc.

This automation will trigger whenever Home Assistant starts (or restarts) and on the state of the Sun (a built-in sensor).

 - alias: change theme
   trigger:
   - event: start
     platform: homeassistant
   - entity_id: sun.sun
     platform: state
     to: above_horizon
   - entity_id: sun.sun
     platform: state
     to: below_horizon
   action:
   - data_template:
       name: >
         {% if states.sun.sun.state == "above_horizon" %}
           oxfordblue
         {% else %}
           darkorange
         {% endif %}
     service_template: frontend.set_theme 

Let’s step through the elements of the automation script:

  1. alias – provide the automation a friendly name
  2. trigger – set what causes the automation to activate, in this case there are three:
    1. when your Home Assistant starts or restarts
    2. when the Sun is above the horizon
    3. when the Sun is below the horizon
  3. action – what to do when the automation is triggered, in this case an if statement checking the state of the sun to select the appropriate theme name

Unfortunately, this automation uses an action statement that is currently not understood by the Configuration Automation editor. Therefore, you need to use the Configurator to edit the ‘automation.yaml’ file directly.

To enable the Moon integration (Sun comes as default), you can simply add the following YAML to your ‘configuration.yaml’ file via the Configurator:

sensor:
  - platform: moon

This sensor will return one of the following values:

  • new_moon
  • waxing_crescent
  • first_quarter
  • waxing_gibbous
  • full_moon
  • waning_gibbous
  • last_quarter
  • waning_crescent

This means you could edit the original script to change the theme based on the current phase of the moon or of course any automation.

Complete: Stride 1: Getting Running