so

Home Assistant

Volume 5, Issue 25; 19 Sep 2021

A brief suvey of my HA setup and a few automations.

After my post about the new doorbell, a couple of folks asked me to say more about what I’ve done. To that end, here’s a little tour/brain dump of an automation novice. Everyone likes pictures, so let’s start with a picture.

Home Assistant makes it easy to create custom “dashboards”. Most of them, as far as I can tell, involve placing “cards” of various sorts on the page, which works fine. But you can also load images and then overlay icons on them with CSS.

Here’s a plan view of the downstairs, as seen on one of my dashboards:

[Photo]
Library plan view

That’s a plan view of all the lights and motion sensors downstairs in the library, a couple of closets,Why, you ask, is there no motion sensor for the light in the other closet? Because, I kid you not, there is no light fixture in that closet! and the entry vestibule. The plan isn’t up to architectural drawing standards but it’s not too bad. The blue lights are off, the yellow ones are on. The overlays are dynamic and interactive, I can toggle the state of a light by clicking on it in the image.

Below the plan views are buttons for turning off the lights in various zones. And below that are a couple of temperature graphs that I added just for fun.

Coincidentally, within the last few days, I came across a Reddit post that provided a template to summarize an HA configuration (I don’t really know anything about these kinds of templates yet, I just did the cut-and-paste). Apparently:

I have 233 states in Home Assistant.
By domain these are;
- 17 automation
- 16 binary_sensor
- 42 device_tracker
- 16 light
- 5 media_player
- 2 person
- 1 remote
- 14 scene
- 108 sensor
- 1 sun
- 7 switch
- 3 weather
- 1 zonex

If you’re interested in playing with HA, the first question has to be “what are you going to connect to it?” Mostly, that’s lights for us, with motion sensors and switches thrown into the mix. But technically, we also have the wifi router, the Apple TV, our phones, several wireless speakers, a couple of meteorology APIs, and, of course, the doorbell.

Most of our lights are Philips Hue lights. They looked like a good choice when I started doing this a while back and they’ve been reliable. I think there are less expensive brands available now. There are a few other Zigbee devices mixed in: the doorbell and a couple of smart plugs on the bedside lamps. (They’re small, vintage lamps that won’t accept any kind of programmable LED, but with the smart plugs, Home Assistant can turn them on and off.)

We don’t have any security cameras and I’m not willing to put electronic locks on the doors or windows. Nice solid deadbolts and physical keys are just fine, thank you very much.

Looking back at that template output, a couple of numbers stand out: are there really 42 trackers and 108 sensors?

Sort of.

The wifi router is providing most of the “device trackers”: it “tracks” every device to which it assigns (or perhaps has ever assigned) an IP address. That’s one for every phone, tablet, computer, printer, etc.

There are 108 sensors, but that’s because each of the motion sensors provides an individual motion, light level, battery level, and temperature sensor. The router provides sensors for uptime, downtime, bytes sent, bytes received, etc., etc., and the printers provide separate sensors for each color of ink. Plus there are sensors for the battery level in phones, tablets, buttons, etc.

All of these are available for automation. Want to have the system react when you’ve downloaded more than 10Gb of data, when the Cyan ink cartridge in the printer is down to 20%, or the battery in your tablet is less than 50%? Have at it!

You can (I think) make an integration for anything with a web API. Our energy company has just such an API that will report half hourly electricity and gas usage data; I’ll be looking at integrating that soon. I’d also like to replace the completely mechanical, analog thermostat with something a little smarter. Whether we need something that can be automated or just something programmable, I’m not sure.

Automations

Automations are fun, but let me start by saying you don’t have to write your own, if the programming aspect doesn’t appeal to you. All of the light switches and sensors can be programmed to do simple things directly in the Hue app.

There are a few ways to do automations. I’m still just playing with the automations system built into Home Assistant. There’s also a system called Node-RED that has more of a visual programming interface, but I haven’t tried it.

There’s a browser UI for editing automations, but they’re actually stored as YAML. I’m going to show the YAML here because it’s going to be easier than putting in a bunch of screenshots.

Coming home

To get a feel for things, let’s start with a simple automation to turn on some lights when someone comes home. (I’ve simplified this automation, and the rest, by removing incidental properties: device IDs and types, and details like the light brightness.)

alias: Coming home
description: Turn on the downstairs lights when motion is detected in the vestibule
trigger:
  - type: motion
    platform: device
    entity_id: binary_sensor.motion_vestibule

The “trigger” property defines what starts the automation. If motion is detected by the motion sensor in the vestibule …

condition:
  - condition: time
    after: '03:59:59'
    before: '19:00:00'

You can add conditions, such as if it’s between 4am and 7pm, …

(I’ll talk about why I’m doing that here in a moment.)

action:
  - type: turn_on
    entity_id: light.vestibule
  - type: turn_on
    entity_id: light.library_ceiling
  - type: turn_on
    entity_id: light.library_stairs

And the actions follow. In this case, turn on the lights in the vestibule and the library, and on the library stairs.

  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0

Wait ten minutes.

  - type: turn_off
    entity_id: light.vestibule
  - type: turn_off
    entity_id: light.library_stairs
  - type: turn_off
    entity_id: light.library_ceiling

And turn them all off.

mode: restart

The restart mode means if the motion sensor is tripped again before 10 minutes passes, the automation starts over. This prevents the lights from going out suddenly if we come home 9 minutes apart.

This is the “during the day” automation for coming home. I have different automations for the evening because the TV is located in the library. Between 7pm and 4am, if the library lights are already on when someone comes home, don’t turn them off after 10 minutes!

I only have two automations that I think are interesting, the one for turning off the lights in the library at the end of the day and the doorbell.

End of the day

The library lights are controlled by motion sensors. When I first set them up, I didn’t have any extra automatable switches, so the only way to turn off the lights before the timer expired was to poke the HA application on some device.

Then I realized that at the end of the evening, we both turn on our bedside lamps. My automation idea was that if we both turn on our bedside lamps, then we’re not downstairs and the library lights can be turned off right away!

alias: Going to bed (N second)
description: Turn off the library lights when we go to bed
trigger:
  - platform: device
    type: turned_on
    entity_id: light.ns_bedside_lamp

If my lamp is turned on…

condition:
  - condition: device
    type: is_on
    entity_id: light.bs_bedside_lamp

And Bethan’s lamp is already on…

  - condition: time
    after: '21:00:00'
    before: '09:00:00'

And it’s in the evening…

action:
  - scene: scene.dark_library

Turn off all the downstairs lights. (I should rework the “coming home” automation to use scenes too, I only discovered them recently. The dark_library scene is all the downstairs lights turned off.)

mode: single

Only do this once. There’s a corresponding automation for Bethan’s lamp if mine is already on, so it doesn’t matter what order they are turned on and the lights don’t go off downstairs until they’re both turned on.

The doorbell

Here’s the one you came to see. I’m sure it can be improved, but it works!

alias: Doorbell
description: Someone's knocking!
trigger:
  - platform: device
    type: remote_button_short_press
  - platform: device
    type: remote_button_long_press

The Zigbee button that I’m using supports several different patterns. You can detect short press, long press, and a few other combinations. That’s great if you’re using the button, but for this purpose, I’m treating either short press or long press as “ring the doorbell.”

condition: []

This trigger is unconditional. And now the action begins.

action:
  - service: notify.mobile_app_tamarack
    data:
      message: Ding dong!

Send a notification to my phone. Fun fact: if I’m away from home, I still get this notification.

  - service: notify.mobile_app_simbeline
    data:
      message: Knock knock!
      title: Doorbell

Send a notification to Bethan’s phone.

  - service: sonos.snapshot
    data: {}
    entity_id: all

Take a snapshot of the current state of the Sonos devices. The Sonos integration documents all these services, I just plugged in the ones I needed.

  - service: media_player.volume_set
    data:
      volume_level: 0.1
    target:
      entity_id: media_player.office

Set the volume to something gentle. I want to hear the doorbell, I don’t need it blasted at me! I chose the Sonos in my office as the controller for this automation, mostly because I was in my office when I wrote it. I don’t think it would make any difference if I chose a different controller.

  - service: sonos.join
    data:
      master: media_player.office
    entity_id: media_player.bedroom

Add the bedroom speakers to this group.

  - service: sonos.join
    data:
      master: media_player.office
    entity_id: media_player.study

Add the speakers in Bethan’s study.

  - service: media_player.play_media
    data:
      media_content_id: http://192.168.178.22/klingons.mp3
      media_content_type: music
    target:
      entity_id: media_player.office

Play “There’s Klingons on the starboard bow!”

  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0

Wait ten seconds.

  - service: sonos.restore
    data: {}
    entity_id: all

Put everything back the way it was. This means that, for example, if the music is playing in one or more rooms, it goes back to playing after the doorbell. (I’ve seen reports that this may not be true if you’re playing from Spotify, but I haven’t tried it.)

mode: single

Only do this once.

I think that’s pretty much all the interesting stuff.

Does it work?

If you stop to think about all the coordination going on between lights and switches, the Hue and Zigbee controllers, the Raspberry PI, the HA software, phones, tablets, and laptops, it’s kind of remarkable how well it all works.

That said, it doesn’t always work so well. Every now and then, for example, the motion detectors slow down. Instead of triggering immediately, they take three, four, five or more seconds to respond. Kind of a nuisance if you’re walking down a dark stairway. Less frequently, the lights seem to come on at some random brightness. I know of one occassion when someone claims they pushed the doorbell button and nothing happened.

I haven’t detected a pattern to any of these failings. If I’m in a grumpy mood, I restart the HA box, but I’m not sure if that’s relevant. Left alone, they seem to sort themselves out over the course of a few hours.

I dunno. I’m a programmer. It’s software. I don’t expect it to work flawlessly.

Would I integrate a washing machine? Sure. My thermostat? Maybe. The locks on my house? Not a chance. But YMMV.