Automations with Home Assistant



Originally posted 2018-03-23 12:28:35.

incorrect use of automations in home assistantIn a previous article, I blogged about Integrating Google Home with Home Assistant. In that article I made a connection between doing this and setting up automations which is incorrect. So in this article, I want to correct the record and make the correct case for automations.

So what’s wrong with doing it like this? Well these automations will ‘work’ in that if you click on the text and click on trigger, they will turn the light on or off then and there. But how is that better than just clicking the on/off toggle on the lights card? Answer – it isn’t. It’s just an unnecessary complication!

I already knew I wasn’t using the automations and I had previously wondered why I had them and what use they were but it wasn’t until recently that I had saw that they were having some undesirable side effects!

I had configured an automation that looked like this:

- action:
  - data:
      brightness_pct: 75
      entity_id: light.lounge
      kelvin: 3100
    service: light.turn_on
  alias: Turn Lounge Light On 1:50 before Sunset
  condition: []
  id: '1'
  trigger:
  - event: sunset
    offset: -01:50:00
    platform: sun

This automation turns the lights on 1hr and 50mins before sunset and is a moving target in that this time is different every day throughout the year. It turns the lights on when it’s getting dark in my lounge room. I am now using a different way of doing this by using the solar angle instead of a time offset. This automation also sets the brightness to 75% and sets the colour temperature to 3100° Kelvin – warm white. Nice!

However, I also had another automation that I could use when we were watching a movie at night. This automation looked like this:

- action:
  - data:
      brightness_pct: 30
      color_name: blue
      entity_id: light.lounge
    service: light.turn_on
  alias: Lounge Lights Movie
  condition: []
  id: '2'
  trigger:
  - entity_id: lounge.light
    from: 'off'
    platform: state
    to: 'on'

When activated, it would turn the lights blue at 30% brightness. So far so good!

However I was noticing for some inexplicable (then) reason that if I used the light toggle or a voice command to turn the light on that it would come on blue every time! This was maddening! I hadn’t used the automation I was just using the on/off. Can you see though what was happening? The movie automation was set to trigger when the light turned from Off to On!!! So DUH! When I turned it on, it went blue. If I then used the normal automation again, it would go to the right colour and brightness.

  
  

Of course it’s obvious NOW but I didn’t need an automation AT ALL for the movie lights and I certainly didn’t need one that triggered every time I turned the lights on. It turns out that Home Assistant has a much better way to do this which is by setting scenes up.

Setting up scenes in Home Assistant

Here are my lounge light scenes in Home Assistant:

# Lounge Light Scenes 
 - name: Lounge 100
   entities:
     light.lounge:
       state: on
       transition: 2
       brightness_pct: 100
       kelvin: 3100
 - name: Lounge Normal
   entities:
     light.lounge:
       state: on
       transition: 2
       brightness_pct: 75
       kelvin: 3100
 - name: Lounge Movie
   entities:
     light.lounge:
       state: on
       transition: 2
       brightness_pct: 30
       rgb_color: [0,0,255]
 - name: Lounge Daylight
   entities:
     light.lounge:
       state: on
       transition: 2
       brightness_pct: 75
       kelvin: 6500
 - name: Lounge Warm
   entities:
     light.lounge:
       state: on
       transition: 2
       brightness_pct: 75
       kelvin: 2700

You can see I have a number of different scenes for the lights with different colours, warmth and brightness. All of these get synced to Google Assistant and I can control them by voice. “OK Google activate lounge movie” Or I can also define a shortcut in Google Home to activate the correct scene.

So Automations are used to trigger an action when triggered by some event (like 1:50 before sunset) and scenes can be used to change settings.

Incidentally, I also have a scene that turns on my office lights and a couple of lights on the way to and in my bedroom so instead of stumbling around bumping into things in the dark, the way is lit before I get there just by saying “OK Google Bedtime”

Other uses and automations I use turn my coffee maker on in the morning at 7am weekdays or 8am on the weekends, turn it off at 10pm or another one that turns it on on every 4th Sunday at 7am as I get up early that day and like coffee before I go out. It also turns it off when I go out and back on again in the afternoon when I am expected home. A few other things as well but you get the idea.

So don’t use automations unless you have a specific action you want for a specific trigger. Automations shouldn’t need to be manually triggered in the Home Assistant GUI.

 



Leave a Reply