Automating with HomeAssistant


Now that I’ve got my devices into HomeAssistant, it’s time to get the most out of it by using automations. While it offers a great web frontend to manage everything from a single place, the true value is in automation. I don’t want to be reaching for my phone every time I want to turn a light off, so in this post I’ll be showing you my thought processes behind creating a simple automation.

Automations need a bit of thought, and you’ll probably end up tweaking them as you realise the logic you need is a bit more complex than you may have first thought.

For example, let’s say we want to turn the entrance hall light on when we open the door after dark, so that we can see what we’re doing when we get in or there’s someone at the door. I’ll assume you have a door sensor that reacts to the door opening – in my case it’s my existing SmartThings sensor that I’ve linked to HomeAssistant using the bridge.

We’ll start off simple. The first step is to trigger the automation based on the state change of the sensor:

- alias: "Light on when door opens"
  trigger:
    platform: state
    entity_id: sensor.front_door_contact
    to: "open"

So far so good, but this will turn the light on at any time of day. We only want it to run after dark – let’s say between sunrise and sunset. I went down a bit of a blind alley with the sun component at this point, focussing on the sunrise and sunset events, until I realised that there’s a “below_horizon” state for the sun component which covers the period. So we can add this as a condition:

 
 condition:
    condition: state
    entity_id: sun.sun
    state_below_horizon

Now we can add our action, which is really simple:

  action:
    - service: light.turn_on
      entity_id: light.hall

I had something similar set up with SmartThings and it had a limitation. This just turns the light on. I needed to turn the light off when I was done and why do that manually every day? Since the idea is to have the light on while you get in, let’s just have the light on for a short period of time – enough time to get in, take your coat and shoes off, and move through to the rest of the house.

For this we can add a second automation that fires when the door has been closed for a period of time. We don’t need to worry about the sun state for this one, or even checking to see if the light is already on – it doesn’t matter if it’s already off:

- alias: "Turn light off 10 minutes after door closed"
  trigger:
    platform: state
    entity_id: sensor.front_door_contact
    to: "closed"
    for:
      minutes: 10
  action:
    - service: light.turn_off
      entity_id: light.hall

That covers it. Ten minutes is plenty of time – it’s more than enough time to see what you’re doing and since it only triggers once the door is closed it won’t turn the light off while you’re paying the pizza delivery guy or politely explaining why you’re not interested in the caller’s religion, political party or charity.

This is all well and good, but what if we’ve turned the light on because we’re doing something in that room and happen to open the door? Ten minutes after we close the door the light goes off and we’re in the dark. We need to cope with that case.

We could disable the automation temporarily, but we’d have to remember to do that. What we really want is for the light to turn off only if it’s been turn on by the automation. If it was already on, leave it alone.

HomeAssistant has a useful component called “input_boolean” which acts as a virtual switch. We can use these virtual switches in the frontend but they can also be used in automations – think of them as boolean variables that we can use to store state information.

We’ll adjust our automation to set an input_boolean switch called “door_timer” to “on” when the automation has switched the light on. We’ll use this as a flag to indicate that our automation has run. We can then check the state of this and only turn the light off if the door_timer is on – i.e. if the light had previously been turned on by the automation.

First of all, we set up an input_boolean in our configuration.yaml file:

input_boolean:
  door_timer:
    name: Door timer
    initial: off

We also need to add an extra condition when turning the light on via the automation – we only want it to run if the light is not already on, because if it’s already on it must mean we’ve turned it on manually. For this we need to add a second condition that checks that the light is off.

Finally, in the actions we need to switch the door_timer boolean on. Putting it all together:

- alias: "Light on when door opens"
  trigger:
    platform: state
    entity_id: sensor.front_door_contact
    to: "open"
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: sun.sun
        state: "below_horizon"
      - condition: state
        entity_id: light.hall
        state: "off"
  action:
    - service: light.turn_on
      entity_id: light.hall
    - service: input_boolean.turn_on
      entity_id: input_boolean.door_timer

We can check this is working by seeing what happens to the input_boolean switch in the frontend when the door is opened. If the light was not already on, opening the door will turn the switch on, otherwise it won’t.

When switching the light off we need to check that the timer is on so that we don’t turn the light off when it’s been switched on manually. In the actions, we’ll also switch the timer flag off.

- alias: "Turn light off 10 minutes after door closed"
  trigger:
    platform: state
    entity_id: sensor.front_door_contact
    to: "closed"
    for:
      minutes: 10
  condition:
    condition: state
    entity_id: input_boolean.door_timer
    state: "on"
  action:
    - service: light.turn_off
      entity_id: light.hall
    - service: input_boolean.turn_off
      entity_id: input_boolean.door_timer

We now have automatic lighting for the hall that sensibly respects manual changes.

Hopefully this demonstrates the thought processes of moving from a simple requirement – turning the light on when the door opens – into a fully fledged automation that doesn’t have unwanted side effects.

in Home Automation

Related Posts