+++

class = "post"
date = "2022-12-24T00:00:00-05:00"
tags = ["diy","automation","buildlog"]
title = "Automatic Home Automation Control, Part 2: Electric Boogaloo"
description = "Because voice control is so passé"
type = "post"
weight = 1

+++

Nearly 5 years ago, I [set up a voice control system for my home automation](https://www.boniface.me/self-hosted-voice-control/). While this had served me fairly well, it's not without its drawbacks. First, it's slow even when everything works right - it's a bit of a pain to walk into a dark room and have to wait 3 seconds saying "computer, lights on" before having light, especially when you're as messy as me. And of course, if Google STT decides it's going to take 15 seconds to respond, that just slows it down even further, and that's when it even gets a result.

Things really came to a head a few weeks ago when I got myself a new heater for my garage. As a smoking refuge, I spend a decent amount of time there, and it gets very cold in the winter; having a 1500W oil heater on all day to keep it vaguely warm just wasn't cutting it any more, so I invested in a nice [infrared heater unit](https://www.amazon.ca/dp/B077JM5PB9). The downside is, this unit can only be controlled via a remote. I really didn't want to deal with setting up another voice control station to try to automate this, and deal with the delays while shivering in the cold.

I needed to step up my home automation game to the next level!

What followed was 2 weeks of trial and error, missed steps and a few dead ends, but in the end I now have a really robust automation scheme that I'm very happy with, and not just for the garage, but for my bedroom and desk as well. Read on to learn what I've configured and how it works.

## Triggering Automations

Of course, to set up automations, I needed some triggers. With voice control, this was voice commands, but as mentioned I wanted something more robust and automatic. Enter a room, actions happen. So I went with Bluetooth LE presense detection and PIR sensors.

### Presence Detection

I started the journey with presence detection using Bluetooth LE and ESPresense base stations communicating with Home Assistant via MQTT. I already had Home Assistant and MQTT set up, so I won't cover that here.

I purchased a set of [6 ESP32 modules](https://www.amazon.ca/dp/B0B3MY3P9T) (and the [corresponding antennas to make them work, see my review there](https://www.amazon.ca/dp/B07X5XD4NR)), and loaded them up with ESPresense. The web flashing tool from the [ESPresense website](https://espresense.com/firmware) works quite well in Chromium but not in Firefox. With 4 units flashed, I set about configuring Bluetooth LE.

On Apple devices and a lot of smart wearables, Bluetooth LE beacons are built-in and generally always transitting. On Android, not so much. Luckily, after a bit of trial and error, I found that the Home Assistant Android App, which I was already running, can send Bluetooth LE beacons as well, which greatly simplified things.

A bit of light configuration of the modules later, and I had excellent presence detection capabilities! Each module is set a little bit differently (specifically, its max reporting distance) to keep them fairly limited to the area I wanted, and while there is a bit of overlap and occasional drifting in distance, it works more than well enough for the automations I use it for below.

![Home Assistant dashboard for location sensors](/images/home-automation-2-electric-boogaloo/location-sensor.png)

The sensor for this is provided by `mqtt_room` with my phone's BLE iBeacon ID. The relevant configuration is:

```
sensor:
  - platform: mqtt_room
    name: "Joshua's S10e"
    device_id: "iBeacon:aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa-100-1"
    name: "ibeacon_joshua_phone"
    state_topic: 'espresense/rooms'
    timeout: 30
    away_timeout: 30
```

I do have ESPresense limited to the specific iBeacon IDs of my phone, to prevent other random devices from neighbours and guests affecting my system. The timeout and away timeout also ensure that, when I leave a beaconed area for a non-beaconed area in my house, I will time out fairly quickly.

## Motion Detection

PIR motion sensors are the most basic kind. While I experimented with Microwave Radar sensors as well, I required the PIR ability to "stop at walls", and thus went with the tried-and-true standard. I grabbed a [set of 5 sensors](https://www.amazon.ca/dp/B012ZZ4LPM) and started configuring them.

First, I lowered the sensitivity to the minimum. I *don't* want these sensors triggering from a mouse or a stiff breeze, only major (human) motion. I also adjusted the timeout to be approximately 30-60 seconds and set retriggering on, so the sensor will ideally output a constant logical HIGH signal whenever there is significant motion and wait about 30-60 seconds before returning to a logical LOW.

One major thing to note about these sensors is that I've found strange triggering when they are mounted vertically; I'm not sure exactly what they're "seeing" in this position, but it resulted in a lot of false triggers. Make sure you mount them horizontally instead.

Luckily ESPresense features sensor support, so adding the motion detection sensors to the system was as easy as connecting them up to the ESP32 boards using digital pin 26 (27, and any others along that edge would cause a boot failure) and configuring the pin in the ESPresense WebUI.

One very important thing I found was to make sure you set the pin to `pulldown` mode; if set to `pullup`, which is the default, the sensor will report a logical HIGH when the sensor "disconnects" should it do so, which for at least some of my automations could be very bad (don't want the heater turning on, and never turning off, if the sensor goes faulty!) By setting this to `pulldown` we ensure a logical LOW should the sensor ever stop responding, which I have seen happen in the cold environment of the garage.

## Temperature Monitoring

One last aspect of my automations, at least for my heater, was temperature sensing. I don't want the heater to turn on if it's nice out, so I also added an AM2302 temperature/humidity sensor to each ESP32. Like the PIR sensor, this is connected to a digital pin on the ESP32 (digital pin 25) and the `DHT22` pin value set. I find [the sensors I use](https://www.amazon.ca/dp/B08XQNKW4P) to be fairly accurate, or at least accurate enough for my purposes, so I don't set any offsets.

With all 3 elements configured for all 3 initial ESPresense modules, I can now see the motion detection, temperature, and state in Home Assistant for each of my 3 main room sensors, plus a fourth without any sensors (yet!)

![Home Assistant dashboard for location sensors](/images/home-automation-2-electric-boogaloo/espresense-sensors.png)

These sensors are all autodetected with MQTT autodiscovery in Home Assistant, so no YAML configuration was needed; I just needed to create the entity cards in the Lovelace UI to get this dashboard.

## Heater Control

With the triggers out of the way, I had to move on to controlling the garage heater itself. For all my other devices, which are all lights, I just use simple smart plugs or switches (Wemo and Kasa brands). But this heater has a remote control, and most importantly, does not "remember" last state when a smart switch turns it off. So I needed to create an IR blaster to turn it on.


